Oracle 12C 新特性之級聯truncate
12c之前的版本中,在子表引用一個主表以及子表存在記錄的情況下,是不提供截斷此主表操作的。而在 12c 中的帶有 CASCADE 操作的TRUNCATE TABLE 可以截斷主表中的記錄,並自動對子表進行遞迴截斷,並作為 DELETE ON CASCADE 服從外來鍵引用。由於這是應用到所有子表的,所以對遞迴層級的數量是沒有 CAP 的,可以是孫子表或是重孫子表等等。這一增強擯棄了要在截斷一個主表之前先截斷所有子表記錄的前提。新的 CASCADE 語句同樣也可以應用到表分割槽和子表分割槽等。
SQL>
create table parent(id number primary key);
create table child(cid number primary key,id number);
insert into parent values(1);
insert into parent values(2);
insert into child values(1,1);
insert into child values(2,1);
insert into child values(3,2);
commit;
SQL> select a.id,b.cid,b.id from parent a, child b where a.id=b.id;
ID CID ID
---------- ---------- ----------
1 1 1
1 2 1
2 3 2
--新增約束,不附上 on delete cascade
SQL> alter table child add constraint fk_parent_child foreign key(id) references parent(id);
SQL> truncate table parent cascade;
ERROR at line 1:
ORA-14705: unique or primary keys referenced by enabled foreign keys in table
"C##ANDY"."CHILD"
-- 檢視錶約束
SQL>
col CONSTRAINT_NAME for a25;
col TABLE_NAME for a25;
col COLUMN_NAME for a25;
select CONSTRAINT_NAME,TABLE_NAME, COLUMN_NAME from user_cons_columns where TABLE_NAME='CHILD';
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
------------------------- ------------------------- -------------------------
SYS_C007353 CHILD CID
FK_PARENT_CHILD CHILD ID
-- 刪除約束
SQL> alter table child drop constraint FK_PARENT_CHILD;
Table altered.
-- 新增約束,並附上 on delete cascade
SQL> alter table child add constraint fk2_parent_child foreign key(id) references parent(id) on delete cascade;
Table altered.
SQL> truncate table parent cascade;
Table truncated.
SQL>
create table parent(id number primary key);
create table child(cid number primary key,id number);
insert into parent values(1);
insert into parent values(2);
insert into child values(1,1);
insert into child values(2,1);
insert into child values(3,2);
commit;
SQL> select a.id,b.cid,b.id from parent a, child b where a.id=b.id;
ID CID ID
---------- ---------- ----------
1 1 1
1 2 1
2 3 2
--新增約束,不附上 on delete cascade
SQL> alter table child add constraint fk_parent_child foreign key(id) references parent(id);
SQL> truncate table parent cascade;
ERROR at line 1:
ORA-14705: unique or primary keys referenced by enabled foreign keys in table
"C##ANDY"."CHILD"
-- 檢視錶約束
SQL>
col CONSTRAINT_NAME for a25;
col TABLE_NAME for a25;
col COLUMN_NAME for a25;
select CONSTRAINT_NAME,TABLE_NAME, COLUMN_NAME from user_cons_columns where TABLE_NAME='CHILD';
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
------------------------- ------------------------- -------------------------
SYS_C007353 CHILD CID
FK_PARENT_CHILD CHILD ID
-- 刪除約束
SQL> alter table child drop constraint FK_PARENT_CHILD;
Table altered.
-- 新增約束,並附上 on delete cascade
SQL> alter table child add constraint fk2_parent_child foreign key(id) references parent(id) on delete cascade;
Table altered.
SQL> truncate table parent cascade;
Table truncated.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2138972/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 12c新特性之Sequence的Session特性OracleSession
- Oracle 12c 新特性之 temp undoOracle
- Oracle 12c 新特性之 PDB 級別閃回資料庫Oracle資料庫
- Oracle 12C 新特性之 恢復表Oracle
- Oracle 12c新特性Oracle
- oracle 12c 新特性之不可見欄位Oracle
- oracle 12C 新特性之臨時undo控制Oracle
- Oracle 12c 新特性(四)Oracle
- Oracle 12c 新特性(三)Oracle
- Oracle 12c 新特性(二)Oracle
- Oracle 12c 新特性(一)Oracle
- 【12c】12c RMAN新特性之recover table(表級別恢復)
- Oracle 12C新特性-History命令Oracle
- Oracle 12c 兩個新特性Oracle
- Oracle 12C新特性In-MemoryOracle
- Oracle GoldenGate 12c 新特性OracleGo
- Oracle 12c新特性之——TABLE ACCESS BY INDEX ROWID BATCHEDOracleIndexBAT
- Oracle 12c新特性--ASMFD(ASM Filter Driver)特性OracleASMFilter
- Oracle12c分割槽新特性之TRUNCATEPARTITION和EXCHANGE PARTITION級聯功能Oracle
- Oracle 12c DG新特性Far SyncOracle
- ORACLE 12C新特性——CDB與PDBOracle
- 12c RMAN新特性之Recover Table
- Oracle 12c 新特性之臨時Undo--temp_undo_enabledOracle
- Oracle 12c新特性之:APPROX_COUNT_DISTINCT 函式OracleAPP函式
- Oracle goldengate 12c 新特性之完美支援Active Data GuardOracleGo
- Oracle 12C 新特性之表分割槽部分索引(Partial Indexes)Oracle索引Index
- Oracle 12C 新特性之 sqlplus檢視History命令OracleSQL
- Oracle 12C 新特性之資料檔案線上遷移Oracle
- oracle 12c 支援級聯 standby dataguardOracle
- Oracle 12C新特性-RMAN恢復表Oracle
- 12c新特性-Oracle Sharding簡介Oracle
- Oracle 12c新特性 - Hybrid histogram 3OracleHistogram
- Oracle 12c新特性 - Hybrid histogram 2OracleHistogram
- Oracle 12c新特性 - Hybrid histogram 1OracleHistogram
- Oracle 12c新特性 - Top frequency histogram 3OracleHistogram
- Oracle 12c新特性 - Top frequency histogram 2OracleHistogram
- Oracle 12c新特性 - Top frequency histogram 1OracleHistogram
- Oracle 12c新特性之:使用高階索引壓縮建立索引Oracle索引