一,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 varchar(11) ) create table dbo.dt_test_add ( id int identity not null constraint PK_ID check(id>0), code int, name varchar(11) )
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
6, trigger fire