sql server添加约束

来源:互联网 发布:无源定位软件 编辑:程序博客网 时间:2024/05/16 05:53

-------添加主键约束(bookid作为主键) 
alter table bookmessage add constraint pk_bookid primary key(bookid) 

-------添加唯一约束 
alter table bookmessage 
add constraint uq_bookid UNIQUE(bookid) 

添加默认约束 
alter table bookmessage 
add constraint df_address DEFAUIT('地址不详') for Address 

--------添加检出约束,要求年龄只能在15---40之间 
alter table readermessage 
add constraint CK_age CHECK(age BETWEEN 15 AND 40) 

-------添加外键约束 
alter table bookmessage 
add constraint fk_bookid 
foreign key(bookid)<外键> references readermessage<表>(readerid)<表中的主键> 

-------删除约束 
alter table 表名 
drop constraint 约束名

原创粉丝点击