DELETE and UPDATE CASCADE in SQL Server foreign key

Jul 20, 2020

In this article, we will review on DELETE 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 CASCADE and UPDATE CASCADE rule in a foreign key using T-SQL script
  2. 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

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.

 

Now, Check Records.

 

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.

 

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.

 

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 UPDATE CASCADE action is defined on the 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 = 3, 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.

 

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.”

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.

Hardik Dangar

About the Author

Hardik Dangar

Project Lead in Magnusminds

Hardik is working as Project Lead of MSBI in INDIA. Hardik started his career working on SQL Server and MSBI. Hardik is having 5+ years of experience. In the starting of his career he was working on SQL Server, SSIS and SSRS. Hardik likes to explore technical things on SQL Server.