Oracle外鍵約束的三種刪除行為小結
Oracle外鍵約束的三種刪除行為分別是:默認刪除(No Action)、級聯刪除(Cascade)和置空刪除(Set Null)。這三種行為定義了當主表(父表)中的記錄被刪除時,子表中對應外鍵的處理方式。
1、創(chuàng)建主表及子表并初始化數據
1.1、創(chuàng)建主表t_parent,并插入三條記錄
hr@orcl> create table t_parent (parent_id int primary key, name varchar2(10)); hr@orcl> insert into t_parent values (1,'record1'); hr@orcl> insert into t_parent values (2,'record2'); hr@orcl> insert into t_parent values (3,'record3'); hr@orcl> commit; hr@orcl> select * from T_PARENT; PARENT_ID NAME ---------- ------------------------------ 1 record1 2 record2 3 record3
1.2、創(chuàng)建字表外鍵約束默認刪除no action類型
hr@orcl> create table t_child1 (child1_id int primary key, parent_id int); hr@orcl> alter table t_child1 add constraint FK_t_child1 foreign key (parent_id) references t_parent (parent_id); hr@orcl> insert into t_child1 values (1,1); hr@orcl> commit; hr@orcl> select * from T_CHILD1; CHILD1_ID PARENT_ID ---------- ---------- 1 1
1.3、創(chuàng)建字表外鍵約束級聯刪除(Cascade)
hr@orcl> create table t_child2 (child2_id int primary key, parent_id int); hr@orcl> alter table t_child2 add constraint FK_t_child2 foreign key (parent_id) references t_parent (parent_id) on delete cascade; hr@orcl> insert into t_child2 values (2,2); hr@orcl> commit; hr@orcl> select * from T_CHILD2; CHILD2_ID PARENT_ID ---------- ---------- 2 2
1.4、創(chuàng)建字表外鍵約束置空刪除(Set Null)
hr@orcl> create table t_child3 (child2_id int primary key, parent_id int); hr@orcl> alter table t_child3 add constraint FK_t_child3 foreign key (parent_id) references t_parent (parent_id) on delete set null; hr@orcl> insert into t_child3 values (3,3); hr@orcl> commit; hr@orcl> select * from T_CHILD3; CHILD2_ID PARENT_ID ---------- ---------- 3 3
2、三種刪除行為
2.1、默認刪除(No Action)
行為描述:
- 當在定義外鍵約束時使用No Action關鍵字(或者什么都不加,因為No Action是默認值),如果嘗試刪除主表中被外鍵引用的記錄,Oracle將阻止這一操作,并返回錯誤,因為這將違反外鍵約束的完整性。
示例:
如果嘗試刪除主表T_PARENT
中PARENT_ID
為1的記錄,而子表T_CHILD1
中存在引用該PARENT_ID
的記錄,則刪除操作將失敗,并返回類似ORA-02292: integrity constraint (FK_T_CHILD1) violated - child record found
的錯誤。
hr@orcl> delete from T_PARENT where parent_id = 1; delete from T_PARENT where parent_id = 1 * ERROR at line 1: ORA-02292: integrity constraint (SEC.FK_T_CHILD1) violated - child record found hr@orcl> select * from T_CHILD1; CHILD1_ID PARENT_ID ---------- ---------- 1 1 在此類型下,不允許刪除。
2.2、級聯刪除(Cascade)
行為描述:
- 當在定義外鍵約束時使用Cascade關鍵字,如果主表中被引用的記錄被刪除,那么子表中所有引用該記錄的外鍵也將被自動刪除。
示例:
如果主表T_PARENT
中PARENT_ID
為2的記錄被刪除,并且子表T_CHILD2
中存在引用該PARENT_ID
的記錄,則這些記錄也將被自動刪除,以保持數據的一致性。
hr@orcl> delete from T_PARENT where parent_id = 2; 1 row deleted. hr@orcl> select * from T_CHILD2; no rows selected 成功,級聯刪除成功。
2.3、 置空刪除(Set Null)
行為描述:
- 當在定義外鍵約束時使用Set Null關鍵字,如果主表中被引用的記錄被刪除,那么子表中所有引用該記錄的外鍵將被設置為NULL。注意,這要求子表中的外鍵列允許NULL值。
示例:
如果主表T_PARENT
中PARENT_ID
為3的記錄被刪除,并且子表T_CHILD3
中存在引用該PARENT_ID
的記錄,則這些記錄中的PARENT_ID
將被設置為NULL。
hr@orcl> delete from T_PARENT where parent_id = 3; 1 row deleted. hr@orcl> select * from T_CHILD3; CHILD2_ID PARENT_ID ---------- ---------- 3
主表記錄可以完成刪除,子表中對應的內容被設置為NULL。
3、 總結
以上就是在Oracle數據庫,當主表信息刪除后對應的子表中記錄的三種不同的處理方式,針對具體的應用場合請選擇合適類型。
Oracle外鍵約束的三種刪除行為為數據庫設計提供了靈活性,可以根據實際需求選擇合適的行為來維護數據的完整性和一致性。在實際應用中,應根據數據的依賴關系和業(yè)務邏輯來選擇合適的刪除行為。
以上信息基于Oracle數據庫的外鍵約束行為,并參考了相關的技術文檔和博客文章。需要注意的是,隨著Oracle數據庫版本的更新,某些細節(jié)和語法可能會有所變化,因此建議查閱最新的官方文檔以獲取最準確的信息。
到此這篇關于Oracle外鍵約束的三種刪除行為小結的文章就介紹到這了,更多相關Oracle外鍵約束刪除內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
oracle 11gR2 win64安裝配置教程另附基本操作
這篇文章主要介紹了oracle 11gR2 win64安裝配置教程,另附數據庫基本操作,感興趣的小伙伴們可以參考一下2016-08-08Oracle導出文本文件的三種方法(spool,UTL_FILE,sqluldr2)
這篇文章主要介紹了Oracle導出文本文件的三種方法(spool,UTL_FILE,sqluldr2),需要的朋友可以參考下2023-05-05