Subscribe to Posts by Email

Subscriber Count

    696

Disclaimer

All information is offered in good faith and in the hope that it may be of use for educational purpose and for Database community purpose, but is not guaranteed to be correct, up to date or suitable for any particular purpose. db.geeksinsight.com accepts no liability in respect of this information or its use. This site is independent of and does not represent Oracle Corporation in any way. Oracle does not officially sponsor, approve, or endorse this site or its content and if notify any such I am happy to remove. Product and company names mentioned in this website may be the trademarks of their respective owners and published here for informational purpose only. This is my personal blog. The views expressed on these pages are mine and learnt from other blogs and bloggers and to enhance and support the DBA community and this web blog does not represent the thoughts, intentions, plans or strategies of my current employer nor the Oracle and its affiliates or any other companies. And this website does not offer or take profit for providing these content and this is purely non-profit and for educational purpose only. If you see any issues with Content and copy write issues, I am happy to remove if you notify me. Contact Geek DBA Team, via geeksinsights@gmail.com

Pages

12c Database : Truncate with Cascade

From 12c onwards,

Oracle 12c introduced truncate with cascade so when truncate operates on master table the child table rows also get truncated automatically.

	SCOTT@PDB > create table MASTER (
	  2  ID number generated always as identity PRIMARY KEY,
	  3  NAME VARCHAR2(20)
	  4  );
	 Table created.

	 SCOTT@PDB > create table CHILD (
	  2   ID number generated always as identity PRIMARY KEY,
	  3  NAMECHILD VARCHAR2(20)
	  4  );
	 Table created.

	 SCOTT@PDB > alter table CHILD add constraint FK_MASTER FOREIGN KEY (ID) references MASTER(ID) ON DELETE CASCADE;
	 Table altered.

	 SCOTT@PDB > insert into MASTER (NAME) values ('Geek DBA');
	 1 row created.

	 SCOTT@PDB > insert into CHILD (NAMECHILD) values ('Geek DBA');
	 1 row created.

	 SCOTT@PDB > commit;
	 Commit complete.

          SCOTT@PDB > select * from MASTER;
		 ID NAME
	---------- --------------------
		 1 Geek DBA

	 SCOTT@PDB > select * from CHILD;
		 ID NAMECHILD
	---------- --------------------
		 1 Geek DBA

	 SCOTT@PDB > truncate table MASTER cascade;
	 Table truncated.

	 SCOTT@PDB > select * from MASTER;
          no rows selected

	 SCOTT@PDB > select * from CHILD;
	 no rows selected

Note: Truncate cascade only works when you have the referential integrity with on delete cascade on

Comments are closed.