ITPUB??ì3
ITPUB论坛 » MySQL及其它开源数据库 » 请问 mysql key这列里的MUL是什么意思,怎么加呢

标题: 请问 mysql key这列里的MUL是什么意思,怎么加呢
离线 yang_s_h_u_o
中级会员



精华贴数 0
个人空间 0
技术积分 717 (2606)
社区积分 7 (13166)
注册日期 2007-3-1
论坛徽章:0
      
      

发表于 2008-6-13 15:42 
请问 mysql key这列里的MUL是什么意思,怎么加呢

如题


只看该作者    顶部
离线 atgc
高级会员


精华贴数 1
个人空间 0
技术积分 7944 (155)
社区积分 168 (2534)
注册日期 2004-7-22
论坛徽章:33
2008北京奥运纪念徽章:蹦床2008北京奥运纪念徽章:跳水2008北京奥运纪念徽章:举重2008北京奥运纪念徽章:排球2008北京奥运纪念徽章:花样游泳2008北京奥运纪念徽章:垒球
2008北京奥运纪念徽章:手球2008北京奥运纪念徽章:帆船2008北京奥运纪念徽章:羽毛球生肖徽章2007版:鼠生肖徽章:鸡生肖徽章:狗

发表于 2008-6-13 16:58 
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 code:


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)

我们看到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是唯一的即可


__________________
只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问