mysql - TRUNCATE 不在 DELETE 触发器上运行?

标签 mysql triggers ddl truncate

我正在搜索为什么将 TRUNCATE 放在 DDL 而不是 DML 中的主题,我在这里找到了以下答案 Why is truncate DDL?

在这个答案中我找到了下面给出的一句话

“TRUNCATE 不在 DELETE 触发器上运行的事实也使它有别于正常的 DML 操作”

这与 MYSQL Reference Manual 冲突上面写着

“如果 FOREIGN KEY 约束指定 DELETE CASCADE,则子(引用)表中的行将被删除,并且截断表变为空。”

我很迷茫。请详细说明这个问题。

谢谢

最佳答案

MySQL 5.0

13.1.21 TRUNCATE TABLE Syntax

...

For an InnoDB table before version 5.0.3, InnoDB processes TRUNCATE TABLE by deleting rows one by one. As of MySQL 5.0.3, row by row deletion is used only if there are any FOREIGN KEY constraints that reference the table. If there are no FOREIGN KEY constraints, InnoDB performs fast truncation by dropping the original table and creating an empty one with the same definition, which is much faster than deleting rows one by one. (When fast truncation is used, it resets any AUTO_INCREMENT counter to zero. From MySQL 5.0.13 on, the AUTO_INCREMENT counter is reset to zero by TRUNCATE TABLE, regardless of whether there is a foreign key constraint.)

In the case that FOREIGN KEY constraints reference the table, InnoDB deletes rows one by one and processes the constraints on each one. If the FOREIGN KEY constraint specifies DELETE CASCADE, rows from the child (referenced) table are deleted, and the truncated table becomes empty. If the FOREIGN KEY constraint does not specify CASCADE, the TRUNCATE TABLE statement deletes rows one by one and stops if it encounters a parent row that is referenced by the child, returning this error:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1`
FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

...

MySQL 5.1

13.1.34 TRUNCATE TABLE Syntax

...

If there are any FOREIGN KEY constraints that reference the table, InnoDB processes TRUNCATE TABLE by deleting rows one by one, processing the constraints as it proceeds. If the FOREIGN KEY constraint specifies DELETE CASCADE, rows from the child (referenced) table are deleted, and the truncated table becomes empty. If the FOREIGN KEY constraint does not specify CASCADE, the TRUNCATE TABLE statement deletes rows one by one and stops if it encounters a parent row that is referenced by the child, returning this error:

ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1`
FOREIGN KEY (`parent_id`) REFERENCES `parent` (`id`))

Note

In MySQL 5.5 and higher, TRUNCATE TABLE is not allowed for InnoDB tables referenced by foreign keys. For ease of upgrading, rewrite such statements to use DELETE instead.

...

MySQL >= 5.5

13.1.34 TRUNCATE TABLE Syntax

...

  • TRUNCATE TABLE fails for an InnoDB table or NDB 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.

...

关于mysql - TRUNCATE 不在 DELETE 触发器上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35295644/

相关文章:

MySQL在文章日期和评论日期中选择最新的批准日期

php - 分解 CSV 中的大量行并将它们作为批处理的 php 进行处理

powershell - 如何在 powershell 的任务调度程序中设置触发器 "on connection to user session"?

mysql - #1060 - 列名重复 'ID' 为什么?

mysql - MYSQL8.0.11上窗口函数括号错误

MySQL MariaDB 不会用外键创建表

MySQLdump 备份脚本不再工作,获取 "mysqldump: unknown variable ' local-infile= 0'"

c++ - 在 Qt 中使用 C++ 创建 mysql 触发器

SQL Server更新触发器: How to make it work

sql - 如何将 CTE 输出插入 Oracle 中的临时表?