---数据库开发基础---约束
1,约束的分类primary key(PK)主键约束,
foreign key(FK)外键约束,
unique(UK)唯一约束,
check约束,
NOT null约束(NN) -------有时候说的是四种约束,就是除了这个约束外的其他四种主键约束,
2.约束的创建语法
1>.主键约束
create table t(x int,y int,primary key(x));
create table t(x int constraint pk_t primary key,y int);
2>.外键约束
create table t(z int constraint pk_t primary key,x int constraints fk_t references t);
create table t(z int,x int,primary key(z),foreign key(x) references t);
3>. unique 唯一键约束
create table t(z int constraint pk_t unique);
create table t(z int ,unique(z));
4>.check 约束
create table t1(z int ,age int check(age>0 and age<150));
create table t2(z int ,age int,check(age>0 and age<150));
5>.null约束
create table t3(z int ,age int not null);
6>组合键约束中的组合作为一个整体对约束对象进行一起约束
---组合主键同时不满足唯一性时才拒绝,两者均各不为空
create table t(x int,y int,primary key(x,y));
insert into t values(1,1);-----YES
insert into t values(1,NULL);--NO
insert into t values(NULL,1);--NO
insert into t values(1,2);-----YES
insert into t values(NULL,NULL);--NO
对于null,oracle将其默认为unknow类型。
3.约束的修改语法
alter table t_emp add constraint fk_dept foreign key(deptno) references t_dept(deptno);---增加约束
alter table t_emp add constraint fk_deptno1 foreign key (deptno) references t_dept(deptno) on delete cascade;---增加可级联删除的外键约束
alter table t_emp drop primary key; ---------------删除约束
alter table t_emp disable constraint fk_deptno1;-----使失效
alter table t_emp enable constraint fk_deptno1;------使生效
alter table t_ck1 modify id constraint t_nk not null;-----已经存在的约束进行修改
alter table t_null modify id constraint t_nk not null;-----已经存在的约束进行修改
在进行约束修改或删除过程中存在外键子表与父表之间的级联关系
alter table T_ORD_ORDER_CHANNEL drop constraint PK_T_ORD_ORDER_CHANNEL CASCADE;----级联删除
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30018455/viewspace-1389498/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/30018455/viewspace-1389498/