MySQL - 唯一外键

发布于 2024-10-22 04:44:05 字数 238 浏览 7 评论 0原文

我必须使其中一个外键唯一。问题是,我从 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 技术交流群。

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

发布评论

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

评论(4

软甜啾 2024-10-29 04:44:05

每个具有键(主键、外键)的列都需要一个索引。与唯一的列相同。您可能创建了两个索引(一个在创建 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.

尐籹人 2024-10-29 04:44:05
mysql > create unique index index_bar_id on foos(bar_id);
mysql > alter table foos add constraint index_bar_id foreign key (bar_id) references bars (id);

阅读更多信息 http://sixarm.com/about/mysql-create-indexes-foreign -keys-constraints.html

mysql > create unique index index_bar_id on foos(bar_id);
mysql > alter table foos add constraint index_bar_id foreign key (bar_id) references bars (id);

Read more at http://sixarm.com/about/mysql-create-indexes-foreign-keys-constraints.html

情深已缘浅 2024-10-29 04:44:05

对于未来,如果你想让你的外键唯一,你可以简单地修改你的外键列,如下所示:

ALTER TABLE your_table
MODIFY COLUMN your_fk_column [INT, VARCHAR etc.][NOT NULL] UNIQUE;

For the future, if you want to make your foreign key unique, you can simply modify your foreign key column like this:

ALTER TABLE your_table
MODIFY COLUMN your_fk_column [INT, VARCHAR etc.][NOT NULL] UNIQUE;
岁月打碎记忆 2024-10-29 04:44:05

正如您所知,似乎您也可以拥有唯一的外键:

CREATE TABLE user(
uid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL UNIQUE,
email_id INT NOT NULL UNIQUE,
FOREIGN KEY (email_id) REFERENCES email(uid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,

PRIMARY KEY (uid));

Just so you know, it seems like you can also have UNIQUE foreign keys:

CREATE TABLE user(
uid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL UNIQUE,
email_id INT NOT NULL UNIQUE,
FOREIGN KEY (email_id) REFERENCES email(uid)
    ON DELETE CASCADE
    ON UPDATE CASCADE,

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