mysql Error : Cannot truncate a table referenced in a foreign key constraint 清空具有外键约束的表时报

在清除mysql表数据时报错:

  1. DELETE from weshares where id >0;
  2. truncate table weshares;     
  3. drop  


以上方式清楚表数据的时候都报以下错误:

Error : Cannot truncate a table referenced in a foreign key constraint (`distribution`.`weshare_delivery_templates`, 

CONSTRAINT `fk_weshare_delivery_templates_weshares` FOREIGN KEY (`weshare_id`) REFERENCES `distribution`.`weshares` (`id`))


原因清空具有外键约束的表就会报错

解决方法:

SET FOREIGN_KEY_CHECKS = 0;   //先归0
TRUNCATE table1;              //在清除数据
SET FOREIGN_KEY_CHECKS = 1;   //能后设置1

你可能感兴趣的:(MySQL)