Solved

How to perform Cascade Delete In Oracle database

Posted on 2007-03-28
6
45,240 Views
Last Modified: 2013-12-18
I am using Oracle 10.2.  How do I set up the relationship between parent table and child tables so that when
record is deleted from the parent table it should also delete all the records in the child tables.

Thank you
0
Comment
Question by:jyem
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 18812170
0
 

Author Comment

by:jyem
ID: 18812521
I created a foreign key in the chilld table and link it to the pramary key in the parent table.  When deleting the record from the parent table I got the following error.

ORA-02292: integrity constraint (CBTM_OWN.CONSIST_DP_LOCO_R01) violated - child record found
0
 
LVL 3

Expert Comment

by:cdemir
ID: 18814043
could you send table scripts
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 13

Accepted Solution

by:
riazpk earned 500 total points
ID: 18814095
while declaring the Foreign key, you will have to mention ON DELETE CASCADE option:

SQL>create table parent(a number primary key);

Table created.

SQL>ed
Wrote file afiedt.buf
  1* create table childe(a number, b varchar2(10), constraint child_fk foreign key(a)references parent on delete cascade)
SQL>/

Table created.

SQL>insert into parent values(1);

1 row created.

SQL>ed
Wrote file afiedt.buf

  1* insert into childe values(1, 'ABC')
SQL>/

1 row created.

SQL>commit;

Commit complete.

SQL>delete parent;

1 row deleted.

SQL>commit;

Commit complete.

SQL>SELECT * FROM parent;

no rows selected

SQL>ed
Wrote file afiedt.buf

  1* SELECT * FROM childe
SQL>/

no rows selected

SQL>
0
 

Author Comment

by:jyem
ID: 18815491
Thank you very much everyone.  Riazpk solution works.
0
 

Expert Comment

by:Chip
ID: 22877881
Is there a script that can be run to identify what that one row will affect, if it is deleted?  I need to display all the tables that are dependant on that one record before deleting for documentation purposes.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question