数据库:主键,集群或非集群 [英] database: primary key, Clustered or NonClustered

查看:119
本文介绍了数据库:主键,集群或非集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a database in 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)
)

我想将U_Id用作主键。
我想问一下

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

 CONSTRAINT pk_UserID PRIMARY KEY (U_Id)

this

 CONSTRAINT pk_UserID PRIMARY KEY CLUSTERED (U_Id)

CONSTRAINT pk_UserID PRIMARY KEY NONCLUSTERED (U_Id)

何时使用每个?

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

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

推荐答案

以下语句:

CONSTRAINT pk_UserID PRIMARY KEY (U_Id)

相同就像这样:

CONSTRAINT pk_UserID PRIMARY KEY CLUSTERED (U_Id)

您只能按一个索引对表数据进行物理排序,默认情况下该索引是用于主键的索引(主键唯一性约束始终受索引支持。)

You can only have the table data physicality ordered by one of 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); 

这篇关于数据库:主键,集群或非集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