MySQL_TRUNCATE

MySQL_TRUNCATE

MySQL TRUNCATE 语法

TRUNCATE [TABLE] tbl_name

TRUNCATE TABLE empties a table completely. It requires the DROP privilege.

Logically, TRUNCATE TABLE is similar to a DELETE statement that deletes all rows, or a sequence of DROP TABLE and CREATE TABLE statements. To achieve high performance, it bypasses the DML method of deleting data. Thus, it cannot be rolled back, it does not cause ON DELETE triggers to fire, and it cannot be performed for InnoDB tables with parent-child foreign key relationships.


Although TRUNCATE TABLE is similar to DELETE, it is classified as a DDL(数据库定义语言) statement rather than a DML(数据操作语言) statement. It differs from DELETE in the following ways in MySQL 5.7:

  • Truncate operations drop and re-create the table, which is much faster than deleting rows one by one, particularly for large tables.

  • Truncate operations cause an implicit(隐式) commit, and so cannot be rolled back.

  • Truncation operations cannot be performed if the session holds an active table lock.

  • TRUNCATE TABLE fails for an InnoDB table if there are any FOREIGN KEY constraints from other tables that reference the table. Foreign key constraints between columns of the same table are permitted.

  • Truncation operations do not return a meaningful value for the number of deleted rows. The usual result is “0 rows affected,” which should be interpreted as “no information.”

  • As long as the table format file tbl_name.frm is valid, the table can be re-created as an empty table with TRUNCATE TABLE, even if the data or index files have become corrupted(败坏的).

  • Any AUTO_INCREMENT value is reset to its start value. This is true even for MyISAM and InnoDB, which normally do not reuse sequence values.

  • When used with partitioned tables, TRUNCATE TABLE preserves the partitioning; that is, the data and index files are dropped and re-created, while the partition definitions (.par) file is unaffected.

  • The TRUNCATE TABLE statement does not invoke ON DELETE triggers.


TRUNCATE TABLE for a table closes all handlers for the table that were opened with HANDLER OPEN.


TRUNCATE TABLE is treated for purposes of binary logging and replication as DROP TABLE followed by CREATE TABLE—that is, as DDL rather than DML. This is due to the fact that, when using InnoDB and other transactional storage engines where the transaction isolation level does not permit statement-based logging (READ COMMITTED or READ UNCOMMITTED), the statement was not logged and replicated when using STATEMENT or MIXED logging mode. (Bug #36763) However, it is still applied on replication slaves using InnoDB in the manner described previously.


TRUNCATE TABLE can be used with Performance Schema summary tables, but the effect is to reset the summary columns to 0 or NULL, not to remove rows. 


测试TRUNCATE TABLE

有两个表t_parent和t_child,t_child表中有一个外键,关联t_parent

TRUNCATE t_parent;

执行这条语句报错:

[TRUNCATE - 0 row(s), 0.000 secs]  [Error Code: 1701, SQL State: 42000]  Cannot truncate a table referenced in a foreign key constraint (`test`.`t_child`, CONSTRAINT `FK_t_child` FOREIGN KEY (`parent_id`) REFERENCES `test`.`t_parent` (`id`))

Code: 1701 SQL State: HY000 --- Cannot truncate a table referenced in a foreign key constraint (`test`.`t_child`, CONSTRAINT `FK_t_child` FOREIGN KEY (`parent_id`) REFERENCES `test`.`t_parent` (`id`))

... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]

因为有外键关联

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE t_parent;
select count(*) from t_parent;

最后查出来的结果是0,首先可以取消外键关联,就可以TRUNCATE表。

============END==========

转载于:https://my.oschina.net/xinxingegeya/blog/339016

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值