gpt4 book ai didi

mysql - 普通 Key 应该包括主键吗?

转载 作者:行者123 更新时间:2023-11-29 04:08:17 29 4
gpt4 key购买 nike

这个问题与 Do MySQL tables need an ID?

有一个无意义的auto_incremental ID作为一个表的PRIMARY KEY,那么我创建其他KEY时,我是否应该在KEY中包含这个ID?

例如,在这个表中:

CREATE TABLE `location` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`country` varchar(50),
`states` varchar(50),
`city` varchar(50),
`county` varchar(50),
`zip` int(5),
PRIMARY KEY(ID),
KEY zip1 (zip),
KEY zip2 (zip, ID)
} ENGINE=InnoDB ;

因为我经常需要使用邮政编码来搜索表格,所以我需要一个从邮政编码开始的 KEY。我应该使用 KEY zip1 或 KEY zip2。这两个KEY哪个更好?

最佳答案

对于 InnoDB,the primary key is always included in secondary indexes ;

All indexes other than the clustered index are known as secondary indexes. In InnoDB, each record in a secondary index contains the primary key columns for the row, as well as the columns specified for the secondary index. InnoDB uses this primary key value to search for the row in the clustered index.

换句话说,ID 已经包含在 zip1 中,并且不必像在 zip2 中那样提及。

关于mysql - 普通 Key 应该包括主键吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21996001/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com