解决外键关联 Cannot delete or update a parent row: a foreign key constraint fails 的mysql报错

1 问题

删除有外键关联的数据或者表的时候,mysql出现报错:

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


2 解决方法

    SET foreign_key_checks = 0;  // 先设置外键约束检查关闭
     
    drop table mytable;  // 删除数据,表或者视图
     
    SET foreign_key_checks = 1; // 开启外键约束检查,以保持表结构完整性

先关闭外键约束,执行删除操作,然后再开启外键约束

猜你喜欢

转载自blog.csdn.net/tjls2008/article/details/90777960