查看: 15361|回复: 1

请问 mysql key这列里的MUL是什么意思,怎么加呢

[复制链接]
论坛徽章:
8
蜘蛛蛋
日期:2011-07-14 17:45:18紫蛋头
日期:2011-10-21 14:16:35迷宫蛋
日期:2011-11-01 11:23:30ITPUB十周年纪念徽章
日期:2011-11-01 16:23:26灰彻蛋
日期:2011-12-17 14:46:392012新春纪念徽章
日期:2012-01-04 11:53:29蜘蛛蛋
日期:2012-02-28 11:39:462013年新春福章
日期:2013-02-25 14:51:24
跳转到指定楼层
1#
发表于 2008-6-13 15:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题
论坛徽章:
115
生肖徽章:狗
日期:2007-01-06 21:14:12马上有车
日期:2014-03-06 16:45:08马上加薪
日期:2014-05-09 12:27:582014年世界杯参赛球队: 英格兰
日期:2014-07-03 13:10:44青年奥林匹克运动会-竞技体操
日期:2014-09-10 15:30:57马上有钱
日期:2014-10-31 13:56:48美羊羊
日期:2015-03-04 14:48:582015年新春福章
日期:2015-03-06 11:57:31懒羊羊
日期:2015-04-23 19:26:10金牛座
日期:2015-09-17 08:21:44
2#
发表于 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]
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是唯一的即可

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表