TRUNCATE TABLE ... CASCADE in Oracle Database 12c Release 1 (12.1)
This article discusses the TRUNCATE TABLE ... CASCADE functionality in Oracle Database 12c Release 1.
oracle 12cconfigurationintermediate
by OracleDba
13 views
This article discusses the TRUNCATE TABLE ... CASCADE functionality in Oracle Database 12c Release 1.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
-- Create the test tables using ON DELETE CASCADE
drop table t3 purge;
drop table t2 purge;
drop table t1 purge;
create table t1 (
id number,
description varchar2(50),
constraint t1_pk primary key (id)
);
create table t2 (
id number,
t1_id number,
description varchar2(50),
constraint t2_pk primary key (id),
constraint t2_t1_fk foreign key (t1_id) references t1 (id) on delete cascade
);
create table t3 (
id number,
t2_id number,
description varchar2(50),
constraint t3_pk primary key (id),
constraint t3_t2_fk foreign key (t2_id) references t2 (id) on delete cascade
);
-- Insert a data into each table.
begin
insert into t1 values (1, 't1 ONE');
insert into t2 values (1, 1, 't2 ONE');
insert into t2 values (2, null, 't2 TWO');
insert into t3 values (1, 1, 't3 ONE');
insert into t3 values (2, null, 't3 TWO');
commit;
end;
/
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
1 2 2
SQL>1234567891011121314
delete from t1;
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
0 1 1
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
rollback;
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
1 2 2
SQL>
truncate table t1;
truncate table t1
*
ERROR at line 1:
ORA-02266: unique/primary keys in table referenced by enabled foreign keys
SQL>
truncate table t1 cascade;
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
0 0 0
SQL>
rollback;
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
0 0 0
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
-- Recreate T3.
drop table t3 purge;
create table t3 (
id number,
t2_id number,
description varchar2(50),
constraint t3_pk primary key (id),
constraint t3_t2_fk foreign key (t2_id) references t2 (id)
);
-- Insert a data into each table.
begin
insert into t1 values (1, 't1 ONE');
insert into t2 values (1, 1, 't2 ONE');
insert into t2 values (2, null, 't2 TWO');
insert into t3 values (1, 1, 't3 ONE');
insert into t3 values (2, null, 't3 TWO');
commit;
end;
/
-- Check the contents of the tables.
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
1 2 2
SQL>
delete from t1;
delete from t1
*
ERROR at line 1:
ORA-02292: integrity constraint (TEST.T3_T2_FK) violated - child record found
SQL>
truncate table t1 cascade;
truncate table t1 cascade
*
ERROR at line 1:
ORA-14705: unique or primary keys referenced by enabled foreign keys in table "TEST"."T3"
SQL>
-- Clear down T3.
truncate table t3;
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
1 2 0
SQL>
-- Truncate still fails.
truncate table t1 cascade;
truncate table t1 cascade
*
ERROR at line 1:
ORA-14705: unique or primary keys referenced by enabled foreign keys in table "TEST"."T3"
SQL>
-- Deletion works as expected.
delete from t1;
select (select count(*) from t1) as t1_count,
(select count(*) from t2) as t2_count,
(select count(*) from t3) as t3_count
from dual;
T1_COUNT T2_COUNT T3_COUNT
---------- ---------- ----------
0 1 0
SQL>Please to add comments
No comments yet. Be the first to comment!