mysql key mul - 看不懂

http://dev.mysql.com/doc/refman/5.1/en/show-columns.html

The Key field indicates whether the column is indexed: 
If Key is empty, the column either is not indexed or is indexed only as a secondary column in a multiple-column, non-unique index. 
If Key is PRI, the column is a PRIMARY KEY or is one of the columns in a multiple-column PRIMARY KEY. 
If Key is UNI, the column is the first column of a unique-valued index that cannot contain NULL values. 
If Key is MUL, multiple occurrences of a given value are allowed within the column. 

The column is the first column of a non-unique index 
or a unique-valued index that can contain NULL values. 
If more than one of the Key values applies to a given column of a table, 
Key displays the one with the highest priority, in the order PRI, UNI, MUL. 

A UNIQUE index may be displayed as PRI if it cannot contain NULL values and there is no PRIMARY KEY in the table.

A UNIQUE index may display as MUL if several columns form a composite UNIQUE index;
although the combination of the columns is unique, each column can still hold multiple occurrences of a given value.

当我们在desc 表名;的时候,有一个Key值,表示该列是否含有索引
假设表结构如下所示

[PHP]
mysql> desc aa;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
| xx    | int(11) | YES  | PRI | NULL    |       |
+-------+---------+------+-----+---------+-------+
| yy    | int(11) | YES  | UNI | NULL    |       |
+-------+---------+------+-----+---------+-------+
| zz    | int(11) | YES  | MUL | NULL    |       |
+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)
[/PHP]
我们看到Key那一栏,可能会有4种值,即'啥也没有','PRI','UNI','MUL'
1. 如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列
2. 如果Key是PRI,  那么该列是主键的组成部分
3. 如果Key是UNI,  那么该列是一个唯一值索引的第一列(前导列),并别不能含有空值(NULL)
4. 如果Key是MUL,  那么该列的值可以重复, 该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以含有空值NULL

如果对于一个列的定义,同时满足上述4种情况的多种,比如一个列既是PRI,又是UNI
那么"desc 表名"的时候,显示的Key值按照优先级来显示 PRI->UNI->MUL
那么此时,显示PRI

一个唯一性索引列可以显示为PRI,并且该列不能含有空值,同时该表没有主键

一个唯一性索引列可以显示为MUL, 如果多列构成了一个唯一性复合索引
因为虽然索引的多列组合是唯一的,比如ID+NAME是唯一的,但是没一个单独的列依然可以有重复的值
只要ID+NAME是唯一的即可

 http://www.itpub.net/thread-1005307-1-1.html

转载于:https://www.cnblogs.com/simhare/archive/2013/05/16/3082807.html

你可能感兴趣的:(mysql key mul - 看不懂)