一,Constraint 是表定义的一部分,用于实现数据完整性。

Data Integrity 由三种类型的constraint实现:

Entity Integrity:数据是唯一的。约束: primary key, unique

Domain integrity:data value符合criteria。约束:check,default

Referential integrity:引用的数据必须存在或联动更新。约束:foreign key

二,constraint是database object,所有的contraint name不能重复,必须是唯一的。共有五种类型:primary key,unique,check,default,foreign key,Each contraint has its own row in the sys.objects catalog view.

create table dbo.dt_test
(
id int identity not null constraint PK_ID primary key,
code int,
name )
)

create table dbo.dt_test_add
(
id ),
code int,
name )
)

Msg 2714, Level 16, State 5, Line 1
There is already an object named 'PK_ID' in the database.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.

通过sys.objects 查看constraint object的信息,每一个constraint object 都必须依附在parent object,也就是一个base table上。

--C = CHECK constraint
--D = DEFAULT (constraint or stand-alone)
--F = FOREIGN KEY constraint
--PK = PRIMARY KEY constraint
--UQ = UNIQUE constraint 

select NAME,object_id,schema_id,object_name(parent_object_id) as parent_object_name,type,type_desc
from sys.objects
where type in('C','D','F','PK','UQ')

三,跟约束有关的两个column properties 是 Identity和 nullability。

when you use the select into command to make a copy of a table, all column names and data types are copied, as well as Identity and nullability, but constraints are not copied to the new table.

四,约束的定义分为两种: table level 和 column level

参考:CREATE TABLE (Transact-SQL)

https://msdn.microsoft.com/zh-cn/library/ms174979(v=sql.120).aspx

五,约束的默认行为

Primary key 约束和unique 约束都会创建一个unique index,Primary key 约束默认创建clustered unqiue index,Unique 约束默认创建nonclustered unique index,索引类型可以通过关键字 clustered 或nonclustered 来修改。

<column_constraint> ::=
[ CONSTRAINT constraint_name ]
{     { PRIMARY KEY | UNIQUE }
        [ CLUSTERED | NONCLUSTERED ]

}

PRIMARY KEY   

Is a constraint that enforces entity integrity for a specified column or columns through a unique index. Only one PRIMARY KEY constraint can be created per table.

UNIQUE 

Is a constraint that provides entity integrity for a specified column or columns through a unique index. A table can have multiple UNIQUE constraints.

CLUSTERED | NONCLUSTERED   

Indicate that a clustered or a nonclustered index is created for the PRIMARY KEY or UNIQUE constraint. PRIMARY KEY constraints default to CLUSTERED, and UNIQUE constraints default to NONCLUSTERED.

In a CREATE TABLE statement, CLUSTERED can be specified for only one constraint. If CLUSTERED is specified for a UNIQUE constraint and a PRIMARY KEY constraint is also specified, the PRIMARY KEY defaults to NONCLUSTERED.

Primary key和Unique的区别是

1.primary key 约束不允许有null值,作为Primary Key 约束的columns 必须是not null;而unique 约束允许有一个null值,在unqieu约束中,null值被认为是相同的,作为unique 约束的columns 可以是null 或 not null。在unique 约束中, (null,A)和(null, null)是不同的,但是(null,null)和(null,null)是相同的。

2.Primary key 约束只能有一个,而unique 约束可以有多个。

3.设计逻辑不同,primary key用于唯一标识表中列,而unique 约束表示数据在table中是唯一的。一个强调的是标识,一个强调的是唯一性。

六,constraint检查的顺序十分重要,一般是按照如下顺序来进行check的。

1, defaut

2, not null

3, check

4,foreigh key

5, unique and primary key

最新文章

  1. CentOS6.5下安装PostgreSQL
  2. P6 EPPM手动安装指南(Oracle数据库)(一)
  3. 使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据
  4. Sublime Text 2 安装与使用SFTP插件
  5. python27+django1.9添加api
  6. asp.net程序中如何使用皮肤更换的小功能
  7. (jQuery||Zepto).extend 的一个小问题
  8. ZYB&#39;s Premutation(有逆序数输出原序列,线段树)
  9. hdu2063+hdu1083(最大匹配数)
  10. 编写高效的jQuery代码
  11. 十七、oracle 权限
  12. Logger.getLogger和 LogFactory.getLog
  13. python json相关问题
  14. Java提高篇(三):内部类和匿名内部类
  15. platform怎么实现数据数据和驱动分离
  16. bootstrap简单使用实例
  17. 移动端rem适配
  18. NoClassDefFoundError com/google/inject/Injector
  19. 猪年设计素材:一波免费猪猪icon已为你备好
  20. zookeepeer使用zkCli.sh命令

热门文章

  1. 复利计算器(软件工程)及Junit测试———郭志豪
  2. [R语言]R语言计算unix timestamp的坑
  3. React虚拟DOM浅析
  4. 从零开始山寨Caffe&#183;捌:IO系统(二)
  5. 未能添加对***.dll的引用 问题解决方法
  6. 第2章 新手必须掌握的Linux命令
  7. [转]关于SVN的操作批处理示例
  8. NFSv4的引用,迁移和备份(用户手册 v0.2)
  9. JavaWeb中读取文件资源的路径问题
  10. HTML学习有感