且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-08-23 08:55:09

这应该可以解决问题:

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.