Oracle數(shù)據(jù)庫中外鍵的相關(guān)操作整理
racle使用外鍵來限制子表中參考的字段值,要求子表中的數(shù)據(jù)必須在主表中存在。當(dāng)主表的記錄發(fā)生變化時(shí)導(dǎo)致外鍵參考唯一約束值發(fā)生了變化時(shí),Oracle指定了三種動(dòng)作:默認(rèn)值(類似于restrict)、delete cascade和delete set null。(
1.創(chuàng)建父表并初始化數(shù)據(jù)
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.創(chuàng)建三種類型的子表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.確認(rèn)主表和子表中的數(shù)據(jù)
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.嘗試對具有默認(rèn)類型外鍵參照的主表記錄進(jìn)行刪除
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類型外鍵參照的主表記錄進(jìn)行刪除
SQL> delete from T_PARENT where parent_id = 2; 1 row deleted. SQL> select * from T_CHILD2; no rows selected
級聯(lián)刪除成功
6.嘗試對具有delete set null類型外鍵參照的主表記錄進(jìn)行刪除
SQL> delete from T_PARENT where parent_id = 3; 1 row deleted. SQL> select * from T_CHILD3; CHILD2_ID PARENT_ID ---------- ---------- 3
主表記錄可以完成刪除,子表中對應(yīng)的內(nèi)容被設(shè)置為NULL。
相關(guān)文章
oracle 9i使用閃回查詢恢復(fù)數(shù)據(jù)庫誤刪問題
本篇文章給大家介紹在oracle 9i中使用閃回查詢恢復(fù)數(shù)據(jù)庫誤刪問題,涉及到數(shù)據(jù)庫增刪改查的基本操作,對oracle數(shù)據(jù)庫閃回查詢感興趣的朋友可以一起學(xué)習(xí)下本篇文章2015-10-10Oracle 數(shù)據(jù)庫針對表主鍵列并發(fā)導(dǎo)致行級鎖簡單演示
本文簡單演示針對表主鍵并發(fā)導(dǎo)致的行級鎖,鎖的產(chǎn)生是因?yàn)椴l(fā)。沒有并發(fā),就沒有鎖。并發(fā)的產(chǎn)生是因?yàn)橄到y(tǒng)需要,系統(tǒng)需要是因?yàn)橛脩粜枰?,感興趣的你可以參考下哈,希望可以幫助到你2013-03-03ORACLE DATAGUARD中手工處理日志v$archive_GAP的方法
從9i以后,oracle dataguard 備庫一般都不需要手工處理丟失的日志,F(xiàn)AL自動(dòng)會(huì)幫我們處理,本文主要通過個(gè)案例來講下手工處理丟失的日志的方法。2014-08-08oracle數(shù)據(jù)庫的基本使用教程(建表,操作表等)
這篇文章主要給大家介紹了關(guān)于oracle數(shù)據(jù)庫的基本使用(建表,操作表等)的相關(guān)資料,包含了Oracle創(chuàng)建表(create table as)使用方法、操作技巧、實(shí)例演示和注意事項(xiàng),需要的朋友可以參考下2024-01-01oracle 12c創(chuàng)建可插拔數(shù)據(jù)庫(PDB)與用戶詳解
Oracle12c 中,增加了可插接數(shù)據(jù)庫的概念,即PDB,允許一個(gè)數(shù)據(jù)庫容器(CDB)承載多個(gè)可插拔數(shù)據(jù)庫(PDB)。下面這篇文章主要給大家介紹了利用oracle 12c創(chuàng)建可插拔數(shù)據(jù)庫(PDB)與用戶的相關(guān)資料,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02ORA-00947:Not enough values (沒有足夠的值)的深入分析
本篇文章是對ORA-00947:Not enough values (沒有足夠的值)的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Oracle查看表結(jié)構(gòu)的幾種方法示例代碼
本文通過示例代碼給大家介紹了oracle查看表結(jié)構(gòu)的幾種方式,感興趣的朋友參考下吧2017-07-07Orcale權(quán)限、角色查看創(chuàng)建方法
查看當(dāng)前用戶擁有的系統(tǒng)權(quán)限、創(chuàng)建用戶、授予擁有會(huì)話的權(quán)限、授予無空間限制的權(quán)限等等,感興趣的朋友可以參考下哈,希望對你有所幫助2013-05-05