Solved

How to perform Cascade Delete In Oracle database

Posted on 2007-03-28
6
45,151 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.

Question has a verified solution.

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

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

896 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

18 Experts available now in Live!

Get 1:1 Help Now