【Foreign Key】Oracle外來鍵約束三種刪除行為
Oracle使用外來鍵來限制子表中參考的欄位值,要求子表中的資料必須在主表中存在。當主表的記錄發生變化時導致外來鍵參考唯一約束值發生了變化時,Oracle指定了三種動作:預設值(類似於restrict)、delete cascade和delete set null。
實際體驗一下他們對刪除操作的不同效果。
1.建立主表及子表並簡單初始化幾條資料
1)建立主表t_parent,並初始化三條記錄
sec@ora10g> create table t_parent (parent_id int primary key, name varchar2(10));
sec@ora10g> insert into t_parent values (1,'record1');
sec@ora10g> insert into t_parent values (2,'record2');
sec@ora10g> insert into t_parent values (3,'record3');
sec@ora10g> commit;
2)建立三種型別的子表t_child1、t_child2和t_child3
(1)no action型別
sec@ora10g> create table t_child1 (child1_id int primary key, parent_id int);
sec@ora10g> alter table t_child1 add constraint FK_t_child1 foreign key (parent_id) references t_parent (parent_id);
sec@ora10g> insert into t_child1 values (1,1);
sec@ora10g> commit;
(1)cascade型別
sec@ora10g> create table t_child2 (child2_id int primary key, parent_id int);
sec@ora10g> alter table t_child2 add constraint FK_t_child2 foreign key (parent_id) references t_parent (parent_id) on delete cascade;
sec@ora10g> insert into t_child2 values (2,2);
sec@ora10g> commit;
(1)set null型別
sec@ora10g> create table t_child3 (child2_id int primary key, parent_id int);
sec@ora10g> alter table t_child3 add constraint FK_t_child3 foreign key (parent_id) references t_parent (parent_id) on delete set null;
sec@ora10g> insert into t_child3 values (3,3);
sec@ora10g> commit;
2.確認主表和子表中的資料
sec@ora10g> select * from T_PARENT;
PARENT_ID NAME
---------- ------------------------------
1 record1
2 record2
3 record3
sec@ora10g> select * from T_CHILD1;
CHILD1_ID PARENT_ID
---------- ----------
1 1
sec@ora10g> select * from T_CHILD2;
CHILD2_ID PARENT_ID
---------- ----------
2 2
sec@ora10g> select * from T_CHILD3;
CHILD2_ID PARENT_ID
---------- ----------
3 3
3.嘗試對具有預設型別外來鍵參照的主表記錄進行刪除
sec@ora10g> 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
sec@ora10g> select * from T_CHILD1;
CHILD1_ID PARENT_ID
---------- ----------
1 1
在此型別下,不允許刪除。
4.嘗試對具有delete cascade型別外來鍵參照的主表記錄進行刪除
sec@ora10g> delete from T_PARENT where parent_id = 2;
1 row deleted.
sec@ora10g> select * from T_CHILD2;
no rows selected
成功,級聯刪除成功。
5.嘗試對具有delete set null型別外來鍵參照的主表記錄進行刪除
sec@ora10g> delete from T_PARENT where parent_id = 3;
1 row deleted.
sec@ora10g> select * from T_CHILD3;
CHILD2_ID PARENT_ID
---------- ----------
3
主表記錄可以完成刪除,子表中對應的內容被設定為NULL。
6.小結
以上就是在Oracle資料庫,當主表資訊刪除後對應的子表中記錄的三種不同的處理方式,針對具體的應用場合請選擇合適型別。
Good luck.
secooler
10.03.21
-- The End --
實際體驗一下他們對刪除操作的不同效果。
1.建立主表及子表並簡單初始化幾條資料
1)建立主表t_parent,並初始化三條記錄
sec@ora10g> create table t_parent (parent_id int primary key, name varchar2(10));
sec@ora10g> insert into t_parent values (1,'record1');
sec@ora10g> insert into t_parent values (2,'record2');
sec@ora10g> insert into t_parent values (3,'record3');
sec@ora10g> commit;
2)建立三種型別的子表t_child1、t_child2和t_child3
(1)no action型別
sec@ora10g> create table t_child1 (child1_id int primary key, parent_id int);
sec@ora10g> alter table t_child1 add constraint FK_t_child1 foreign key (parent_id) references t_parent (parent_id);
sec@ora10g> insert into t_child1 values (1,1);
sec@ora10g> commit;
(1)cascade型別
sec@ora10g> create table t_child2 (child2_id int primary key, parent_id int);
sec@ora10g> alter table t_child2 add constraint FK_t_child2 foreign key (parent_id) references t_parent (parent_id) on delete cascade;
sec@ora10g> insert into t_child2 values (2,2);
sec@ora10g> commit;
(1)set null型別
sec@ora10g> create table t_child3 (child2_id int primary key, parent_id int);
sec@ora10g> alter table t_child3 add constraint FK_t_child3 foreign key (parent_id) references t_parent (parent_id) on delete set null;
sec@ora10g> insert into t_child3 values (3,3);
sec@ora10g> commit;
2.確認主表和子表中的資料
sec@ora10g> select * from T_PARENT;
PARENT_ID NAME
---------- ------------------------------
1 record1
2 record2
3 record3
sec@ora10g> select * from T_CHILD1;
CHILD1_ID PARENT_ID
---------- ----------
1 1
sec@ora10g> select * from T_CHILD2;
CHILD2_ID PARENT_ID
---------- ----------
2 2
sec@ora10g> select * from T_CHILD3;
CHILD2_ID PARENT_ID
---------- ----------
3 3
3.嘗試對具有預設型別外來鍵參照的主表記錄進行刪除
sec@ora10g> 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
sec@ora10g> select * from T_CHILD1;
CHILD1_ID PARENT_ID
---------- ----------
1 1
在此型別下,不允許刪除。
4.嘗試對具有delete cascade型別外來鍵參照的主表記錄進行刪除
sec@ora10g> delete from T_PARENT where parent_id = 2;
1 row deleted.
sec@ora10g> select * from T_CHILD2;
no rows selected
成功,級聯刪除成功。
5.嘗試對具有delete set null型別外來鍵參照的主表記錄進行刪除
sec@ora10g> delete from T_PARENT where parent_id = 3;
1 row deleted.
sec@ora10g> select * from T_CHILD3;
CHILD2_ID PARENT_ID
---------- ----------
3
主表記錄可以完成刪除,子表中對應的內容被設定為NULL。
6.小結
以上就是在Oracle資料庫,當主表資訊刪除後對應的子表中記錄的三種不同的處理方式,針對具體的應用場合請選擇合適型別。
Good luck.
secooler
10.03.21
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29067253/viewspace-2121353/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 聊聊Oracle外來鍵約束(Foreign Key)的幾個操作選項Oracle
- 【SQL】15 SQL 約束(Constraints)、NOT NULL 約束、UNIQUE 約束、PRIMARY KEY 約束、FOREIGN KEY 約束、CHECK 約束、DEFAULT約束SQLAINull
- oracle外來鍵約束的總結Oracle
- 批量刪除MSSQL 中主外來鍵約束SQL
- 外來鍵約束
- Javaweb-約束-外來鍵約束JavaWeb
- Mysql-基本練習(06-唯一約束、外來鍵約束、新增、刪除單列)MySql
- 生成指令碼,得到所有表的外來鍵約束,然後刪除並重建這些約束指令碼
- 建立外來鍵時報 Cannot add foreign key constraint 解決方法AI
- 約束外來鍵筆記筆記
- 關於外來鍵約束
- 教你mysql如何增加外來鍵約束MySql
- AppBoxFuture(七): 分散式外來鍵約束APP分散式
- 在 SQL Server 中 你可以使用以下查詢來找到引用 的 FOREIGN KEY 約束SQLServer
- mysql不能新增外來鍵約束怎麼辦MySql
- SQL外來鍵約束的含義及建立SQL
- [轉帖]Redis中刪除過期Key的三種策略Redis
- 【YashanDB資料庫】自關聯外來鍵插入資料時報錯:YAS-02033 foreign key constraint violated parent key not found資料庫AI
- mysql~資料完整性考慮~外來鍵約束MySql
- 淺談Oracle 主外來鍵刪除語法格式Oracle
- [資料庫]資料庫中為什麼不推薦使用外來鍵約束資料庫
- Redis刪除大KeyRedis
- cad刪除快捷鍵命令 cad刪除有幾種方式
- 資料遷移無法新增外來鍵約束,錯誤程式碼 1215
- 外來鍵的變種
- Sqlserver中所有約束的型別,建立、修改與刪除SQLServer型別
- MariaDB資料庫的外來鍵約束例項程式碼介紹詳解資料庫
- 主鍵約束、唯一約束和唯一索引索引
- oracle 註釋和約束Oracle
- 刪除大key時要小心
- 【MySQL】MySQL進階(外來鍵約束、多表查詢、檢視、備份與恢復)MySql
- ORACLE批量刪除無主鍵重複資料Oracle
- 10、Oracle中的約 束constraintOracleAI
- 《資料庫系統概論》5.0——常見約束 大學生學習筆記(主鍵 外來鍵)資料庫筆記
- [20180423]關於閃回表與主外來鍵約束.txt
- 為什麼不用外來鍵
- Mysql關於資料庫是否應該使用外來鍵約束詳解說明創磅MySql資料庫
- Python 中刪除列表元素的三種方法Python
- oracle刪除日誌Oracle