mysql truncate带有被引用外键的表时报错解决方法

时间:2022-10-07 18:26:54
清空具有外键约束的表时报ERROR 1701(42000)的解决
 

mysql> truncate table t_users;
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (`test`.`t_user_action`, CONSTRAINT `t_action_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `test`.`t_users` (`id`))

 
解决方法:
 
mysql> SET foreign_key_checks=0;
 
truncate table xxx;
truncate table xxx;
truncate table xxx;
……
 
mysql> SET foreign_key_checks=1;