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 | |
+-----------+---------+------+-----+---------+-------+
4 回答
这意味着该字段是(一部分)非唯一索引 . 你可以发行
要查看有关表结构的更多信息 .
这实际上是一个快捷方式:
无论如何,“Key”属性有三个可能的值:
PRI
UNI
MUL
PRI和UNI的含义非常清楚:
PRI =>主键
UNI =>唯一键
第三种可能性,MUL(你问过)基本上是一个既不是主键也不是唯一键的索引 . 该名称来自"multiple",因为允许多个相同值的出现 . 直接从MySQL documentation:
还有一个最后的警告:
总的来说,MySQL文档非常好 . 如有疑问,请查看!
继续讨论MySQL中的MUL,PRI和UNI是什么?
来自MySQL 5.7文档:
实例
Control group, this example has neither PRI, MUL, nor UNI:
A table with one column and an index on the one column has a MUL:
A table with a column that is a primary key has PRI
A table with a column that is a unique key has UNI:
A table with an index covering foo and bar has MUL only on foo:
A table with two separate indexes on two columns has MUL for each one
A table with an Index spanning three columns has MUL on the first:
A table with a foreign key that references another table's primary key is MUL
坚持在你的新皮层,并将表盘设置为“冰沙” .
对于Mul来说,这对我来说也是有用的文档 - http://grokbase.com/t/mysql/mysql/9987k2ew41/key-field-mul-newbie-question
“MUL表示密钥允许多行具有相同的值 . 也就是说,它不是UNIque密钥 . ”
例如,假设您有两个模型,Post和Comment . Post与Comment有很多关系 . 然后,对于Comment表,有一个MUL键(Post id)是有道理的,因为许多注释可以归属于同一个Post .