Cascading Deletes

A cascading delete is relatively simple to create. Listing 30.6 shows a cascading delete trigger for the customers table.

Tip

SQL Server 2000 added a feature that allows you to define cascading actions on a FOREIGN KEY constraint. When defining the constraints on a table, you can use the ON UPDATE CASCADE or ON DELETE CASCADE clause, which causes changes to the primary key of a table to cascade to the related foreign key tables. Refer to Chapter 26, “Implementing Data Integrity,” for further information on this option.

Listing 30.6 A Cascading Delete for the customers Table

image

The following DELETE statement deletes the row for ...

Get Microsoft® SQL Server 2008 R2 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.