mysql column key mul_请问 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是唯一的即可

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值