首页 > 数据库技术 > 详细

数据库的约束

时间:2017-10-01 11:05:16      阅读:235      评论:0      收藏:0      [点我收藏+]

 

约束 描述 实现
主键(primary key)

唯一、非空

定义列时:id int primary key / id int constraint pk_id primary key

定义表之后:alter table student add constraint pk_stu primary key(id,name) 

(必须有id,name的非空限制)

唯一(unique)

唯一、可为空

 定义列时:name varchar(255) unique/name varchar(255) constraint uk_stu unique

定义表之后:alter table student add constraint uk_name unique(name)

外键(foreign key)

依赖关系

 定义列时:te_id int references teacher(id)    or
alter table student
drop constraint fk_stu

定义表之后:

alter table student 
add constraint fk_stu foreign key(te_id)
references teacher(id)

 

默认值(default)

提供默认值

 定义表时:
birthday date default(1900-1-1)   or...

 定义表后:

alter table userInfo
add constraint df_user_birthday default(1900-1-1) for birthday

 

检查(check)

检查约束

 定义表时:
sex char(2) check(sex=male or sex=female) or ...

 定义表后:

alter table userInfo
add constraint cj_user_sex check(sex=male or sex=female)

 

数据库的约束

原文:http://www.cnblogs.com/zhuxiang1633/p/7616743.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!