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:
Let us see how to create a foreign key with DELETE and UPDATE CASCADE rules along with few examples.
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”.
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.
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.
Detailed-oriented, Responsible and Committed Full Stack Developer and Junior Team Lead at MagnusMinds IT Solution having 2+ years of experience. Skilled in C#, .Net Core, Web API, ASP.NET, jQuery, SQL Server, SSIS, SSRS.