简体   繁体   English

无法删除mysql表

[英]Cannot drop a mysql table

I am getting a weird error. 我收到一个奇怪的错误。 I have a table animals , that I am trying to drop. 我有一桌animals ,我想放下。 I cannot drop the table because of a foreign key constraint fails. 由于外键约束失败,我无法删除表。 I know that I must drop the foreign keys before I drop the table. 我知道在放下桌子之前我必须丢弃外键。 The problem is this table has no foreign keys. 问题是这个表没有外键。 I have already dropped them. 我已经放弃了它们。 SQLyog reflects that they have been dropped. SQLyog反映他们已被删除。 But I can still find them in the info schema. 但我仍然可以在info架构中找到它们。 It as if I drop the key half way or something. 好像我把钥匙掉了一半或者别的东西。

How can I drop this table without dropping the database? 如何在不丢弃数据库的情况下删除此表? I need to rekey this table and I just want to drop it and recreate it. 我需要重新调整此表,我只想删除它并重新创建它。

EDIT: Here is the error message from SQL: 编辑:这是来自SQL的错误消息:

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

It doesn't matter if your table animals has foreign keys or not. 你的餐桌animals是否有外键并不重要。 You would be able to drop the table anyway if it had foreign keys. 如果有外键,你可以放弃桌子。

What matters is that there are other tables with foreign keys referencing animals . 重要的是,还有其他表用外键引用animals

Here's how you can check: 以下是您可以检查的方法:

SELECT TABLE_SCHEMA, TABLE_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE (REFERENCED_TABLE_SCHEMA, REFERENCED_TABLE_NAME) = ('mydatabase', 'animals');

Re your comment: 你的评论:

I've never heard of a case where foreign keys were invisible but still preventing the table from being dropped. 我从来没有听说过外键不可见但仍然阻止表被丢弃的情况。 But I suppose it's possible. 但我想这是可能的。

You can drop your table if you do it this way: 如果你这样做,你可以放弃你的表:

mysql> SET FOREIGN_KEY_CHECKS=0;
mysql> DROP TABLE animals;
mysql> SET FOREIGN_KEY_CHECKS=1;

However, I can't predict what may go wrong if the InnoDB tablespace is in a weird state already. 但是,如果InnoDB表空间已处于奇怪状态,我无法预测可能出现的问题。

Ultimately, what you may have to do to clean this up is to dump all your InnoDB tables using mysqldump, then shut down the MySQL daemon, remove the InnoDB tablespace files, and then restart and reimport your dumped data. 最后,你可能需要做的就是使用mysqldump转储所有 InnoDB表,然后关闭MySQL守护进程,删除InnoDB表空间文件,然后重新启动并重新导入转储的数据。 That will eliminate any possibility of tablespace corruption. 这将消除表空间损坏的任何可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM