首页 > 代码库 > 【转】 #1451 - Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

【转】 #1451 - Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法

转载地址:http://blog.csdn.net/donglynn/article/details/17056099

错误

SQL 查询:

DELETE FROM `zmax_lang` WHERE CONVERT( `zmax_lang`.`lang` USING utf8 ) = ‘fr‘ LIMIT 1

MySQL 返回:文档

#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`myreview/zmax_text`, CONSTRAINT `zmax_text_ibfk_1` FOREIGN KEY (`lang`) REFERENCES `zmax_lang` (`lang`)) 




以上是因为,改条记录的某个字段作为innodb的另外一个表的外链。而在删除操作时会自动检查外链。解决的办法之一是,不检查外链,只需将执行语句前加上下句即可:

SET FOREIGN_KEY_CHECKS = 0;

DELETE FROM `zmax_lang` WHERE CONVERT( `zmax_lang`.`lang` USING utf8 ) = ‘fr‘ LIMIT 1