Mysql

當該列已經是主鍵的一部分時,為什麼 MariaDB 還要為該列創建另一個鍵?

  • June 12, 2022
create table foo (
 id int primary key
);

create table bar (
 a int references foo(id),
 b int references foo(id),
 primary key (a, b)
);

show index from bar;

+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| bar   |          0 | PRIMARY  |            1 | a           | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| bar   |          0 | PRIMARY  |            2 | b           | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| bar   |          1 | b        |            1 | b           | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

小提琴:https ://dbfiddle.uk/?rdbms=mariadb_10.6&fiddle=ebd524aa5702fd8c75ef140e5e9264ea

b 上的附加鍵從何而來?這是一個錯誤嗎?使用 MySQL 時不會發生這種情況。

創建附加索引/鍵是因為當只給出“b”時,不能使用多列主鍵來查找行。有關更多資訊,請參閱有關問題的評論和https://dev.mysql.com/doc/refman/5.7/en/multiple-column-indexes.html

引用自:https://dba.stackexchange.com/questions/313175