Share via


Overview of Referential Integrity

Referential integrity is a system of rules that ensure relationships between rows in related tables are valid and that you do not accidentally delete or change related data.

Note

A new version of Table Designer appears for databases in the SQL Server 2012 format. This topic describes the old version of Table Designer, which you use with databases in earlier formats of SQL Server.

In the new version, you can change a table definition through a graphical interface or directly in a script pane. If you use the graphical interface, the table’s definition is automatically updated in the script pane. To apply the SQL code in the script pane, choose the Update button. For more information about the new version, see How to: Create Database Objects Using Table Designer.

Procedure

You can set referential integrity when all of the following conditions are met:

  • The matching column from the primary table is a primary key or has a unique constraint.

  • The related columns in the foreign table have the same data type and size.

When referential integrity is enforced, you must observe the following rules:

  • You cannot enter a value in the foreign-key column of the related table if that value does not exist in the primary key of the related table. However, you can enter a null in the foreign-key column. For example, you cannot indicate that a job is assigned to an employee who is not included in the employee table, but you can indicate that an employee has no assigned job by entering a null in the job_id column of the employee table.

  • You cannot delete a row from a primary key table if rows matching it exist in a related table. For example, you cannot delete a row from the jobs table if there are employees assigned to the job represented by that row in the employee table.

  • You cannot change a primary key value in the primary key table if that row has related rows. For example, you cannot change a row's job_id value in the jobs table if there are employees with that job_id in the employee table.

See Also

Reference

Foreign Key Relationships Dialog Box

Other Resources

Working with Relationships

Database Objects