Creating referential constraints on a table

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:

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.

Privileges

Only a database or table owner who has references permission on the referenced table can create a referential constraint.

StepsCreating a referential constraint

  1. In the table, select the Foreign Keys folder.

  2. Select File | New | Foreign Key. The Foreign Key Creation wizard opens. Complete the wizard. The Foreign Key Creation wizard asks for the following information:

    Table 17-1: Inputs to foreign key creation wizard

    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.