SQL Server: Primer — Foreign Key Constraints

by Stephen Horsfield 11. January 2008 15:31

Overview

You may know that it is best practice to create FOREIGN KEY constraints on related tables in a database.  Why is this?

Details

A foreign key constraint can be used for:

  • Ensuring referential integrity: a foreign key constraint can ensure database consistency
  • Self-documenting databases: the foreign key clause can be viewed in the database and describes a relationship
  • Efficiency: even without an index, the SQL Server engine recognises the constraint and uses it to optimise table joins automatically

Generally, you will also want to index the foreign key columns in the table, but it is worth adding the FOREIGN KEY constraint even if you don't create the index because it helps the database to optimise the execution plan.

References

Versions

Metadata

Tags:

IT Management | Software Development | SQL Server

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar