“主键”关键字与 SQL Server 中的聚集索引有何关系?

发布于 2024-09-17 17:52:30 字数 209 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-09-24 17:52:30

PRIMARY KEY 关键字如何与 MS SqlServer 中的聚集索引相关?

默认情况下,PRIMARY KEY 被实现为聚集索引。但是,您也可以通过非聚集索引来支持它(在其声明中指定 NONCLUSTERED 选项)。

聚集索引不一定是 PRIMARY KEY。它甚至可以是非唯一的(在这种情况下,一个名为 uniqueifier 的隐藏列被添加到每个键中)。

请注意,聚集索引并不是真正的索引(即,以不同方式排序的表的投影,并引用原始记录)。它是表本身,其中原始记录已排序。

当您创建聚集索引时,您并没有真正“创建”任何可以从表中删除的内容。您只需重新排列表本身并更改记录的存储方式即可。

How does the PRIMARY KEY keyword related to clustered indexes in MS SqlServer?

By default, a PRIMARY KEY is implemented as a clustered index. However, you can back it by an unclustered index as well (specifying NONCLUSTERED 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 called uniqueifier 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.

少年亿悲伤 2024-09-24 17:52:30

表的聚集索引通常定义在主键列上。

然而,这并不是严格要求。

来自 MSDN

当您创建 PRIMARY KEY 约束时,如果表上尚不存在聚集索引并且您未指定唯一非聚集索引,则会自动在该列上创建唯一聚集索引。

和:

如果指定了非聚集主键约束,您可以在主键列以外的列上创建聚集索引。

The clustered index of a table is normally defined on the primary key columns.

This, however is not a strict requirement.

From MSDN:

When you create a PRIMARY KEY constraint, a unique clustered index on the column or columns is automatically created if a clustered index on the table does not already exist and you do not specify a unique nonclustered index.

And:

You can create a clustered index on a column other than primary key column if a nonclustered primary key constraint was specified.

伤感在游骋 2024-09-24 17:52:30

顾名思义,主键是表中行的唯一标识符。聚集索引根据索引对数据进行物理排序。虽然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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文