SQL 具有“非空”约束的唯一键和主键有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20404730/
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-08-31 23:21:06  来源:igfitidea点击:

What is difference between unique key with 'not null' constraint and primary key?

sqlsql-server-2008

提问by Bhavesh Kachhadiya

I want to know difference between these two key.

我想知道这两个键之间的区别。

When the Unique key with not nullconstrain in terms of how they are stored in database

当唯一键not null限制它们在数据库中的存储方式时

and what difference are there when we making Select,Insert,Update, Deleteoperation for these keys.

以及我们Select,Insert,Update, Delete对这些键进行操作时有什么区别。

回答by D Stanley

A primary key must be unique and non-null, so they're the same from that standpoint. However, a table can only have one primary key, while you can have multiple unique non-null keys.

主键必须是唯一的且非空的,因此从这个角度来看它们是相同的。但是,一张表只能有一个主键,而您可以有多个唯一的非空键。

Most systems also use metadata to tag primary keys separately so that they can be identified by designers, etc.

大多数系统还使用元数据单独标记主键,以便设计人员等可以识别它们。

What are the differences between a primary key and a Unique key with not null constrain in terms of how they are stored in database

就它们在数据库中的存储方式而言,主键和具有非空约束的唯一键之间有什么区别

If both are either CLUSTEREDor NON CLUSTEREDthen the only difference is metadata in most systems to tag a index as a PK.

如果两者都是CLUSTEREDNON CLUSTERED那么唯一的区别是大多数系统中的元数据将索引标记为 PK。

what difference are there when we making Select,Insert,Update, Deleteoperation for these keys

当我们对这些键进行Select, Insert, Update,Delete操作时有什么区别

None.

没有任何。

回答by jean

To answer you comment, Yes! In general there is one huge difference in how unique keys and primary keys are stored in SQL Server 2008.

回答你的评论,是的!一般来说,唯一键和主键在 SQL Server 2008 中的存储方式存在巨大差异。

A unique key by default(you can change that) will be created as a non-clustered index and a PK will be created as a clustered index by default(you can change that also).

默认情况下,唯一键(您可以更改)将创建为非聚集索引,PK 将默认创建为聚集索引(您也可以更改)。

Non-clustered means it will be stored in a structure "attached" to the table and will consume disk space.

非集群意味着它将存储在“附加”到表的结构中,并会占用磁盘空间。

Clustered means the records will be actually stored in that physical order, no consuming disk space, and that's why your table can own just one clustered index. (Just if you are wondering... no, you cannot get 2 non-clustered PK in a table, PK are unique even if they are non-clustered.)

聚簇意味着记录将实际按物理顺序存储,不占用磁盘空间,这就是为什么您的表只能拥有一个聚簇索引的原因。(如果您想知道……不,您不能在表中获得 2 个非集群 PK,即使它们是非集群的,PK 也是唯一的。)

回答by Glen Joseph

a. Both primary key and unique key enforce uniqueness of the column on which they are defined. But by default, the primary key creates a clustered index on the column, whereas unique key creates a non-clustered index by default. Another major difference is that primary key doesn't allow NULLs, but unique key allows one NULL only.

一种。主键和唯一键都强制定义它们的列的唯一性。但默认情况下,主键在列上创建聚集索引,而唯一键默认创建非聚集索引。另一个主要区别是主键不允许 NULL,但唯一键只允许一个 NULL。

回答by Andrew

Primary key cannot be null, there can be only one per table.

主键不能为空,每张表只能有一个。

Unique keys can contain nulls, and you can have more than one per table.

唯一键可以包含空值,并且每个表可以有多个。