当前位置: 首页 > 工具软件 > MUL > 使用案例 >

mysql pri_关于mysql:SQL键,MUL,PRI和UNI

司徒瀚
2023-12-01

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    |       |

+-----------+---------+------+-----+---------+-------+

DESCRIBE

这实际上是以下目的的快捷方式:

在任何情况下,"键"属性都有三个可能的值:

PRI

UNI

MUL

PRI和UNI的含义很清楚:

PRI =>主键

UNI =>唯一键

第三种可能性MUL(您询问的)基本上是既不是主键也不是唯一键的索引。该名称来自"多个",因为允许多次出现相同的值。直接来自MySQL文档:

If Key is MUL, the column is the first column of a nonunique index in which multiple occurrences of a given value are permitted within the column.

最后还有一个警告:

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.

作为一般说明,MySQL文档非常不错。如有疑问,请检查!

"主键必须包含唯一值。" w3schools.com/sql/sql_primarykey.asp

在某些情况下,可以说MUL表示密钥是外键吗?

这个答案比公认的要好得多。

@ robguinness,MySQL文档的读取方式与非英语版本类似。很多时候,他们用3行来解释可以用1行完成的操作。

另请注意,带有外键引用另一个表主键的表是MUL。

@pacerier,关于MySQL文档的详细信息,我同意您的看法。这就是为什么Stackoverflow通常是我首先检查的地方,尤其是如果Im急的话。 ;-)

这意味着该字段是非唯一索引(的一部分)。您可以发出

查看有关表结构的更多信息。

如果该字段是非唯一索引(的一部分),那么为什么MUL仅针对该列而不是所有其他列显示?

必然没有其他涉及的列。非唯一表示同一个值在该列中可以出现多次。

深入了解MySQL中的MUL,PRI和UNI是什么?

从MySQL 5.7文档中:

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 index. (A UNIQUE index permits multiple NULL values, but you can tell whether the column permits NULL by checking the Null field.)

If Key is MUL, the column is the first column of a nonunique index in which multiple occurrences of a given value are permitted within the column.

现场例子

对照组,此示例没有PRI,MUL或UNI:

mysql> create table penguins (foo INT);

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  |     | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

具有一列和一列的索引的表具有MUL:

mysql> create table penguins (foo INT, index(foo));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  | MUL | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

具有作为主键的列的表具有PRI

mysql> create table penguins (foo INT primary key);

Query OK, 0 rows affected (0.02 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | NO   | PRI | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

具有唯一键列的表的表具有UNI:

mysql> create table penguins (foo INT unique);

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  | UNI | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

包含foo和bar索引的表仅在foo上具有MUL:

mysql> create table penguins (foo INT, bar INT, index(foo, bar));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  | MUL | NULL    |       |

| bar   | int(11) | YES  |     | NULL    |       |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.00 sec)

在两列上有两个单独索引的表每个都有MUL

mysql> create table penguins (foo INT, bar int, index(foo), index(bar));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  | MUL | NULL    |       |

| bar   | int(11) | YES  | MUL | NULL    |       |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.00 sec)

具有跨越三列的索引的表的第一行包含MUL:

mysql> create table penguins (foo INT,

bar INT,

baz INT,

INDEX name (foo, bar, baz));

Query OK, 0 rows affected (0.01 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| foo   | int(11) | YES  | MUL | NULL    |       |

| bar   | int(11) | YES  |     | NULL    |       |

| baz   | int(11) | YES  |     | NULL    |       |

+-------+---------+------+-----+---------+-------+

3 rows in set (0.00 sec)

具有外键引用另一个表的主键的表是MUL

mysql> create table penguins(id int primary key);

Query OK, 0 rows affected (0.01 sec)

mysql> create table skipper(id int, foreign key(id) references penguins(id));

Query OK, 0 rows affected (0.01 sec)

mysql> desc skipper;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id    | int(11) | YES  | MUL | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

mysql> desc penguins;

+-------+---------+------+-----+---------+-------+

| Field | Type    | Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| id    | int(11) | NO   | PRI | NULL    |       |

+-------+---------+------+-----+---------+-------+

1 row in set (0.00 sec)

将其粘贴在您的新皮层中,并将刻度盘设置为" frappe"。

对于Mul来说,这对我也是有用的文档-http://grokbase.com/t/mysql/mysql/9987k2ew41/key-field-mul-newbie-question

" MUL表示键允许多行具有相同的值。

也就是说,它不是唯一键。"

例如,假设您有两个模型,"发布"和"评论"。 Post与Comment具有has_many关系。那么对Comment表具有MUL键(Post id)将是有意义的,因为可以将许多评论归因于同一Post。

如果它不是唯一键,为什么要明确提及为MUL?默认情况下它不是唯一的,不是吗?还是我错过了什么?

@SudipBhandari如果您在既不是主要也不是唯一的字段上设置索引,则MySQL会将密钥类型设置为MUL,除了上面的解释之外,这种类型的类型有助于MySQL理解它正在处理的索引类型。

 类似资料: