溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么理解oracle外鍵約束

發(fā)布時間:2021-11-29 15:52:58 來源:億速云 閱讀:168 作者:柒染 欄目:關系型數(shù)據(jù)庫

這期內容當中小編將會給大家?guī)碛嘘P怎么理解oracle外鍵約束,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

外鍵約束的創(chuàng)建方法
tes1的建表語句為create table test1 (hid number primary key,hname varchar2(10));
1、創(chuàng)建表的同時創(chuàng)建外鍵約束
1.1、列級別
create table test2 (hid1 number(10) REFERENCES test1(hid),hname1 varchar2(10));--系統(tǒng)自動生成約束名
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));--系統(tǒng)自動生成約束名
create table test2 (hid1 number(10) ,hname1 varchar2(10),constraint hid_pk foreign key (hid1) REFERENCES test1(hid));

2、表創(chuàng)建后再創(chuàng)建外鍵約束
ALTER TABLE test2 ADD  FOREIGN KEY (hid1)  REFERENCES test1 (hid);--系統(tǒng)自動生成約束名
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;--繼續(xù)報錯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外鍵約束了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI