database 数据库:主键,集群或非集群

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10706992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:33:15  来源:igfitidea点击:

database: primary key, Clustered or NonClustered

databasesql-server-2008primary-keyclustered-index

提问by YtotheZ

I am creating a database in SQL server 2008,

我正在 SQL Server 2008 中创建一个数据库,

CREATE TABLE Users
(
    U_Id INT NOT NULL
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50) NOT NULL,
    Email VARCHAR(200)
    Password VARCHAR(50)
)

I want to make U_Id the primary key. I would like to ask what is the difference between

我想让 U_Id 为主键。我想问一下有什么区别

 CONSTRAINT pk_UserID PRIMARY KEY (U_Id)

this

这个

 CONSTRAINT pk_UserID PRIMARY KEY CLUSTERED (U_Id)

and this

和这个

CONSTRAINT pk_UserID PRIMARY KEY NONCLUSTERED (U_Id)

When to use each?

什么时候使用?

I read some article but it is still unclear to me. Can someone give me a quick explanation?

我读了一些文章,但对我来说仍然不清楚。有人可以给我一个快速的解释吗?

回答by PAC

The following statement:

以下声明:

CONSTRAINT pk_UserID PRIMARY KEY (U_Id)

Is the same as this one:

和这个一样:

CONSTRAINT pk_UserID PRIMARY KEY CLUSTERED (U_Id)

You can only have the table data physicality ordered by oneof the indexes, and by default that index is the one used for the primary key (the primary key unique constraint is always supported by an index).

您只能让表数据物理性按其中一个索引排序,并且默认情况下该索引是用于主键的索引(主键唯一约束始终由索引支持)。

If you want to leave the order of the table data to be stored according to some other index then you should create the primary key with:

如果要根据其他索引保留表数据的存储顺序,则应使用以下命令创建主键:

CONSTRAINT pk_UserID PRIMARY KEY NONCLUSTERED (U_Id)

And then create the clustered index with:

然后使用以下命令创建聚集索引:

CREATE CLUSTERED INDEX ix_Email ON Users (Email);