Oracle数据库中外键的相关操作整理

所属分类: 数据库 / oracle 阅读数: 1955
收藏 0 赞 0 分享

racle使用外键来限制子表中参考的字段值,要求子表中的数据必须在主表中存在。当主表的记录发生变化时导致外键参考唯一约束值发生了变化时,Oracle指定了三种动作:默认值(类似于restrict)、delete cascade和delete set null。(
1.创建父表并初始化数据

SQL> create table t_parent (parent_id int primary key, name varchar2(10));
Table created.
SQL> insert into t_parent values (1,'record1');
1 row created.
SQL> insert into t_parent values (2,'record2');
1 row created.
SQL> insert into t_parent values (3,'record3');
1 row created.
SQL> commit;
Commit complete.

2.创建三种类型的子表t_child1、t_child2和t_child3
(1)no action类别

SQL> create table t_child1 (child1_id int primary key, parent_id int);
Table created.
SQL> alter table t_child1 add constraint FK_t_child1 foreign key (parent_id) references t_parent (parent_id);
Table altered.
SQL> insert into t_child1 values (1,1);
1 row created.
SQL> commit;
Commit complete.

(2)cascade类型

SQL> create table t_child2 (child2_id int primary key, parent_id int);
Table created.
SQL> alter table t_child2 add constraint FK_t_child2 foreign key (parent_id) references t_parent (parent_id) on delete cascade;
Table altered.
SQL> insert into t_child2 values (2,2);
1 row created.
SQL> commit;
Commit complete.

(3)SET NULL类型

SQL> create table t_child3 (child2_id int primary key, parent_id int);
Table created.
SQL> alter table t_child3 add constraint FK_t_child3 foreign key (parent_id) references t_parent (parent_id) on delete set null;
Table altered.
SQL> insert into t_child3 values (3,3);
1 row created.
SQL> commit;
Commit complete.

3.确认主表和子表中的数据

SQL> select * from T_PARENT;
PARENT_ID NAME
---------- ----------
     1 record1
     2 record2
     3 record3
SQL> select * from T_CHILD1;
CHILD1_ID PARENT_ID
---------- ----------
     1     1
SQL> select * from T_CHILD2;
 CHILD2_ID PARENT_ID
---------- ----------
     2     2
SQL> select * from T_CHILD3;
 CHILD2_ID PARENT_ID
---------- ----------
     3     3



4.尝试对具有默认类型外键参照的主表记录进行删除

SQL> delete from T_PARENT where parent_id = 1;
delete from T_PARENT where parent_id = 1
*
ERROR at line 1:
ORA-02292: integrity constraint (HBHE.FK_T_CHILD1) violated - child record
found
SQL> select * from T_CHILD1;


 CHILD1_ID PARENT_ID
---------- ----------
     1     1

在此类型下,不允许删除操作

5.尝试对具有delete cascade类型外键参照的主表记录进行删除

SQL> delete from T_PARENT where parent_id = 2;
1 row deleted.
SQL> select * from T_CHILD2;
no rows selected

级联删除成功


6.尝试对具有delete set null类型外键参照的主表记录进行删除

SQL> delete from T_PARENT where parent_id = 3;


1 row deleted.

SQL> select * from T_CHILD3;


 CHILD2_ID PARENT_ID
---------- ----------
     3

  主表记录可以完成删除,子表中对应的内容被设置为NULL。

更多精彩内容其他人还在看

Oracle轻松取得建表和索引的DDL语句

Oracle轻松取得建表和索引的DDL语句
收藏 0 赞 0 分享

重新编译PLSQL中的无效对象或者指定的对象 的方法

重新编译PLSQL中的无效对象或者指定的对象 的方法
收藏 0 赞 0 分享

在OracleE数据库的字段上建立索引的方法

在OracleE数据库的字段上建立索引的方法
收藏 0 赞 0 分享

oracle下加密存储过程的方法

oracle下加密存储过程的方法
收藏 0 赞 0 分享

浅谈LogMiner的使用方法

浅谈LogMiner的使用方法
收藏 0 赞 0 分享

oracle 下WITH CHECK OPTION用法

oracle 下WITH CHECK OPTION用法
收藏 0 赞 0 分享

在Oracle中向视图中插入数据的方法

在Oracle中向视图中插入数据的方法
收藏 0 赞 0 分享

DBA_2PC_PENDING 介绍

DBA_2PC_PENDING 介绍
收藏 0 赞 0 分享

在Oracle PL/SQL中游标声明中表名动态变化的方法

在Oracle PL/SQL中游标声明中表名动态变化的方法
收藏 0 赞 0 分享

DB2和 Oracle的并发控制(锁)的比较

DB2和 Oracle的并发控制(锁)的比较
收藏 0 赞 0 分享
查看更多