MySQL_TRUNCATE_清空table里的记录

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==========

原文:http://my.oschina.net/xinxingegeya/blog/339016

mysql 清空row_MySQL_TRUNCATE_清空table里的记录相关推荐

  1. MySQL删除或清空表内数据的方法

    MySQL删除或清空表内数据的方法 一.使用MySQL清空表数据命令:truncate SQL语法为: truncate table 表名 注意: truncate该命令会直接将数据表内数据清空: t ...

  2. mysql data目录 清空_Mysql binlog备份数据及恢复数据,学会这个,我在也不怕删库跑路啦~...

    导读 我一直都主张,技多不压身(没有学不会的技术,只有不学习的人),多学一项技能,未来就少求人一次.网上经常听到xxx删库跑路,万一真的遇到了,相信通过今天的学习,也能将数据再恢复回来~~~ 介绍 记 ...

  3. MySQL删除或清空表中数据的方法

    原标题:MySQL删除或清空表中数据的方法 要想把MySQL中的数据删除或清空,可以通过两种方法来实现:DELETE语句和TRUNCATE TABLE语句. DELETE语句可以将表中所有记录都删除, ...

  4. mysql获取删除的条数_如何从mysql表中删除数百万条记录而不会减速

    有没有一种很好的方法来删除很多记录而不会减慢网站的速度? 我需要从没有索引和主键的MySQL表中删除数百万条记录.我阅读了SO和网上的各种教程,基本策略是限制删除查询,在删除之间休眠一两秒钟,然后重复 ...

  5. mysql返回#1114 – The table ‘xxxx’is full解决方法

    发现执行mysql的 REPLACE INTO 的时候mysql返回 #1114 – The table 'xxxx' is full 这个错误. 以前没有遇到过,于是查找资料解决这个问题.得知是由于 ...

  6. MySQL出现Waiting for table metadata lock的原因以及解决方法

    MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景.而且,一旦alter table TableA的操作停滞在Wa ...

  7. 【MySQL】MySQL出现Waiting for table metadata lock的原因、解决方法

    Waiting for table metadata lock MySQL在进行alter table等DDL操作时,有时会出现Waiting for table metadata lock的等待场景 ...

  8. html 拖拽选择表格,JS拖动选择table里的单元格完整实例【基于jQuery】

    本文实例讲述了JS拖动选择table里的单元格.分享给大家供大家参考,具体如下: 用JS 实现类似Excel里面动态选择单元格的例子,从网上得到的例子,先记录在这里,以后参考用. www.jb51.n ...

  9. linux清空动态文件内容,linux 动态清空 nohup 清空后,发现文件大小不变

    linux 动态清空 nohup 清空后,发现文件大小不变 关注:282  答案:2  mip版 解决时间 2021-01-18 22:48 提问者[舉燈向ηáη] 2021-01-18 15:17 ...

最新文章

  1. Ubuntu安装nodeJS
  2. 构造类斐波那契数列矩阵(矩阵
  3. stm32f103插拔pc端的串口引起的问题及解决办法
  4. 逻辑备库的Swichover和Failover
  5. nssl1468-V【状压,数学期望,dfs】
  6. bzoj 3377: [Usaco2004 Open]The Cow Lineup 奶牛序列
  7. 微服务化的基石--持续集成
  8. 思维导图学习---数据库相关基础思维导图(2)
  9. 物联网识别技术期末复习概要
  10. 23个开源App的App Store地址和源代码
  11. 21天打卡挑战 - 经典算法之折半查找
  12. 天价月饼被套上“紧箍咒”,499元成天花板?
  13. Emscripten中的虚拟文件系统
  14. 【电工基础】电路的基本概念与基本定律
  15. Regex.Replace( )
  16. 体验 TiDB v6.0.0 之 Clinic
  17. 庞东升:我是如何从红杉融资的
  18. 一款MS SQL查询分析工具(1.3M)
  19. SystemTap应用跟踪探测的使用
  20. VMware虚拟机XP系统安装教程【图文教程】

热门文章

  1. 北大陈平原教授:写出优秀的学术论文,“小题大做”是关键
  2. 900 多道 LeetCode 题解,这个 GitHub 项目值得 Star!
  3. 入门 | CNN也能用于NLP任务,一文简述文本分类任务的7个模型
  4. 谷歌大脑联手Hinton提出SimCLR新框架,疯狂提升自监督学习性能 | 北邮毕业生一作...
  5. 用了这么多年 Spring Boot 你知道他爹有多大背景吗?
  6. 赫夫曼编码(基于赫夫曼树的实现)
  7. Mysql高级调优篇——第二章:Explain执行计划深度剖析
  8. gunicorn源码分析
  9. Levenshtein 相似度算法——Levenshtein(编辑距离)
  10. 使用CV2和Keras OCR从图像中删除文本