【转】 #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 问题的解决办法

#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

上一篇:Linux学习之CentOS(十)--虚拟机下的CentOS如何上网


下一篇:HDU 1754 I Hate It(线段树单点更新区间最值查询)