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

mysql truncate 授权_MySQL_TRUNCATE相关推荐

  1. mysql truncate 授权_Oracle给用户授权truncatetable的实现方案

    1,对其它用户下的表执行trundate table操作 开发说在用dwetl下执行调用shop用户下的表的时候提示没有权限操作,google了查了下,发现oracle账户没法直接赋予对某个表的tru ...

  2. MySQL权限授权认证详解

    MySQL权限授权认证详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL权限系统介绍 1>.权限系统的作用是授予来自某个主机的某个用户可以查询.插入.修改.删 ...

  3. mysql数据库授权

    mysql数据库授权所有人 grant all privileges on *.* to 'root'@'%' identified by 'password'; flush privileges; ...

  4. mysql 57授权失败_MYSQL教程完美解决mysql客户端授权后连接失败的问题

    <MYSQL教程完美解决mysql客户端授权后连接失败的问题>要点: 本文介绍了MYSQL教程完美解决mysql客户端授权后连接失败的问题,希望对您有用.如果有疑问,可以联系我们. MYS ...

  5. mysql truncate table命令使用总结

    truncate使用注意 由于上过truncate table a_table命令一次当,将教训记录下来,以示警戒! mysql truncate table a_table命令受影响结果说明,亲身体 ...

  6. mysql 用户授权

    grant 权限 on 库名.表名 to 用户@"客户端地址" identified by '密码' [with grant option] with grant option 让 ...

  7. mysql 授权 失败_完美解决mysql客户端授权后连接失败的问题

    在本地(192.168.1.152)部署好mysql环境,授权远程客户机192.168.1.%连接本机的mysql,在iptables防火墙也已开通3306端口. 如下: mysql> sele ...

  8. mysql truncate很慢_mysql truncate 的问题

    问题是微信群里一伙计提的 `mysql truncate 空表都需要3 4秒,要优化解决` 一开始觉得这莫名其妙,因为作这种操作的都是后台运维,不是实时的对外服务,运维又不差这3秒 其反应trunca ...

  9. mysql授权许可_分析MySQL的授权许可

    MySQL是开源软件,但开源不意味着免费,开源软件的使用应遵循该软件提供的使用授权许可.MySQL的授权许可是英文的,而且一直以来没有权威的中文译本,所以很多人都不清楚其中的细节. 最近我在做一些AS ...

最新文章

  1. 目标检测计算mAP,AP,Recall,Precision的计算方式和代码(YOLO和FastRCNN等)
  2. BZOJ5089 最大连续子段和(分块)
  3. 让小乌龟可以唱歌——对Python turtle进行拓展
  4. linux 毕业设计 apache,毕业论文基于Linux的Apache服务器的设计与设计6喜欢就下吧...
  5. Apple Pay及其背后的安全技术
  6. Python与C之间的相互调用(Python C API及Python ctypes库)
  7. Ollydbg使用教程学习总结(三)
  8. file 选择的文件胖多有多大_如何删除 macOS 压缩包中的隐藏文件?
  9. 在Matlab2006a中如何创建.net组件
  10. 恶心的Oracle的if else if...
  11. java app退出登录_java – 通过从一个Activity调用一个函数,将退出按钮添加到Android App...
  12. poj 匈牙利二分匹配 1274 The Perfect Stall
  13. 开发 高质量 android应用 pdf,《打造高质量Android应用》读书笔记
  14. 常见8种无线通信协议简介
  15. 学习微信小程序的资料汇总---转载自知乎
  16. 表头顺序不一样的表格如何合并_表头一样的几个excel表怎么合并在一起?
  17. 2022-05 - 英语语法 - 16种时态终极详解
  18. 802.11协议总结
  19. 创建型模式——建造者模式(Builder Pattern)
  20. 周易六十四卦——风火家人卦

热门文章

  1. CentOS系统下安装NVIDIA GPU驱动
  2. codeup|贪心算法|问题 F: 迷瘴
  3. python简单计算器综合实验报告_Python实现的简单计算器功能详解
  4. HDI PCB 板 0.1mm 以下的孔不好造?它不服
  5. php防止项目不给钱,织梦加入隐藏帐号保护版权用户到期不给钱
  6. CSS3常用的几种颜色渐变模式总结
  7. 千万巨钻一元起拍,网购奢侈品渐成趋势?
  8. 百度为什么不学谷歌也搞个B.cn
  9. vue考试系统后台管理项目-接口封装调用
  10. 计算机主机实际功率,一台台式计算机(高配置)消耗一千瓦电几个小时?