mysql删除有外链索引数据
Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
查询:DELETE FROM `goods` WHERE `goods_id` = '11'
错误代码: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`webDB`.`goods_properties_detail`, CONSTRAINT `FK_rip_43` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`goods_id`))
以上是因为,该条记录的某个字段作为innodb的另外一个表的外链。而在删除操作时会自动检查外链。
解决办法一:先判断删除关联数据,然后再删除(这样比较符合业务逻辑比较安全)。
解决办法二:不检查外链,设置FOREIGN_KEY_CHECKS变量:
SET FOREIGN_KEY_CHECKS = 0;
DELETE FROM `goods` WHERE `goods_id` = '11'
删除完成后设置
SET FOREIGN_KEY_CHECKS = 1;
ps:关闭唯一性校验
set unique_checks=0;
set unique_checks=1;