Posts Cascade Delete in SQL
Post
Cancel

Cascade Delete in SQL

Many a times you need to delete the record but at the same time you need to make sure all the related or referenced records are also deleted. This is because you can not delete a record if it is referenced in another table. In this case you can either delete the referenced record or you can set the “Cascade Delete On”.

Here is the code to implement cascade delete in SQL

ALTER TABLE B  ADD (

CONSTRAINT FK_1 FOREIGN KEY (PARENT_ID)

REFERENCES A (ID{.tfTextLink}) ON DELETE CASCADE)

This code will delete all the referenced records on deletion of primary record. You can use this because deleting the referenced records becomes very tedious at times.

Similar to Cascade Delete there is also Cascade Update. For that stay tuned for my next post :).

This post is licensed under CC BY 4.0 by the author.