Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a child and a parent row with one SQL script

Instead of deleting the child row and then writing another sql statement to delete the parent row I wanted to use one statement which will do both. FYI: we use Oracle database.

Update: I dont have a privilege to do DELETE ON CASCADE

like image 748
WowBow Avatar asked Mar 09 '12 20:03

WowBow


2 Answers

Define your foreign keys with cascading deletes. Then you only need to delete the "parent" row.

like image 163
Albin Sunnanbo Avatar answered Sep 21 '22 23:09

Albin Sunnanbo


delete from 
(
select * from parent join child using (id)
where id = 1
)

WARNING! Will only delete where both parent AND child rows exist. Will NOT delete parents without children

like image 38
grokster Avatar answered Sep 20 '22 23:09

grokster