无法删除表:外键约束失败

编程入门 行业动态 更新时间:2024-10-25 14:23:10
本文介绍了无法删除表:外键约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在MySQL中,我想删除一个表. 我做了很多尝试,但始终收到错误消息,名为bericht的表无法删除.这是我得到的错误:

In MySQL I want to drop a table. I tried a lot things but I keep getting the error that the table named bericht can't be dropped. This is the error I'm getting:

#1217-无法删除或更新父行:外键约束失败

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

如何删除此表?

推荐答案

这应该可以解决问题:

SET FOREIGN_KEY_CHECKS=0; DROP TABLE bericht; SET FOREIGN_KEY_CHECKS=1;

正如其他人指出的那样,这几乎不是您想要的,即使这是问题中要问的内容.一种更安全的解决方案是在删除bericht之前删除依赖于bericht的表.请参阅CloudyMarble答案以了解有关操作方法.当我不想或无法删除和重新创建数据库本身时,我会在帖子中使用bash和方法删除数据库中的所有表.

As others point out, this is almost never what you want, even though it's whats asked in the question. A more safe solution is to delete the tables depending on bericht before deleting bericht. See CloudyMarble answer on how to do that. I use bash and the method in my post to drop all tables in a database when I don't want to or can't delete and recreate the database itself.

当其他表对您要删除的表具有外键约束并且您使用的是InnoDB数据库引擎时,会发生#1217错误.此解决方案暂时禁用检查约束,然后重新启用它们.阅读文档以了解更多信息.请确保根据bericht删除表中的外键约束和字段,否则可能会使数据库处于损坏状态.

The #1217 error happens when other tables has foreign key constraints to the table you are trying to delete and you are using the InnoDB database engine. This solution temporarily disables checking the restraints and then re-enables them. Read the documentation for more. Be sure to delete foreign key restraints and fields in tables depending on bericht, otherwise you might leave your database in a broken state.

更多推荐

无法删除表:外键约束失败

本文发布于:2023-10-14 21:44:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492303.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!