MySQL – 唯一外键

本教程将介绍MySQL - 唯一外键的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

MySQL - 唯一外键 教程 第1张

问题描述

我必须使其中一个外键唯一.问题是,我从 phpMyAdmin 收到以下消息:

以下索引似乎相等,应删除其中之一:consignmentnumber_id_UNIQUE、fk_consignments_consignmentnumbers2

所以我的问题是:我应该被打扰吗?没有这样的索引真的很重要吗?

解决方案

每个有键(主键、外键)的列都需要一个索引.与列是唯一的相同.您可能创建了两个索引(一个在创建 FK 时创建,一个在唯一约束上创建).如果是这种情况,只需删除其中一个索引.

数据库维护两个等效索引是开销.

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?

解决方案

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.

好了关于MySQL - 唯一外键的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。