DELETE CASCADE and UPDATE CASCADE in SQL Server foreign key.





In this article, we will review on DELETE CASCADE AND UPDATE CASCADE rules in SQL Server foreign key with different examples.

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.

UPDATE CASCADE: When we create a foreign key using UPDATE CASCADE the referencing rows are updated in the child table when the referenced row is updated in the parent table which has a primary key.

We will be discussing the following topics in this article:

  1. Creating DELETE and UPDATE CASCADE rule in a foreign key using SQL Server management studio
  2. Creating DELETE CASCADE and UPDATE CASCADE rule in a foreign key using T-SQL script
  3. Triggers on a table with DELETE or UPDATE cascading foreign key

Let us see how to create a foreign key with DELETE and UPDATE CASCADE rules along with few examples.

Creating a foreign key with DELETE and UPDATE CASCADE rules

Using the SQL Server Management Studio GUI:

Login to the SQL Server using SQL Server Management Studio, Navigate to the Keys folder in the child table. Right click on the Keys folder and select New Foreign Key.

Edit table and columns specification by clicking … as shown in the below image.

DELETE CASCADE foreign key in SQL Server

Select the parent table and the primary key column in the parent table. select the foreign key column in the child table. Click on OK. Please refer to the below sample image.

Primary and Foreign key column mapping

In the INSERT and UPDATE specifications, select Cascade for the delete rule.

cascade as delete rule on a foreign key in SQL Server

Click on Close and save the table in the designer. Click Yes in the warning message window.

warning window

Once you click on Yes, a foreign key with delete rule is created. Similarly, we can create a foreign key with UPDATE CASCADE rule by selecting CASCADE as an action for the update rule in INSERT and UPDATE specifications.

Foreign key in SQL Server

Using T-SQL:

Please refer to the below T-SQL script which creates a parent, child table and a foreign key on the child table with DELETE CASCADE rule.

Insert some sample data using below T-SQL script.

Sample data

Now I deleted a row in the parent table with CountryID =1 which also deletes the rows in the child table which has CountryID =1.

deleting row in parent table

Please refer to the below T-SQL script to create a foreign key with UPDATE CASCADE rule.

Now update CountryID in the Countries for a row which also updates the referencing rows in the child table States.

update cascade rule in SQL Server foreign key

Following is the T-SQL script which creates a foreign key with cascade as UPDATE and DELETE rules.

To know the update and delete actions in the foreign key, query sys.foreign_keys view. Replace the constraint name in the script.

The below image shows that a DELETE CASCADE action and no UPDATE action is defined on the foreign key.

delete and update rules in SQL Server foreign key

Let’s move forward and check the behavior of delete and update rules the foreign keys on a child table which acts as parent table to another child table. The below example demonstrates this scenario.

In this case, “Countries” is the parent table of the “States” table and the “States” table is the parent table of Cities table.

We will create a foreign key now with cascade as delete rule on States table which references to CountryID in parent table Countries.

Now on the Cities table, create a foreign key without a DELETE CASCADE rule.

If we try to delete a record with CountryID =1, it will throw an error as delete on parent table “Countries” tries to delete the referencing rows in the child table States. But on Cities table, we have a foreign key constraint with no action for delete and the referenced value still exists in the table.

The delete fails at the second foreign key.

multiple childs

When we create the second foreign key with cascade as delete rule then the above delete command runs successfully by deleting records in the child table “States” which in turn deletes records in the second child table “Cities”.

Triggers on a table with delete cascade or update cascade foreign key

An instead of an update trigger cannot be created on the table if a foreign key on with UPDATE CASCADE already exists on the table. It throws an error “Cannot create INSTEAD OF DELETE or INSTEAD OF UPDATE TRIGGER ‘trigger name’ on table ‘table name’. This is because the table has a FOREIGN KEY with cascading DELETE or UPDATE.”

INSTEAD OF TRIGGERS on table with foreign key

Similarly, we cannot create INSTEAD OF DELETE trigger on the table when a foreign key CASCADE DELETE rule already exists on the table.

Conclusion

In this article, we explored a few examples on DELETE CASCADE and UPDATE CASCADE rules in SQL Server foreign key. In case you have any questions, please feel free to ask in the comment section below.

Please refer to this article, SQL Server foreign key to dig in more details on delete and update rules in SQL Server foreign key.