![]() |
|
|
By: Armando Prato | Read Comments (5) | Print Armando has over 24 years of industry experience and has been working with SQL Server since version 6.5. Related Tips: 1 | 2 | 3 | 4 | 5 | 6 | More |
|
Problem
Some time ago, I had a water cooler discussion with one my company’s java developers about maintaining data integrity. His belief was that all data integrity checks should be handled in the logic tier of an n-tier system. I politely explained why I consider this a bad practice. Foreign keys are specifically provided by SQL Server to prevent your database from storing invalid data relationships and should be a mainstay in every relational database model developed!
Solution
Foreign keys are part of the family of constraints that SQL Server provides to ensure database integrity. You may be familiar with other constraint types that help maintain data integrity such as Primary Key constraints, Unique constraints, Default constraints, and Check constraints. Each of these constraint types serves a specific purpose. The foreign key’s purpose is to ensure the relationship integrity between a parent table and its child tables.
You can define a foreign key as follows
| ALTER TABLE DBO.<child table> ADD CONSTRAINT <foreign key name> FOREIGN KEY <child column> REFERENCES DBO.<parent table>(<parent column>) {ON [DELETE|UPDATE] CASCADE} |
The following example declares that a line item row cannot exist without an associated order header row. The ON DELETE CASCADE option tells the database engine that if the parent ORDER HEADER row’s ORDERNUMBER is deleted, then any LINE ITEM tied to the ORDER HEADER by the deleted ORDERNUMBER should be automatically deleted as well.
| ALTER TABLE DBO.LINEITEM ADD CONSTRAINT FK_LINEITEM_ORDERNUMBER FOREIGN KEY (ORDERNUMBER) REFERENCES DBO.ORDERHEADER(ORDERNUMBER) ON DELETE CASCADE |
There are obvious reasons for defining foreign key constraints in your data model
The not so obvious
Unlike primary key constraints and unique constraints, foreign key constraints are not automatically indexed by SQL Server. However, indexing the column used in your foreign key is a good idea for a few reasons
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
| Tuesday, March 24, 2009 - 7:17:36 AM - markntejas |
|
| many thx ... | |
| Thursday, January 26, 2012 - 11:36:06 PM - Lx[n] |
|
|
Nice article, but just want to let you know that I believe someone taken your article and make it as his own, below are the url link: http://ps-india.blogspot.com/2008/10/importance-of-sql-server-foreign-keys.html Prakash Samariya (M): +91-9879074678 Cheers! |
|
| Friday, January 27, 2012 - 9:21:04 AM - Armando Prato |
|
|
Thanks for letting me know. I will let the folks at Edgewood know |
|
|
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 |