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)
作者:Agoly 出处:https://www.cnblogs.com/qmfsun/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |