数据库约束

来源:互联网 发布:网红les淘宝店 编辑:程序博客网 时间:2024/04/29 10:31

2 约束 就是用户输入超出允许范围的数据

(1)check约束:通过限制列的可接受值,强制域的完整性。通过任何基于逻辑运算符号返回true或者false的逻辑值创建check约束。
一个列可以有多个check约束,按照约束的创建顺序进行验证
搜索条件必须为逻辑表达式,并且不能引用其他表
列级别的check约束只能引用被约束的列,表级别的约束只能引用用一个表中的列
不能再text,ntext或image上定义check约束

(2)规则(Rule):执行和check相同的功能。使用check约束是限制列值的首要方法。一个列只能应用一个规则,可以应用多个check约束。check约束和表或者表的某一列绑定在一起。而规则作为数据库对象而单独存在。


create rule chk_id as @id between 10000 and 99999
go

create table rule_sample
(rule_id 
int primary key)
go

sp_bindrule chk_id, 
'rele_sample.rule_id'


(3)unique约束:禁止出现重复的值
如果没有为unique指定clustered或者nonclustered,默认使用nonclustered
每个unique约束都会生成一个索引。unique约束的数目不会使表中的nonclustered索引超过249个,clustered索引超过1个


create table unique_sample
(
unique_column 
nvarchar(10unique nonclustered
)


(4)主键约束
一个表只能包含一个主键约束
由主键约束生成的索引不会使表中的nonclustered索引超过999个,clustered索引超过1个
如果没有为主键指定clustered或者nonclustered,并且没有为unique约束指定clustered索引,主键约束将默认采用clustered
主键约束定义中的所有列必须定义为not null


CREATE TABLE [Multiple_PK_Sample]
(
    columnA 
[nvarchar](20NOT NULL,
    columnB 
[nvarchar](20NOT NULL,
 
CONSTRAINT [PK_Multiple_PK_Sample] PRIMARY KEY CLUSTERED 
 (columnA, columnB)
)

(5)外键约束:保证引用的完整性。确保用户输入的值存在于一个指定的表的对应的列里。
如果FK约束输入非null值,则此值必须在被引用的列中存在。
FK仅能用于同一服务器上同意数据库中的表。跨数据库应用完整性必须通过触发器实现。
FK可以引用同一表中的其他列,此行为成为自引用。比如AdventureWork中HumanResources.Employee的外键FK_Employee_Employee_MangerID
列级别的FK只能引用一个列。两者数据类型必须相同
表级别的FK的引用中列的数目比月和约束列中的列数相同,数据类型也必须相同
FK约束只能引用所引用表的PK或者unique约束中的列或所引用的表中unique index中的列


CREATE TABLE [FK_Sample]
(
    columnA 
[nvarchar](20NOT NULL,
    
foreign key(columnA) references referencetable(columnA) 
)


(6)default约束:当用户未指定值是,sql server自动为其赋值


create table default_value_sample
(
create_date 
datetime default getdate()
)

 

所有的约束的完整sql


-- create the class table
create table testing_class
(
class_id 
int primary key,
class_name 
nvarchar(50not null
)

-- create the student table
create table testing_student
(
--pk
student_id int identity(1,1primary key,
-- check and unique
student_no nvarchar(50constraint chk_no check(student_no between 10000 and 99999unique,
-- check
student_gendar nvarchar(6constraint chk_gendar check(student_gendar='male' or student_gendar='female'),
-- fk
class_id int foreign key(class_id) references testing_class(class_id),
-- default
create_date datetime default getdate()
)


insert into testing_class (class_id, class_name) values(1'AAA')
insert into testing_class (class_id, class_name) values(2'BBB')

-- test the student no check constraint
insert into testing_student
(student_no, student_gendar, class_id)
values ('123','male','1')

-- test the student gendar check constraint
insert into testing_student
(student_no, student_gendar, class_id)
values ('10001','xmale','1')

-- test the class id check foreign constraint
insert into testing_student
(student_no, student_gendar, class_id)
values ('10001','male','3')

-- testing the student no check unique constraint
--
(1)insert into one valid record
insert into testing_student
(student_no, student_gendar, class_id)
values ('10001','male','1')
--(2)insert into one same student no record
insert into testing_student
(student_no, student_gendar, class_id)
values ('10001','female','1')