“主键”关键字与 SQL Server 中的聚集索引有何关系?
PRIMARY KEY 关键字与 SQL Server 中的聚集索引有何关系?
(有些人似乎想回答这个问题,而不是我的另一个问题询问,所以我为他们提供了一个更好的地方来这样做。)
How does the PRIMARY KEY keyword relate to clustered indexes in SQL Server?
(Some people seem to want to answer this question instead of a different question I asked, so I am giving them a better place to do so.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
PRIMARY KEY
被实现为聚集索引。但是,您也可以通过非聚集索引来支持它(在其声明中指定NONCLUSTERED
选项)。聚集索引不一定是
PRIMARY KEY
。它甚至可以是非唯一的(在这种情况下,一个名为uniqueifier
的隐藏列被添加到每个键中)。请注意,聚集索引并不是真正的索引(即,以不同方式排序的表的投影,并引用原始记录)。它是表本身,其中原始记录已排序。
当您创建聚集索引时,您并没有真正“创建”任何可以从表中删除的内容。您只需重新排列表本身并更改记录的存储方式即可。
By default, a
PRIMARY KEY
is implemented as a clustered index. However, you can back it by an unclustered index as well (specifyingNONCLUSTERED
options to its declaration)A clustered index is not necessarily a
PRIMARY KEY
. It can even be non-unique (in this case, a hidden column calleduniqueifier
is added to each key).Note that a clustered index is not really an index (i. e. a projection of a table ordered differently, with the references to original records). It is the table itself, with the original records ordered.
When you create a clustered index, you don't really "create" anything that you can drop apart from the table. You just rearrange the table itself and change the way the records are stored.
表的聚集索引通常定义在主键列上。
然而,这并不是严格要求。
来自 MSDN:
和:
The clustered index of a table is normally defined on the primary key columns.
This, however is not a strict requirement.
From MSDN:
And:
顾名思义,主键是表中行的主唯一标识符。聚集索引根据索引对数据进行物理排序。虽然SQL Server默认会聚集一个主键,但是两者之间并没有直接的关系。
A primary key is, as the name implies, the primary unique identifier for a row in your table. A clustered index physically orders the data according to the index. Although SQL Server will cluster a primary key by default, there is no direct relationship between the two.