MySQL - 唯一外键
我必须使其中一个外键唯一。问题是,我从 phpMyAdmin 收到以下消息:
The following indexes appear to be equal and one of them should be removed: consignmentnumber_id_UNIQUE, fk_consignments_consignmentnumbers2
所以我的问题是:我应该被打扰吗?没有这样的索引真的很重要吗?
I have to make one of the foreign keys unique. The problem is, I am getting the following message from the phpMyAdmin:
The following indexes appear to be equal and one of them should be removed: consignmentnumber_id_UNIQUE, fk_consignments_consignmentnumbers2
So my question is this: should I be bothered? Is it really important not to have such indexes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
每个具有键(主键、外键)的列都需要一个索引。与唯一的列相同。您可能创建了两个索引(一个在创建 FK 时创建,另一个在唯一约束上创建)。如果是这种情况,只需删除这些索引之一即可。
数据库维护两个等效索引是有开销的。
Every column with an key (primary, foreign) needs an index. Same with column being unique. You probably created two indexes (one when creating FK and one on Unique constraint). If this is the case just drop one of those indexes.
It is overhead for the DB to maintain two equivalent indexes.
阅读更多信息 http://sixarm.com/about/mysql-create-indexes-foreign -keys-constraints.html
Read more at http://sixarm.com/about/mysql-create-indexes-foreign-keys-constraints.html
对于未来,如果你想让你的外键唯一,你可以简单地修改你的外键列,如下所示:
For the future, if you want to make your foreign key unique, you can simply modify your foreign key column like this:
正如您所知,似乎您也可以拥有唯一的外键:
Just so you know, it seems like you can also have UNIQUE foreign keys: