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

SQL keys, MUL vs PRI vs UNI

MySQL中MULPRIUNI有什么区别?

我正在使用以下命令进行MySQL查询:

1
desc mytable;

其中一个字段显示为MUL键,其他字段显示为UNIPRI

我知道如果键是PRI,则每个表只能有一个记录与该键关联。 如果键是MUL,是否表示可能有多个相关记录?

这是mytable的响应。

1
2
3
4
5
6
7
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| courseid  | int(11) | YES  | MUL | NULL    |       |
| dept      | char(3) | YES  |     | NULL    |       |
| coursenum | char(4) | YES  |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+

1
DESCRIBE <table>;

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

1
SHOW COLUMNS FROM <table>;

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

  • PRI
  • UNI
  • MUL
  • PRIUNI的含义很清楚:

    • 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文档非常不错。如有疑问,请检查!


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

    1
    show create table <table>;

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


    深入了解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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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。