Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

Can anybody tell me how to delete data row by row from a master table & values in the child table using a loop. I have a master table which has got a foreign key relationship with other table. What i want is, i need to fetch the ID from master table & with this ID i want to delete the data present in the other table, along with the master table content.

*Get ID from Master table
*Delete the values in child table containing the same ID that was fetched from Master table.
*Delete that Master ID row from master table.

This is done a process of database cleanup, which contain junk data.

Also i am new to PL\SQL.

Thanks & regards,
Jagadisha.N

What I have tried:

SQL
Declare
your_variable varchar2(19);
BEGIN
 FOR x IN (SELECT COORDINATEDVERSIONID FROM Mastertable
                 where ISCURRENTVERSION = 0 order by coordinatedversionid asc) LOOP
   FOR y IN (DELETE FROM Childtable
            where coordinatedversionid = x.COORDINATEDVERSIONID order by coordinatedversionid asc
                      
            DELETE FROM Mastertable where coordinatedversionid = x.COORDINATEDVERSIONID
          
            ) LOOP
                 
         END LOOP;
   END LOOP;
END;
Posted
Updated 22-Jun-16 0:29am
Comments
jgakenhe 22-Jun-16 1:02am    
I'd set the Master table to Delete on Cascade (http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php) and then all you have to do is delete the Master key record by the Primary Key you wish; then all the related child records will be deleted.

Easy Peezy!

1 solution

Hi,

Check this...

Oracle/PLSQL: Foreign Keys with cascade delete[^]

Hope this will help you.

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900