mysql truncate table_MySQL数据库之MySQL TRUNCATE TABLE语句介绍

本文主要向大家介绍了MySQL数据库之MySQL TRUNCATE TABLE语句介绍 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

Summary: in this tutorial, you will learn how to use the MySQL TRUNCATE TABLE statement to delete all data in a table.

Introduction to MySQL TRUNCATE TABLE statement

The MySQL TRUNCATE TABLE statement allows you to delete all data in a table. Therefore it is like a DELETE statement without a WHERE clause. However in some cases, the MySQL TRUNCATE TABLE statement is more efficient than the DELETE statement.

The syntax of the MySQL TRUNCATE TABLE statement is as follows:

TRUNCATE TABLE table_name

You specify table name after the TRUNCATE TABLE clause to delete all data in the table.

If you are using InnoDB tables, MySQL will check if there is any foreign key constraint available in the tables before deleting data.

If the table has any foreign key constraint, the TRUNCATE TABLE statement deletes rows one by one. If the foreign key constraint has DELETE CASCADE action, the corresponding rows in the child tables are also deleted.

If the foreign key constraint does not specify the DELETE CASCADE action, the TRUNCATE TABLE deletes rows one by one, and it will stop and issue an error when it encounters a row that is referenced by a row in a child table.

If the table does not have any foreign key constraint, the TRUNCATE TABLE statement drops the table and recreates a new empty one with the same definition, which is faster and more efficient than using the DELETE statement especially for big tables.

If you are using other storage engines, the TRUNCATE TABLE statement just drops and recreates a new table.

Notice that the TRUNCATE TABLE statement resets auto increment value to 0 if the table has an AUTO_INCREMENTcolumn.

In addition, the TRUNCATE TABLE does not use the DELETE statement therefore the DELETE triggers associated with the table will not be invoked.

MySQL TRUNCATE TABLE examples

First, create a new table named books for the demonstration:

CREATE DATABASE dbdemo; CREATE TABLE books( id int auto_increment primary key,         title varchar(255) not null )ENGINE=InnoDB;

Second, populate data for the books table by using the following stored procedure:

DELIMITER $$ CREATE PROCEDURE load_book_data(IN num int(4)) BEGIN DECLARE counter int(4) default 0; DECLARE book_title varchar(255) default ''; WHILE counter 

Third, load 10000 rows into the books table. It will take a while.

CALL load_book_data(10000)

Fourth, use the TRUNCATE TABLE statement to see how fast it performs in comparison with the DELETE statement.

TRUNCATE TABLE books;

In this tutorial, we have shown you how to use the MySQL TRUNCATE TABLE statement to delete all data from tables efficiently especially for large tables.

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值