简体   繁体   English

主键与聚集索引的关系

[英]Relationship of Primary Key and Clustered Index

Can a TABLE have a primary key without a clustered index? TABLE可以有没有聚集索引的主键吗?

And can a TABLE have a clustered index without having a primary key?一个TABLE可以有一个没有主键的聚集索引吗?

Can anybody briefly tell me the relationship between primary key and clustered index?谁能简单介绍一下主键和聚集索引的关系?

A primary key is a logical concept - it's the unique identifier for a row in a table.主键是一个逻辑概念 - 它是表中行的唯一标识符。 As such, it has a bunch of attributes - it may not be null, and it must be unique.因此,它有一堆属性——它不能为空,而且必须是唯一的。 Of course, as you're likely to be searching for records by their unique identifier a lot, it would be good to have an index on the primary key.当然,由于您可能经常通过唯一标识符搜索记录,因此最好在主键上建立索引。

A clustered index is a physical concept - it's an index that affects the order in which records are stored on disk.聚集索引是一个物理概念 - 它是影响记录在磁盘上存储顺序的索引。 This makes it a very fast index when accessing data, though it may slow down writes if your primary key is not a sequential number.这使它在访问数据时成为一个非常快的索引,但如果您的主键不是序列号,它可能会减慢写入速度。

