本文介绍了无法删除表:外键约束失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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:

如何删除此表?

推荐答案

这应该可以解决问题:

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.

这篇关于无法删除表:外键约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:53