如果主键是表中的聚集索引,那么其他列是例如表中的任何唯一列是非聚集的吗?
CREATE TABLE people(
personID int,
FirstName VARCHAR(255),
LastNanme VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
PRIMARY KEY (PERSONID)
);
CREATE TABLE people(
personID int,
FirstName VARCHAR(255),
LastNanme VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255)
PRIMARY KEY (PERSONID)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,一张给定的表只能有一个聚集索引,并且在 InnoDB 中,它始终是主键。该表上的所有其他索引都是非聚集索引。
聚集索引还存储表的所有列,包括非索引列,因此在您的示例中,FirstName、LastName、Address 和 City 作为字段存储在聚集索引的叶节点中。
二级索引仅存储索引列和表的主键。因此,通过二级索引搜索表的所有列将执行两次索引搜索:首先在二级索引中查找您要搜索的值,这将在您要搜索的值所在的行中生成主键值发生。其次,主键值将用于搜索聚集索引,以获取其他非索引列。 InnoDB 存储引擎自动执行这两个步骤,并将所有列交付给您。
Yes, a given table can only have one clustered index, and in InnoDB, it is always the primary key. All other indexes on that table are non-clustered.
The clustered index also stores all the columns of the table, including non-indexed columns, so in your example the FirstName, LastName, Address, and City are stored as fields in the leaf nodes of the clustered index.
A secondary index stores only the indexed columns, and the primary key of the table. So a search for all the columns of a table by a secondary index would execute two index searches: first to find the value you are searching in the secondary index, which would yield the primary key values in the rows where the value you were searching for occurs. Second, the primary key values would be used to search the clustered index, to get the other non-indexed columns. The InnoDB storage engine does both steps automatically, and delivers all the columns to you.