国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle數據庫中外鍵的相關操作整理

2024-08-29 13:58:49
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Oracle數據庫中外鍵的相關操作整理,包括對外鍵參照的主表記錄進行刪除的操作方法等,需要的朋友可以參考下
 

racle使用外鍵來限制子表中參考的字段值,要求子表中的數據必須在主表中存在。當主表的記錄發生變化時導致外鍵參考唯一約束值發生了變化時,Oracle指定了三種動作:默認值(類似于restrict)、delete cascade和delete set null。(
1.創建父表并初始化數據

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.創建三種類型的子表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.確認主表和子表中的數據

SQL> select * from T_PARENT;PARENT_ID NAME---------- ----------     1 record1     2 record2     3 record3SQL> select * from T_CHILD1;CHILD1_ID PARENT_ID---------- ----------     1     1SQL> select * from T_CHILD2; CHILD2_ID PARENT_ID---------- ----------     2     2SQL> select * from T_CHILD3; CHILD2_ID PARENT_ID---------- ----------     3     3

 


4.嘗試對具有默認類型外鍵參照的主表記錄進行刪除

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 recordfoundSQL> select * from T_CHILD1; CHILD1_ID PARENT_ID---------- ----------     1     1

在此類型下,不允許刪除操作

5.嘗試對具有delete cascade類型外鍵參照的主表記錄進行刪除

SQL> delete from T_PARENT where parent_id = 2;1 row deleted.SQL> select * from T_CHILD2;no rows selected

級聯刪除成功


6.嘗試對具有delete set null類型外鍵參照的主表記錄進行刪除

SQL> delete from T_PARENT where parent_id = 3;1 row deleted.SQL> select * from T_CHILD3; CHILD2_ID PARENT_ID---------- ----------     3

  主表記錄可以完成刪除,子表中對應的內容被設置為NULL。



注:相關教程知識閱讀請移步到oracle教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沛县| 临城县| 额济纳旗| 巨鹿县| 杂多县| 海兴县| 杭锦旗| 新郑市| 泸水县| 海伦市| 屏边| 崇阳县| 岗巴县| 铜梁县| 肃北| 年辖:市辖区| 鱼台县| 贵阳市| 渭南市| 巩义市| 鄂伦春自治旗| 鄯善县| 平罗县| 调兵山市| 太谷县| 廊坊市| 鄂州市| 阜宁县| 门头沟区| 德钦县| 科技| 汉川市| 利津县| 丹棱县| 康定县| 彭泽县| 邢台县| 县级市| 安远县| 久治县| 正蓝旗|