Referential CONSTRAINT definition 

A referential CONSTRAINT definition defines an integrity condition (restrictions for columns values, see data integrity) that must be satisfied by all the rows in two tables. The resultant dependency between two tables affects changes to the rows contained in them.

Syntax

<referential_constraint_definition> ::=
FOREIGN KEY [<referential_constraint_name>] (<referencing_column>,...)
REFERENCES <referenced_table> [(<referenced_column>,...)] [<delete_rule>]

referential_constraint_name, delete_rule

referenced_table
referenced_column

Reference table, referenced column (table/column that is to be addressed)

referencing_column

Referencing column (column that establishes the link to the column that is to be addressed)

 

Dependency between the model tables customer and reservation. The referential CONSTRAINT definition is specified when the reservation table is defined. The reservation table is assigned a foreign key that corresponds to the key in the customer table.

CREATE TABLE reservation(rno FIXED (4) KEY, cno FIXED (4), hno FIXED (4)
roomtype CHAR (6), arrival DATE, departure DATE,
FOREIGN KEY (cno) REFERENCES customer ON DELETE CASCADE

The defined relationship is assigned the name customer_reservation. The DELETE rule ON DELETE CASCADE specifies that deleting rows in the customer table causes the associated rows in the reservation table to be deleted automatically.

Explanation

A referential CONSTRAINT definition can be used in a CREATE TABLE statement or ALTER TABLE statement. The table specified in the corresponding statement (table_name) is referred to in the following sections as the referencing table.

The referencing columns are specified in the referential CONSTRAINT definition. The referencing columns must denote columns in the referencing table and must all be different. They are also called foreign key columns.

Referenced columns

Relationship between referenced and referencing columns:

The referencing table and the referenced table must be base tables, but not temporary base tables.

The current user must have the ALTER privilege for the referencing table and the REFERENCE privilege for the referenced table.

Name of a referential constraint

The name of a referential constraint can be specified after the keywords FOREIGN KEY.

Inserting and modifying rows in the referenced table

The following restrictions apply when rows in the referencing table are added or modified:

Let Z be an inserted or modified row. Rows can only be inserted or modified if one of the following conditions is fulfilled for the associated referenced table:

Further terms