Previous Topic: Defining an IndexNext Topic: Dropping a Default Index


Defining a Referential Constraint

You create a referential constraint by issuing the CREATE CONSTRAINT statement and specifying the referenced and referencing tables and columns.

Things You Can Specify

  1. Name of the constraint
  2. The referencing table and column(s)
  3. The referenced table and column(s)
  4. Whether the referential constraint is linked or unlinked (the default)
  5. Options such as clustered (ORDER BY) or indexed (INDEX)

Considerations

Example - Linked Referential Constraint

In this example, a linked referential constraint has been created to ensure that the employee ID in the benefits table is a valid ID by checking it against the employee IDs in the employee table. The referential constraint is indexed and ordered by the fiscal year.

create constraint emp_benefits
   benefits (emp_id)
   references employee (emp_id)
   linked index
     order by (fiscal_year desc);

Example - Unlinked Referential Constraint

In this example, an unlinked referential constraint has been created to ensure that an employee's manager is a valid employee. Since this is a self-referencing constraint (both columns being in the same table), it must be unlinked. UNLINKED is the default.

create constraint manager_emp
   employee (manager_id)
   references employee(emp_id);