在复合主键中使用时未创建MySQL外键 创建表测试 ( 用户id int未签名非空, post_id int unsigned not null, 主键(用户id、发布id), 外键测试用户id(用户id)引用用户id, 外键测试后标识(后标识)引用后标识 ); 显示测试结果; +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ |表|非唯一|键|名称|列|名称|排序规则|基数|子|部分|压缩|空|索引|类型|注释|索引|注释| +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ |测试| 0 |主| 1 |用户| id | A | 0 | NULL | NULL | BTREE || |测试| 0 |主| 2 | post | id | A | 0 | NULL | NULL | BTREE || |测试| 1 |测试| post | id | 1 | post | id | A | 0 | NULL | NULL | BTREE || +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

在复合主键中使用时未创建MySQL外键 创建表测试 ( 用户id int未签名非空, post_id int unsigned not null, 主键(用户id、发布id), 外键测试用户id(用户id)引用用户id, 外键测试后标识(后标识)引用后标识 ); 显示测试结果; +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ |表|非唯一|键|名称|列|名称|排序规则|基数|子|部分|压缩|空|索引|类型|注释|索引|注释| +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ |测试| 0 |主| 1 |用户| id | A | 0 | NULL | NULL | BTREE || |测试| 0 |主| 2 | post | id | A | 0 | NULL | NULL | BTREE || |测试| 1 |测试| post | id | 1 | post | id | A | 0 | NULL | NULL | BTREE || +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+,mysql,foreign-keys,primary-key,Mysql,Foreign Keys,Primary Key,添加id列作为主键将产生: show index from test; +-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | C

添加
id
列作为主键将产生:

show index from test;

+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test  |          0 | PRIMARY      |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          1 | test_user_id |            1 | user_id     | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          1 | test_post_id |            1 | post_id     | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
当使用由外键约束内使用的列组成的复合主键时,似乎没有创建索引
test\u user\u id
。应该是这样吗?有没有办法克服这个问题?
(MySQL 5.7)

创建外键约束时,必须为其创建索引。如有必要,创建外键将自动创建一个新索引(至少因为MySQL的一些旧版本,如4.0或其他版本,但有一段时间,甚至旧版本也没有自动创建FK索引IIRC)

但是如果已经存在合适的索引,MySQL就不需要创建新的索引。InnoDB至少有那么聪明

任何包含FK列作为索引最左侧子集的索引都将满足该要求。主键索引(即表的聚集索引)工作正常