This pseudo-code gives an idea of another safer approach that takes into account the constraint of having a 'relation' based on
primary-key+
foreign-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 table
DELETE FROM Invoice WHERE Invoice.id_invoice='put.here.the.invoice.id'
4)
Commit()
the transaction.
In case of error (exception),
RollBack()
the transaction.
p.d. I've assumed you have one
primary-key column named
id_invoice
in your
Invoice table that uniquely identifies the invoices and one foreign-key named
InvoiceID
in your
InvoiceDetails table.