Referential integrity

For a foreign key relationship to be valid, the entries in the foreign key must correspond to the primary key values of a row in the referenced table. Occasionally, some other unique column combination may be referenced instead of a primary key.

A foreign key is a reference to a primary key or UNIQUE constraint, usually in another table. When that primary key does not exist, the offending foreign key is called an orphan. SQL Anywhere automatically ensures that your database contains no rows that violate referential integrity. This process is referred to as verifying referential integrity. The database server verifies referential integrity by counting orphans.

When using a multi-column foreign key, you can determine what constitutes an orphaned row versus what constitutes a violation of referential integrity using the MATCH clause. The MATCH clause also allows you to specify uniqueness for the key, thereby eliminating the need to declare uniqueness separately.

The following is a list of MATCH types you can specify:

  • MATCH [ UNIQUE ] SIMPLE   A match occurs for a row in the foreign key table if all the column values match the corresponding column values present in a row of the primary key table. A row is orphaned in the foreign key table if at least one column value in the foreign key is NULL.

    MATCH SIMPLE is the default behavior.

    If the UNIQUE keyword is specified, the referencing table can have only one match for non-NULL key values.

  • MATCH [ UNIQUE ] FULL   A match occurs for a row in the foreign key table if none of the values are NULL and the values match the corresponding column values in a row of the primary key table. A row is orphaned if all column values in the foreign key are NULL.

    If the UNIQUE keyword is specified, the referencing table can have only one match for non-NULL key values.

 Example 1
 Example 2
 Example 3

Referential cycles