Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete master detail records using one sql?

Using Delphi 7 and interbase 7

Is it possible to delete a master detail record and all of its nested detail records in one SQL statement?

Example:

Table1
ID - Integer
TITLE - Varchar(80)

Table2
ID - Integer
Table1_ID - Integer
TITLE - Varchar(80)

Table3
ID - Integer
Table2_ID - Integer
TITLE - Varchar(80)

I would like to delete ID 10 from Table1, and all of its matching records (Table1_ID) in table 2, and all its matching records (Table2_ID) in table 3

If i can't do this in one sql, how do i do it in multiple sqls (correct sequence to call statements)?

like image 254
IElite Avatar asked Dec 03 '22 01:12

IElite


1 Answers

Yo can do it witn some SQL's deleteing in order, records on Table3, table2 and table1. All in one transaction to do it like "unique operation".

One alternative is use Triggers for delete record related on table2 when you delete one record on table1, and equivalent in table2 to delete related records on table3.

Another (if DB let you) is use ON CASCADE DELETE (or similar) to delete related records on a tabla2 and table3, when you delete a record un table1 (see help or documentation on your SGBD/Database).

Excuse-me for mistakes with English. It's not my natural language.

Regards.

like image 56
Germán Estévez -Neftalí- Avatar answered Dec 18 '22 08:12

Germán Estévez -Neftalí-