父表修改與外來鍵的關係(主鍵DML與外來鍵的關係)
Data Integrity學習(二)
父表修改與外來鍵的關係(主鍵DML與外來鍵的關係)【這張表就是本文重點】
Issued Against Parent Table |
Issued Against Child Table |
|
Always OK if the parent key value is unique |
OK only if the foreign key value exists in the parent key or is partially or all null |
|
UPDATE NO ACTION |
Allowed if the statement does not leave any rows in the child table without a referenced parent key value |
Allowed if the new foreign key value still references a referenced key value |
DELETE NO ACTION |
Allowed if no rows in the child table reference the parent key value |
Always OK |
DELETE CASCADE |
Always OK |
Always OK |
DELETE SET NULL |
Always OK |
Always OK |
主鍵DML與外來鍵的關係:
1、INSERT:主鍵可以匹配數值,允許外來鍵列插入
2、UPDATE NO ACTION:主鍵更新沒有匹配的資料是允許的,更新被匹配的資料不允許
3、DELETE NO ACTION:主鍵刪除沒有被匹配的資料時允許的,刪除被匹配的資料不允許
4、DELETE CASCADE:使用了DELETE CASCADE,刪除主鍵列資料,會級聯刪除外來鍵列相應的資料
5、DELETE SET NULL:使用了DELETE SET NULL,刪除主鍵列資料,會把外來鍵列相應的資料 DELETE SET NULL
實驗:
Parent table : t3 primary key : t3.id
Child table: t3_fk foreign key : t3_fk.id
1、DML Statement INSERT
doudou@TEST> insert into t3 values (2,'3');
1 row created.
doudou@TEST> commit;
Commit complete
doudou@TEST> insert into t3_fk values (2,'fk_2');
1 row created.
doudou@TEST> commit;
Commit complete
【外來鍵列插入的資料在父表主鍵列能相應的匹配到,允許外來鍵列資料插入】
2、DML Statement UPDATE NO ACTION
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
1 1
2 3
3 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
1 pk_1
pk_2
2 fk_2
doudou@TEST> update t3 set id=4 where id=3;
1 row updated.
doudou@TEST> update t3 set id=5 where id=2;
update t3 set id=5 where id=2
*
ERROR at line 1:
ORA-02292: integrity constraint (DOUDOU.SYS_C007471) violated - child record found
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
1 1
2 3
4 pk_3
doudou@TEST> update t3_fk set id=3 where id=2;
update t3_fk set id=3 where id=2
*
ERROR at line 1:
ORA-02291: integrity constraint (DOUDOU.SYS_C007471) violated - parent key not found
doudou@TEST> update t3_fk set id=4 where id=2;
1 row updated.
【父表update:update 主鍵列沒有被外來鍵相關聯的資料,是可以更新的。
子表update:update 外來鍵列的資料,更改後的資料在主鍵列可以找到相應的匹配是允許修改的。找不到不允許修改】
3、DML Statement DELETE NO ACTION
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
1 1
2 3
4 pk_3
3 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
1 pk_1
pk_2
4 fk_2
doudou@TEST> delete t3 where id=3;
1 row deleted.
doudou@TEST> delete t3 where id=4;
delete t3 where id=4
*
ERROR at line 1:
ORA-02292: integrity constraint (DOUDOU.SYS_C007471) violated - child record found
doudou@TEST> delete t3_fk where id=4;
1 row deleted.
【父表delete:delete主鍵列沒有被外來鍵列相匹配的資料,是允許的。已經被匹配的不允許刪除,可以先刪除子表匹配的資料,然後刪除主鍵列被匹配的資料。
子表delete:delete外來鍵列的資料是被允許的。】
4、DML Statement DELETE CASCADE
doudou@TEST> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME from user_constraints where table_name ='T3_FK';
CONSTRAINT_NAME CO TABLE_NAME
------------------------------ -- ------------------------------
SYS_C007471 R T3_FK
doudou@TEST> alter table t3_fk drop constraints SYS_C007471;
Table altered.
doudou@TEST> alter table t3_fk add constraints t3_fk_id foreign key (id) references t3 (id) on delete cascade; --delete cascade 語法
Table altered.
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
1 1
2 3
4 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
1 pk_1
pk_2
doudou@TEST> delete t3 where id=1;
1 row deleted.
doudou@TEST> commit;
Commit complete.
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
2 3
4 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
pk_2
doudou@TEST> delete t3_fk;
1 row deleted.
【外來鍵約束使用delete cascade :刪除主鍵列的資料會級聯把外來鍵相關聯的資料delete】
5、DML Statement DELETE SET NULL
doudou@TEST> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME from user_constraints where table_name ='T3_FK';
CONSTRAINT_NAME CO TABLE_NAME
------------------------------ -- ------------------------------
T3_FK_ID R T3_FK
doudou@TEST> alter table t3_fk drop constraints t3_fk_id;
Table altered.
doudou@TEST> alter table t3_fk add constraint t3_fk_id foreign key (id) references t3 (id) on delete set null; --delete set null語法
Table altered.
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
2 3
4 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
2 fk_2
doudou@TEST> delete t3 where id=2;
1 row deleted.
doudou@TEST> commit;
Commit complete.
doudou@TEST> select * from t3;
ID NAME
---------- ----------------------------------------
4 pk_3
doudou@TEST> select * from t3_fk;
ID NAME
---------- ----------------------------------------
fk_2
【外來鍵約束使用delete set null :刪除主鍵的資料會使外來鍵列相應的資料delete set null】
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26442936/viewspace-753900/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Django(15)外來鍵和表關係Django
- 關於主外來鍵關係DML父表和DML子表加鎖方式
- Oracle查詢表的外來鍵引用關係Oracle
- 主鍵與主鍵索引的關係索引
- Oracle 查詢表與表之間的 主外來鍵關係Oracle
- 求主外來鍵的關係
- Django中ORM外來鍵和表的關係(Django程式設計-4)DjangoORM程式設計
- indexedDB 內鍵與外來鍵Index
- 檢視主外來鍵關係
- 關於 oracle 外來鍵引用 與 goldengateOracleGo
- 開發CMP關係的時候,是建立主鍵表CMP關係然後指向外來鍵表CMP,還是反之?
- 資料庫的主外來鍵關係資料庫
- ORACLE查詢表之間的主外來鍵關係Oracle
- mysql主外來鍵依賴關係MySql
- SQL的主鍵和外來鍵約束SQL
- 新的主鍵和外來鍵的語法
- 求主外來鍵的關係的指令碼(ZT)指令碼
- oracle 主外來鍵關係及實驗Oracle
- MySQL建立資料表並建立主外來鍵關係MySql
- 實現 MongoDB 外來鍵關聯MongoDB
- Oracle根據主鍵查詢外來鍵Oracle
- MYSQL的外來鍵MySql
- 關於外來鍵約束和對應主鍵資訊的查詢指令碼指令碼
- EntityFramework Core 遷移忽略主外來鍵關係Framework
- sqlserver外來鍵SQLServer
- 在已存在的表結構上新增主鍵、外來鍵、聯合主鍵、聯合索引的例子索引
- 批量修改欄位長度,考慮主鍵外來鍵索引的情況【轉】索引
- mysql中的外來鍵MySql
- 查詢一個表的外來鍵
- oracle外來鍵索引解決父表鎖定問題Oracle索引
- MySQL 檢視資料庫中有主外來鍵關係的表資訊MySql資料庫
- 菜鳥學資料庫(四)——超鍵、候選鍵、主鍵、外來鍵資料庫
- 關於外來鍵的理解和實驗步驟
- 查詢(看)表的主鍵、外來鍵、唯一性約束和索引索引
- 外來鍵技術
- 修改外來鍵為validate時需要驗證資料是否符合外來鍵約束
- 多對一(主鍵)關係,create問題
- ORACLE: 查詢(看)表的主鍵、外來鍵、唯一性約束和索引Oracle索引