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.
總結
以上是生活随笔為你收集整理的Oracle 12C 新特性之级联truncate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4.AngularJS四大特征之二: 双
- 下一篇: ava RMI 框架(远程方法调用)