Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete records from multiple tables simultaneously?

Tags:

sql

oracle

I have 4 tables. In first table has appid as primary key and in other three table its foreign key. I want to delete data from all the three in one query. I tried my best but failed. Can anybody help?

like image 328
sam Avatar asked Aug 16 '11 10:08

sam


1 Answers

You cannot write a delete statement that references more than one table, you need to write 4 delete statements.

However, if appropriate, you can define the foreign keys on the 3 child tables to "ON DELETE CASCADE". Then when you delete from the parent table, all associated rows from the 3 child tables are also deleted. This can be useful sometimes, but I would not recommend it as a general practice as it can be dangerous and confusing for developers.

like image 191
Tony Andrews Avatar answered Oct 28 '22 21:10

Tony Andrews