怎么理解oracle外键约束

网友投稿 278 2023-12-14

怎么理解oracle外键约束

这期内容当中小编将会给大家带来有关怎么理解oracle外键约束,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

怎么理解oracle外键约束

外键约束的创建方法

tes1的建表语句为create table test1 (hid number primary key,hname varchar2(10));

1、创建表的同时创建外键约束

1.1、列级别

create table test2 (hid1 number(10) REFERENCES test1(hid),hname1 varchar2(10));--系统自动生成约束名

create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));

1.2、表级别

create table test2 (hid1 number(10) ,hname1 varchar2(10),foreign key (hid1) REFERENCES test1(hid));--系统自动生成约束名

create table test2 (hid1 number(10) ,hname1 varchar2(10),constraint hid_pk foreign key (hid1) REFERENCES test1(hid));

2、表创建后再创建外键约束

ALTER TABLE test2 ADD  FOREIGN KEY (hid1)  REFERENCES test1 (hid);--系统自动生成约束名

ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid);

子表操作会遇到的报错

不能修改值为父表不存在的记录

不能插入父表不存在的记录

create table test1 (hid number primary key,hname varchar2(10));

create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));

insert into test1 values(1,1);

insert into test2 values(1,100);

update test2 set hid1=2 where hid1=1;--报错ORA-02291: 违反完整约束条件 (HR.HID_PK) - 未找到父项关键字

insert into test2 values(2,100);--报错ORA-02291: 违反完整约束条件 (HR.HID_PK) - 未找到父项关键字

drop table test2;

drop table test1;

父表操作遇到的报错

create table test1 (hid number primary key,hname varchar2(10));

create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));

insert into test1 values(1,1);

insert into test2 values(1,100);

delete from test1;--报错ORA-02292: 违反完整约束条件 (HR.HID_PK) - 已找到子记录

truncate table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用

drop table test1;--报错ORA-02449: 表中的唯一/主键被外键引用

update test1 set hid=2 where hid=1;--报错ORA-02292: 违反完整约束条件 (HR.HID_PK) - 已找到子记录

create table test1 (hid number primary key,hname varchar2(10));

create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));

insert into test1 values(1,1);

truncate table test1;

drop table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用

create table test1 (hid number primary key,hname varchar2(10));

create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));

drop table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用

delete报错的解决方法

解决方法1

delete from test2;

delete from test1;

解决方法2(不保留子表记录)

alter table test2 drop constraint hid_pk;

ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid) ON DELETE CASCADE;

delete from test1;

解决方法3(保留子表记录,但是字表对应字段值变成null,如下test2的hid1为null了)

alter table test2 drop constraint hid_pk;

ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid) ON DELETE SET NULL;

delete from test1;

truncate报错的解决方法

drop table test2;

truncate table test1;

alter table test1 disable primary key cascade;

truncate table test1;

alter table test1 disable primary key cascade;

truncate table test2;

truncate table test1;

采用如下一样会报错

truncate table test2;

truncate table test1;--继续报错ORA-02266: 表中的唯一/主键被启用的外键引用

drop报错的解决方法

drop table test1 cascade constraints;

drop table test2;

drop table test1;

采用如下一样会报错

alter table test1 disable primary key cascade;

truncate table test2;

drop table test1;

上述就是小编为大家分享的怎么理解oracle外键约束了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:oracle异步IO中的filesystemio_options参数是怎样的
下一篇:Oracle 12c集群启动故障该怎么办
相关文章

 发表评论

暂时没有评论,来抢沙发吧~