MySQL中的MUL, PRI和UNI有什么区别?

我正在处理一个MySQL查询,使用命令:

desc mytable; 

其中一个字段显示为MUL键,其他字段显示为UNI或PRI。

我知道,如果一个键是PRI,那么每个表只能有一条记录与该键相关联。如果一个键是MUL,这是否意味着可以有多个相关记录?

这是mytable的响应。

+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| courseid  | int(11) | YES  | MUL | NULL    |       | 
| dept      | char(3) | YES  |     | NULL    |       | 
| coursenum | char(4) | YES  |     | NULL    |       | 
+-----------+---------+------+-----+---------+-------+

这意味着该字段是一个非唯一索引的(一部分)。你可以发行

show create table <table>;

查看有关表结构的更多信息。

DESCRIBE <table>; 

这实际上是一个快捷方式:

SHOW COLUMNS FROM <table>;

在任何情况下,"Key"属性有三个可能的值:

革命制度党 大学 MUL

PRI和UNI的含义非常清楚:

PRI =>主键 UNI =>唯一键

第三种可能是MUL(您所询问的),它基本上是一个既不是主键也不是唯一键的索引。这个名字来源于“multiple”,因为相同的值允许多次出现。直接从MySQL文档:

如果Key为MUL,则该列是非唯一索引的第一列,在该列中允许多次出现给定值。

还有最后一个警告:

如果一个表的给定列应用了多个Key值,则Key将按PRI、UNI、MUL的顺序显示优先级最高的Key值。

总的来说,MySQL文档非常好。如果有疑问,就去看看吧!

在MySQL中什么是MUL, PRI和UNI ?

从MySQL 5.7文档:

如果“Key”为“PRI”,则该列为“PRIMARY Key”或多列“PRIMARY Key”中的一列。 如果Key为UNI,则该列是UNIQUE索引的第一列。(UNIQUE索引允许多个NULL值,但您可以通过检查NULL字段来判断列是否允许NULL。) 如果Key为MUL,则该列是非唯一索引的第一列,在该列中允许多次出现给定值。

生活的例子

对照组,本例中既没有PRI, MUL,也没有UNI:

mysql> create table penguins (foo INT);
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

一个只有一列并且在这一列上有索引的表有一个MUL:

mysql> create table penguins (foo INT, index(foo));
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

具有主键列的表具有PRI

mysql> create table penguins (foo INT primary key);
Query OK, 0 rows affected (0.02 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

具有唯一键列的表有UNI:

mysql> create table penguins (foo INT unique);
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

一个索引覆盖了foo和bar的表只有在foo上有MUL:

mysql> create table penguins (foo INT, bar INT, index(foo, bar));
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  | MUL | NULL    |       |
| bar   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

在两个列上有两个独立索引的表每个列都有MUL

mysql> create table penguins (foo INT, bar int, index(foo), index(bar));
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  | MUL | NULL    |       |
| bar   | int(11) | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

一个索引横跨三列的表在第一列上有MUL:

mysql> create table penguins (foo INT, 
       bar INT, 
       baz INT, 
       INDEX name (foo, bar, baz));
Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| foo   | int(11) | YES  | MUL | NULL    |       |
| bar   | int(11) | YES  |     | NULL    |       |
| baz   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

具有引用另一个表的主键的外键的表是MUL

mysql> create table penguins(id int primary key);
Query OK, 0 rows affected (0.01 sec)

mysql> create table skipper(id int, foreign key(id) references penguins(id));
Query OK, 0 rows affected (0.01 sec)

mysql> desc skipper;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> desc penguins;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

把它插进你的大脑皮层,调到"冰沙"

对Mul来说,这也是对我有帮助的文档- http://grokbase.com/t/mysql/mysql/9987k2ew41/key-field-mul-newbie-question

MUL意味着该键允许多行具有相同的值。 也就是说,它不是唯一键。”

例如,假设您有两个模型,Post和Comment。Post与Comment有has_many关系。这样,Comment表有一个MUL键(Post id)是有意义的,因为许多注释可以归属于同一个Post。

UNI:对于唯一的:

它是要唯一标识的表的一个或多个列的集合 备案。 一个表可以有多个UNIQUE键。 它很像允许唯一值的主键,但是 可以接受一个主键不接受的空值。

PRI:对于PRIMARY:

它也是表的一个或多个列的集合,用于惟一地标识记录。 一个表只能有一个主键。 它很像UNIQUE键,允许惟一值,但不允许 任何空值。

MUL:用于多个:

It is also a set of one or more columns of a table which does not identify the record uniquely. A table can have more than one MULTIPLE key. It can be created in table on index or foreign key adding, it does not allow null value. It allows duplicate entries in column. If we do not specify MUL column type then it is quite like a normal column but can allow null entries too hence; to restrict such entries we need to specify it. If we add indexes on column or add foreign key then automatically MUL key type added.

让我们用简单的话来理解

PRI -它是一个主键,用于唯一地标识记录。 UNI -它是唯一的键,也用于唯一地标识记录。它看起来像主键,但一个表可以有多个唯一键,唯一键可以有一个空值,另一方面,表可以只有一个主键,不能将null存储为主键。 MUL -它没有唯一的约束,表可以有多个MUL列。

注意:作为一个概念,这些键有更多的深度,但这是一个很好的开始。