gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:25:43 25 4
gpt4 key购买 nike

我正在搜索为什么将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com