怎么理解oracle外键约束

58次阅读
没有评论

共计 2855 个字符,预计需要花费 8 分钟才能阅读完成。

这期内容当中丸趣 TV 小编将会给大家带来有关怎么理解 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;

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

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-17发表,共计2855字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)