在 MySQL 中删除表也会删除索引吗?

发布于 2024-07-21 21:20:02 字数 227 浏览 6 评论 0原文

文档中没有明确提及(http://dev.mysql .com/doc/refman/6.0/en/drop-table.html)。 我问这个问题是因为我刚刚在 Rails 项目中看到了一个奇怪的数据库迁移,其中开发人员在删除表之前删除了所有索引,这似乎没有必要。

It's not explicitly mentioned in the documentation (http://dev.mysql.com/doc/refman/6.0/en/drop-table.html). I ask because I just saw a curious database migration in a Rails project where the developer was removing all the indexes before dropping the table, and that seemed unnecessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉世弥音 2024-07-28 21:20:02

是的,它确实。

但是,如果您有外键约束(例如 RESTRICT)来确保与其他表的引用完整性,则您需要在删除或截断表之前删除这些键。

Yes, it does.

However, if you have foreign key constraints such as RESTRICT that ensure referential integrity with other tables, you'll want to drop those keys prior to dropping or truncating a table.

塔塔猫 2024-07-28 21:20:02

是的,它会删除索引。 如果基础表不存在,则没有理由保留索引。 我怀疑向下迁移只是在一对一的基础上做与向上迁移相反的事情。

Yes it would drop the index. There's no reason to keep the index if the underlying table isn't there. I suspect that the downward migration is just doing the opposite of the upward migration on a one-to-one basis.

岁月如刀 2024-07-28 21:20:02

这是没有必要的。 然而,当表是外键关系的一部分并且删除表会破坏依赖关系时,您的 DROP TABLE 可能会被阻止。

It is unneccessary. Your DROP TABLE might however be prevented when the table is part of foreign key relationships and dropping your table would break the dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文