Yes, you can have a primary key without a clustered index - and sometimes, you may want to (for instance when your primary key is a combination of foreign keys on a joining table, and you don't want to incur the disk shuffle overhead when writing).是的,您可以拥有一个没有聚集索引的主键 - 有时,您可能想要(例如,当您的主键是连接表上的外键组合时,并且您不想招致磁盘洗牌开销写作时)。

Yes, you can create a clustered index on columns that aren't a primary key.是的,您可以在不是主键的列上创建聚集索引。

A table can have a primary key that is not clustered, and a clustered table does not require a primary key.一个表可以有一个非聚簇的主键,聚簇表不需要主键。 So the answer to both questions is yes.所以这两个问题的答案都是肯定的。

A clustered index stores all columns at the leaf level.聚集索引在叶级别存储所有列。 That means a clustered index contains all data in the table.这意味着聚集索引包含表中的所有数据。 A table without a clustered index is called a heap.没有聚集索引的表称为堆。

A primary key is a unique index that is clustered by default.主键是默认情况下聚集的唯一索引。 By default means that when you create a primary key, if the table is not clustered yet, the primary key will be created as a clustered unique index.默认情况下,当您创建主键时,如果表尚未聚簇,则主键将创建为聚簇唯一索引。 Unless you explicitly specify the nonclustered option.除非您明确指定nonclustered选项。

An example, where t1 has a nonclustered primary key, and t2 is not clustered but has a primary key:一个示例,其中t1具有非集群主键,而t2未集群但具有主键:

create table t1 (id int not null, col1 int);
alter table t1 add constraint PK_T1 primary key nonclustered (id);
create clustered index IX_T1_COL1 on t1 (col1);

create table t2 (id int not null, col1 int);
alter table t2 add constraint PK_T2 primary key nonclustered (id);

Example at SQL Fiddle. SQL Fiddle 中的示例。

First of all, take a look at Index-Organized Tables and Clustered Indexes .首先,看看索引组织表和聚集索引 Actually, I recommend reading the whole Use the Index Luke!实际上,我建议阅读整个使用索引卢克! site from the beginning until you reach the clustering topic to really understand what's going on.网站从一开始直到您到达聚类主题以真正了解正在发生的事情。

Now, to your questions...现在,对于你的问题...


Can a TABLE have primary key without Clustered Index?没有聚集索引的表可以有主键吗?

Yes, use NONCLUSTERED keyword when declaring your primary key to make a heap-based table.是的,在声明主键以创建基于堆的表时使用 NONCLUSTERED 关键字。 For example:例如:

CREATE TABLE YOUR_TABLE (
    YOUR_PK int PRIMARY KEY NONCLUSTERED
    -- Other fields...
);

This is unfortunate, since a lot of people seem to just accept the default (which is CLUSTERED), even though in many cases a heap-based table would actually be better (as discussed in the linked article).这是不幸的,因为很多人似乎只接受默认值(即 CLUSTERED),即使在许多情况下基于堆的表实际上会更好(如链接文章中所述)。


and Can a TABLE have Clustered Index without primary key?表可以有没有主键的聚集索引吗?

Unlike some other DBMSes, MS SQL Server will let you have a clustering index that is different from primary key, or even without having the primary key at all.与其他一些 DBMS 不同,MS SQL Server 将让您拥有一个不同于主键的聚集索引,甚至根本没有主键。

The following example creates a clustering index separate from the PK, that has a UNIQUE constraint on top of it, which is what you'd probably want in most cases:下面的示例创建了一个与 PK 分开的聚集索引,它上面有一个 UNIQUE 约束,这在大多数情况下可能是您想要的:

CREATE TABLE YOUR_TABLE (
    YOUR_PK int PRIMARY KEY,
    YOUR_CLUSTERED_KEY int NOT NULL UNIQUE CLUSTERED
    -- Other fields...
);

If you choose a non-unique clustering index (using CREATE CLUSTERED INDEX ... ), MS SQL Server will automatically make it unique by adding a hidden field to it.如果您选择非唯一聚集索引(使用CREATE CLUSTERED INDEX ... ),MS SQL Server 将通过向其添加隐藏字段来自动使其唯一。

Please note that the benefits of clustering are most visible for range scans.请注意,聚类的好处在范围扫描中最为明显。 If you use a clustering index that doesn't "align" with range scans done by your client application(s) (such as when over-relying on the hidden column mentioned above, or clustering on a surrogate key ), you are pretty much defeating the purpose of clustering.如果您使用的集群索引与客户端应用程序执行的范围扫描不“对齐”(例如过度依赖上面提到的隐藏列,或在代理键上集群时),您几乎违背了聚类的目的。


Can anybody briefly tell me the relationship of primary key and clustered index?谁能简单介绍一下主键和聚集索引的关系?

Under MS SQL Server, primary key is also clustered by default .在 MS SQL Server 下,主键默认也是集群 You can change that default, as discussed above.如上所述,您可以更改该默认值。

Answers taken from MSDN Using Clustered Indexes 使用聚集索引MSDN获取的答案

Can a TABLE have primary key without Clustered Index?没有聚集索引的表可以有主键吗? - Yes. - 是的。

Can a TABLE have Clustered Index without primary key? TABLE可以有没有主键的聚集索引吗? - Yes. - 是的。

A Primary Key is a constraint that ensures uniqueness of the values, such that a row can always be identified specifically by that key.主键是一种约束,可确保值的唯一性,以便始终可以通过该键专门标识行。

An index is automatically assigned to a primary key (as rows are often "looked up" by their primary key).索引会自动分配给主键(因为行通常通过它们的主键“查找”)。

A non-clustered index is a logical ordering of rows, by one (or more) of its columns.非聚集索引是行的一个(或多个)列的逻辑排序。 Think of it as effectively another "copy" of the table, ordered by whatever columns the index is across.将其视为有效的表的另一个“副本”,按索引所跨越的任何列排序。

A clustered index is when the actual table is physically ordered by a particular column.聚集索引是指实际表按特定列进行物理排序。 A table will not always have a clustered index (ie while it'll be physically ordered by something , that thing might be undefined ).一个表并不总是有一个聚集索引(即,虽然它会被某物物理排序,但该物可能是未定义的)。 A table cannot have more than one clustered index, although it can have a single composite clustered index (ie the table is physically ordered by eg Surname, Firstname, DOB).一张表不能有多个聚簇索引,尽管它可以有一个复合聚簇索引(即该表按姓氏、名字、DOB 等物理排序)。

The PK is often (but not always) a clustered index. PK 通常(但不总是)是聚集索引。

For what it may be worth, in MS SQL Server all columns in the primary key must be defined as NOT Null, while creating unique clustered index does not require this.值得一提的是,在 MS SQL Server 中,主键中的所有列都必须定义为 NOT Null,而创建唯一聚集索引不需要这样做。 Not sure about other DB systems though.虽然不确定其他数据库系统。

It might not relate as answer to this question, but some important aspects on primary key and Clustered Indexes are ->它可能与这个问题的答案无关,但主键和聚集索引的一些重要方面是 ->

If there is a primary key (By Default Which is Clustered Index, however we can change that) with Clustered Index, then we can not create one more clustered index for that table.如果有一个带有聚集索引的主键(默认情况下它是聚集索引,但是我们可以改变它),那么我们不能为该表创建更多的聚集索引。 But if there is not a primary key set yet, and there is a clustered index, then we can't create a primary key with Clustered Index.但是如果还没有设置主键,并且有聚集索引,那么我们就不能用聚集索引创建主键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM