主键和非空约束之间有什么区别 [英] What is the difference between Primary key and Not NULL constrain

查看:503
本文介绍了主键和非空约束之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这两个约束之间的基本区别是什么。如果有可能,那么请举例解释。

解决方案

主键只允许跨表的唯一值,并且不允许存储NULL。

非空约束也不允许存储NULL但你可以有重复的值。


简而言之,两个约束都不允许NULL值但是主键需要是唯一的,NOT NULL列不一定是。



其次,默认情况下,主键创建聚簇索引以提高查询性能。此外,每个表只能有一个主键,而不是NOT NULL的情况。


如果你google它你可以找到更多与此相关的资源:



http://www.studytonight.com/dbms/sql-constraints。 php [ ^ ]

I want to know what is the basic difference between this two constrains. If it is possible then please explain with example.

解决方案

Primary key allows only unique value across table and does not allows NULL to be stored.
Not Null constraint also wouldnt allow to store NULL but you can have values which are duplicates.


In a nutshell, both constraints don't allow NULL values however Primary key needs to be unique, NOT NULL column need not be.

Secondly, primary key, by default, creates a clustered index so as to improve query performance. Moreover, there can be only one primary key per table which is not the case for NOT NULL.


if you google it you can find more resources related to this:

http://www.studytonight.com/dbms/sql-constraints.php[^]


这篇关于主键和非空约束之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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