MySQL出现无法删除行记录

今天mysql在删除一张InnoDB类型的表时,出现错误Error No. 1451

MYSQL: Cannot delete or update a parent row: a foreign key constraint fails

 

解决方法:

这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。

SET FOREIGN_KEY_CHECKS = 0;

执行删除sql语句;
删除完成后设置 ;
SET FOREIGN_KEY_CHECKS = 1;

 



其他:
关闭唯一性校验
set unique_checks=0;
set unique_checks=1;

 

 

mysql> delete from repositories where repo_name = "watch/sinawatch_agent_update";
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`rhodecode`.`repo_to_perm`, CONSTRAINT `repo_to_perm_ibfk_3` FOREIGN KEY (`repository_id`) REFERENCES `repositories` (`repo_id`))
 
这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况。
 
mysql> SET FOREIGN_KEY_CHECKS = 0;
Query OK, 0 rows affected (0.02 sec)
 
mysql> delete from repositories where repo_name = "watch/sinawatch_agent_update";
Query OK, 1 row affected (0.02 sec)
 
mysql> SET FOREIGN_KEY_CHECKS = 1;
Query OK, 0 rows affected (0.00 sec)
posted @ 2015-09-25 14:20 Agoly 阅读(...) 评论(...) 编辑 收藏