久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

怎么理解oracle外鍵約束

180次閱讀
沒有評論

共計(jì) 2855 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。

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

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

子表操作會(huì)遇到的報(bào)錯(cuò)
不能修改值為父表不存在的記錄
不能插入父表不存在的記錄
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;– 報(bào)錯(cuò) ORA-02291: 違反完整約束條件 (HR.HID_PK) – 未找到父項(xiàng)關(guān)鍵字
insert into test2 values(2, 100 – 報(bào)錯(cuò) ORA-02291: 違反完整約束條件 (HR.HID_PK) – 未找到父項(xiàng)關(guān)鍵字
drop table test2;
drop table test1;

父表操作遇到的報(bào)錯(cuò)
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;– 報(bào)錯(cuò) ORA-02292: 違反完整約束條件 (HR.HID_PK) – 已找到子記錄
truncate table test1;– 報(bào)錯(cuò) ORA-02266: 表中的唯一 / 主鍵被啟用的外鍵引用
drop table test1;– 報(bào)錯(cuò) ORA-02449: 表中的唯一 / 主鍵被外鍵引用
update test1 set hid=2 where hid=1;– 報(bào)錯(cuò) 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;– 報(bào)錯(cuò) 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;– 報(bào)錯(cuò) ORA-02266: 表中的唯一 / 主鍵被啟用的外鍵引用

delete 報(bào)錯(cuò)的解決方法
解決方法 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(保留子表記錄,但是字表對應(yīng)字段值變成 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 報(bào)錯(cuò)的解決方法
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;

采用如下一樣會(huì)報(bào)錯(cuò)
truncate table test2;
truncate table test1;– 繼續(xù)報(bào)錯(cuò) ORA-02266: 表中的唯一 / 主鍵被啟用的外鍵引用

drop 報(bào)錯(cuò)的解決方法
drop table test1 cascade constraints;

drop table test2;
drop table test1;

采用如下一樣會(huì)報(bào)錯(cuò)
alter table test1 disable primary key cascade;
truncate table test2;
drop table test1;

上述就是丸趣 TV 小編為大家分享的怎么理解 oracle 外鍵約束了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道。

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-18發(fā)表,共計(jì)2855字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 金寨县| 门头沟区| 右玉县| 桦川县| 田东县| 微博| 沂源县| 仁化县| 揭西县| 金乡县| 洪湖市| 永兴县| 青冈县| 浑源县| 云南省| 南平市| 中超| 湘潭县| 福安市| 孟津县| 玉田县| 松滋市| 泸州市| 青龙| 梧州市| 云林县| 扎兰屯市| 宜宾县| 民乐县| 五台县| 庐江县| 佳木斯市| 长宁区| 区。| 家居| 通山县| 莱芜市| 囊谦县| 广元市| 普宁市| 静乐县|