当前位置:Gxlcms > 数据库问题 > 数据库的约束

数据库的约束

时间:2021-07-01 10:21:17 帮助过:46人阅读

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)

 

数据库的约束

标签:默认   drop   ons   _id   char   数据库   reference   har   ...   

人气教程排行