0

I have 3 tables.

  • mainT has primary key main_id
  • sub1T has foreign key to main_id
  • sub2T has foreign key to main_id

I need to delete all records from sub1T and sub2T in one SQL command.

I know how to delete all records inside sub1T using this command:

DELETE FROM `sub1T` 
WHERE NOT EXISTS (SELECT NULL 
                  FROM mainT 
                  WHERE `mainT`.main_id = main_id)

I need to delete from multiple child tables

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Just E
  • 5
  • 4
  • I don't think you can delete from multiple tables in one command. You need to write PL/SQL for that. – Srihari Karanth May 23 '17 at 10:56
  • Most RDBMS support `on delete cascade`, which will delete data from child tables, when parent is deleted. For current requirement, you should use `2` `delete` statements and then try to implement `on delete cascade` on current table structure. – Utsav May 23 '17 at 10:57
  • 1
    Possible duplicate of [MySQL delete row from multiple tables](https://stackoverflow.com/questions/2214433/mysql-delete-row-from-multiple-tables) – Alexander May 23 '17 at 11:17
  • we cant do this in simple sql command – Just E May 30 '17 at 21:10

0 Answers0