`
slbszq
  • 浏览: 264322 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

Oracle中的约束条件

阅读更多

 Oracle中的约束条件

 

1.主键约束primary key [pk]不能重复,不能为null

添加代码:constraint constraint_name primary key (column)

 

2.外键约束foreign key [fk]可以为null

constraint constraint_name foreign key (column) references table_name(column);

 

3.设置唯一约束unique [uk])不能重复的,但是可以为null

创建表时,最后添加如下代码:

constraint constraint_name unique (column)

 

4.非空约束not null [nn] 只能定义在列级定义,定义表后添加使用modify关键字

 

5.核查约束check [ck]

constraint constraint_name check(condition)

 

6.查询核查约束

select constraint_name,constraint_type,search_condition

from user_constraints where table_name='emp';

 

7.增加约束条件(其中not null必须使用modify,不能使用add)

alter table tablename add [constraint constraintname] type (column)

eg:alter table depart

add constraint depart_empid_fk

foreign key(empid) references emp(empid);

 

8.启动,关闭约束条件

启动:alter table depart

enable constraint depart_empid_fk;

 

关闭:alter table depart

disable constraint depart_empid_fk;

 

9.删除约束条件

alter table depart

drop constraint depart_empid_fk;

10.重建约束条件(以主外键为例)

       alter table student_yin add

           constraint stu_y_mid_fkey foreign key(mid) references mid_yin(mid_no)

           on delete set null;

           --删除主表(mid_yin)的记录时 子表中依赖该记录的记录被置空

      

       alter table student_yin add

           constraint stu_y_mid_fkey foreign key(mid) references mid_yin(mid_no)

           on delete cascade;

 

           --当删除主表(mid_yin)的记录时 子表中依赖该记录的记录被同时删除

11.表级约束和列级约束

列级定义在定义列的同时定义约束

表级定义是指在定义了所有列后,再定义约束

注意: not null约束只能在列级上定义。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics