Solved

How to perform Cascade Delete In Oracle database

Posted on 2007-03-28
6
45,137 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now