This pseudo-code gives an idea of another safer approach that takes into account the constraint of having a 'relation' based on primary-key
between both tables:
1) Open one transaction
(this is implemented by almost all DB engines);
2) Delete the rows from the 'child' table (InvoiceDetails
), the selector should be the value for the foreign key that references the 'parent' table (Invoice
DELETE FROM InvoiceDetails WHERE InvoiceDetails.InvoiceID='put.here.the.invoice.id'
3) Delete the invoice row from the Invoice
DELETE FROM Invoice WHERE Invoice.id_invoice='put.here.the.invoice.id'
In case of error (exception),
p.d. I've assumed you have one primary-key
in your Invoice
table that uniquely identifies the invoices and one foreign-key named
in your InvoiceDetails