为什么主键不能为空 [英] Why Primary key can't be null

查看:3094
本文介绍了为什么主键不能为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我最近的采访中我被问到为什么主键不能为空?

当主键和唯一键都用于唯一标识时一行和一个唯一键可以具有空值,然后为什么主键不能。

期待详细解释。

Hi,
in my recent interview I have been asked why primary key can't be null??
when primary key and unique key both used to uniquely identify a row and unique key can have null value once then why primary key can't.
expecting a details explanation on this.

推荐答案

主键所需的基本属性是什么?



唯一性



那么,NULL有多独特?允许主键包含空值会导致与唯一性约束的潜在冲突。
What is the fundamental attribute required of a Primary Key?

Uniqueness.

So, how unique is NULL? Allowing a primary key to contain nulls would cause a potential conflict with the uniqueness constraint.


从技术上讲,主键约束只是

唯一约束的组合和一个非空约束。





主键表示一列或一组列可以用作

a表中行的唯一标识符。 (这是主键定义的直接结果

。请注意,唯一的约束是什么?b $ b本身不提供唯一标识符,因为它不排除

null值。)这对于文档目的和

客户端应用程序都很有用。
Technically, a primary key constraint is simply a combination of a
unique constraint and a not-null constraint.


A primary key indicates that a column or group of columns can be used as
a unique identifier for rows in the table. (This is a direct consequence
of the definition of a primary key. Note that a unique constraint does
not, by itself, provide a unique identifier because it does not exclude
null values.) This is useful both for documentation purposes and for
client applications.


它来自SQL规范:

It comes from SQL specification:
Quote:

当且仅当

a中没有两行时,才满足唯一约束表在唯一列中具有相同的非空值。在

中,如果使用PRIMARY KEY定义了唯一约束,

那么它要求指定列中没有任何值或

列是空值。

A unique constraint is satisfied if and only if no two rows in
a table have the same non-null values in the unique columns. In
addition, if the unique constraint was defined with PRIMARY KEY,
then it requires that none of the values in the specified column or
columns be the null value.



对于完整的SQL92 - http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt [ ^ ]


这篇关于为什么主键不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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