【MySQL】ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key

Posted by 西维蜀黍 on 2019-10-20, Last Modified on 2021-09-21

Problem

mysql删除主键重复ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key

Analysis

若一个table 中的某个column是其他table的foreign key,那么,就不能使用对该table 使用truncate 或者drop SQL命令。

Solution

执行以下语句可以执行成功

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE tableName;
SET FOREIGN_KEY_CHECKS=1;

Reference