In the previous tutorial, you learned how to delete data from multiple related tables using a single DELETE statement. However, MySQL provides a more effective way called ON DELETE CASCADE referential action for a foreign key that allows you to delete data from child tables automatically when you delete the data from the parent table. If a record in the parent table is delete then the corresponding records in the child table will automatically be deleted. Use the ON DELETE CASCADE option if you want rows deleted from the child table when the DELETE or MERGE statement removes corresponding rows from the parent table. Here the parent table is the table specified in the REFERENCING clause of the definition of an enabled foreign key constraint,.
Any record on table BookCourses that has reference to table Courses will be deleted automatically. But when you try to delete on table BookCourses only the table itself is affected and not on the Courses. How do I use cascade delete with SQL Server? In this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with different examples. Foreign key constraints: When to use ON UPDATE.
DELETE CASCADE : When we create a foreign key using this option, it deletes the referencing rows in the child table when the referenced row is deleted in the parent table which has a primary key.