Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete data in child tables?

I have 6 tables named A,B,C,D,E,F

I have A's Primary key, Now B have ref in A, C has ref in B, D has ref in C

E has ref in A, F has ref in B

Now I want to delete A's records and its related records in other tables and I have only A's primary key,

Best way to delete all records.

Thanks in advance.

like image 795
Karan Gandhi Avatar asked Dec 02 '25 10:12

Karan Gandhi


1 Answers

One of

  • Use a stored procedure to delete child tables, using WHERE EXISTS
  • Set foreign keys to CASCADE DELETE, so all child rows are deleted when you DELETE from A
    See How do I use cascade delete with SQL Server?
like image 157
gbn Avatar answered Dec 04 '25 23:12

gbn