Referential integrity is coordinated through the use of primary and foreign keys.
Referential constraints allow you to ensure that data inserted in one table has matching values in another table. For example, in the pubs2 database, a row inserted into the salesdetail table, recording the sale of books, must have a valid title_id in the titles table. Currently, pubs2 enforces this referential integrity with a trigger. However, a referential constraint on the salesdetail table, referencing the title_id column in the titles table, could accomplish the same enforcement of referential integrity.
A referential constraint involves two tables:
A referencing table on which data insertions are performed. This table contains one or more columns that serve as a foreign key.
A referenced table against which insertions are checked. This table contains one or more columns that serve as a primary key.
When an insertion occurs on the referencing table, the value in the foreign key columns must match a value in the primary key columns on the referenced table. You can define a referential constraint on either the referenced or the referencing table.
Only a database or table owner who has references permission on the referenced table can create a referential constraint.
Creating a referential constraint
In the table, select the Foreign Keys folder.
Select File | New | Foreign Key. The Foreign Key Creation wizard opens. Complete the wizard. The Foreign Key Creation wizard asks for the following information:
Input |
Description |
|---|---|
Database |
Database in which the foreign key table is located. |
Table |
Table in which the foreign key is located. |
Columns to match |
The local columns to match against the foreign columns. |
Name |
A name for the foreign key. |