SQL truncate 或 drop 带有外键的表

阅读(3493)

当对一个带有外键引用的table进行truncate 或 drop时,会遇到如下提示。

truncate table遇到错误:

ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint ……

drop table遇到错误:

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

解决:

先去除外键检查,操作完成后再开启。

SET foreign_key_checks=0;  # 关闭外键检查
SET foreign_key_checks=1;  # 开启外键检查
Tags: mysql