parent_table ---------------------------------------------------------------------- id name 1 a 2 b child_table --------------------------------------------------------------------- id parent_table_id name 1 1 c 2 1 d 3 1 e 4 2 f 5 2 g
When I will delete first row from parent table then all child row of parent first row. how is it possible??
Apart from cascade delete
, you can use join
, see example below:
DELETE parent_table, child_table
FROM parent_table INNER JOIN child_table
ON parent_table.id = child_table.parent_table_id
WHERE parent_table.id = 1
While adding foreign key constraint use below options
ON DELETE 'CASCADE'
ON UPDATE 'RESTRICT'
Now if you delete a parent row all associated child row will be deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With