![]() |
|
|
By: Atif Shehzad | Read Comments (4) | Print Atif is a passionate SQL Server DBA, technical reviewer and article author. Related Tips: 1 | 2 | 3 | 4 | 5 | 6 | More |
|
Foreign key constraints are an integral part of SQL Server database design. These are used to maintain integrity among related data in different tables. While implementing update and delete operations on values in the parent table (referenced table with primary key) we have to consider the impact on related values in the child table. SQL Server provides different rules for managing the effect of updates and deletes on child table values. How can these rules be used effectively without threatening the relational integrity?
Foreign key constraints may be created by referencing a primary or unique key. Foreign key constraints ensure the relational integrity of data in associated tables. A foreign key value may be NULL and indicates a particular record has no parent record. But if a value exists, then it is bound to have an associated value in a parent table. When applying update or delete operations on parent tables there may be different requirements about the effect on associated values in child tables. There are four available options in SQL Server 2005 and 2008 as follows:
It is not necessary that the same rule be applied for both update and delete operations. There may be different rules for each of the update and delete operations on a single FK constraint. Before proceeding with the demo, here is summary of the effects for update and delete operations:
| Specification | Update operation on parent table | Delete operation on parent table |
|---|---|---|
| No Action | Not allowed. Error message would be generated. | Not allowed. Error message would be generated. |
| Cascade | Associated values in child table would also be updated. | Associated records in child table would also be deleted. |
| Set NULL | Associated values in child table would be set to NULL. Foreign key column should allow NULL values to specify this rule. | Associated values in child table would be set to NULL. Foreign key column should allow NULL values to specify this rule. |
| Set Default | Associated values in child table would be set to default value specified in column definition. Also default value should be present in primary key column. Otherwise basic requirement of FK relation would fail and update operation would not be successful. If no default value is provided in foreign key column this rule could not be implemented. | Associated values in child table would be set to default value specified in column definition. Also default value should be present in primary key column. Otherwise basic requirement of FK relation would fail and delete operation would not be successful. If no default value is provided in foreign key column this rule could not be implemented. |
The following demonstrates each of these options.
Now let's create a couple of tables to resemble a foreign key relationship.
The foreign key relation can be created either through SSMS GUI or T-SQL. Rules for update/delete operations may be specified explicitly. However if nothing is specified then the default rule is No Action. The rule may be changed to any other option at any time later by recreating the FK relation. Let's create the foreign key with the default specification through T-SQL.
Now populate the tables with sample data.
Now we can look at the relationship along with the rules available in SSMS. In SSMS, right click on the FK for table EmpEducation and select modify to open the relationships frame as shown below.
For our data with default specification (No Action) SQL Server would not allow an update or delete operation on referenced values of the primary key table. Since we did not define a specification for our foreign key the default No Action is used. So based on this, no update or delete should be allowed for values that are referenced in the child table.
Let's verify the effect of the No Action rule.
The following error messages are generated as result of the above script and the update and delete did not take place.
Now let's change the default specification (No Action) to Cascade. Select the CASCADE rule from the SSMS GUI as shown in the above screenshot or use this T-SQL code.
The following script adds "ON DELETE CASCADE ON UPDATE CASCADE"
Run script #4 again to verify the result produced with the cascade option.

To utilize the SET NULL rule for update/delete operations the foreign key column should allow NULL values otherwise the SET NULL specification would fail by generating an error message.
The data in child table (EmpEducation) shows that as a result of the update and delete operations foreign key values are set to NULL as shown below.

For utilizing the SET DEFAULT rule for update/delete operations default value should be there for foreign key column. Else SET DEFAULT specification would fail by generating error message. Our foreign key column has default value 100, so we may proceed with following steps
We have specified a default value of 100 for empno in the child table. Script 4 would change the corresponding four values to 100 as shown below.

The following script drops the objects we created for this demo.
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Thursday, April 21, 2011 - 1:22:20 PM - Joe Celko | Read The Tip |
|
SQL and RDBMS do not have "Child" and "Parent" tables. That was the old CODASYL network model. We have "Referencing" and "Referenced" tables. They are quite different; you cannot be your own parent, but you can make self-references. A child has s siginal parent and navigation is done in a record-at-a-time manner (did you read Bachman's original paper?). Referencing is a schema level concept. |
|
| Friday, April 22, 2011 - 1:27:11 AM - Atif Shehzad | Read The Tip |
|
I have noted the term Parent/Child table at various articles in MSDN. For example here is a sentence from MSDN "The example shown in this topic uses tables from the AdventureWorks database. The example uses the child Product table that contains the foreign-key column ProductCategory that is defined in the parent table ProductCategories" So the real world rule is OK that you can not be your own parent, but in RDBMS the term "Parent/Child" is used. Please provide some refrence from BOL for your point.
Thanks |
|
| Monday, April 25, 2011 - 8:52:40 PM - Jeremy Kadlec | Read The Tip |
|
Gentlemen,
Let’s agree to disagree on terminology and focus on the fact that this tip outlines the four rules available in SQL Server to manage foreign key relationships for DELETE and UPDATE commands.
Thank you, Jeremy Kadlec |
|
| Tuesday, May 03, 2011 - 10:26:02 AM - Vishal Gamji | Read The Tip |
|
Nice article with good guidance on managing FKs. |
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |