Scalable Database Server, HiRDB Version 8 Description
Tables in a database are normally not independent entities, because typically there are links between tables. The data in a table that is not linked is probably of no value to other tables. To maintain referential integrity of data that is linked across tables, the concept of referential constraints is introduced. A referential constraint is defined for a specific column (called a foreign key) when a table is defined. A table in which a referential constraint and a foreign key are defined is called a referencing table, while a table that is referenced from a referencing table with a foreign key is called a referenced table. In a referenced table, a primary key that is referenced by the foreign key must be pre-defined.
Note that execution of SQL code or a utility may cause a loss of referential integrity between a referenced table and a referencing table. When this occurs, the referencing table is placed in check pending status. For details about check pending status, see 5.9 Check pending status.
Figure 5-7 shows an example of a referenced table and a referencing table. In this example, the product table is the referencing table, and the manufacturer table is the referenced table. The foreign key in the referencing table references the primary key to determine the name of the manufacturer.
Figure 5-7 Example of referenced table and referencing table
For details about referential constraints, see the HiRDB Version 8 Installation and Design Guide.
To enable a referential constraint, you must first define in the referenced table the primary key that is referenced by the foreign key. To do so, use the CREATE TABLE definition SQL statement to specify PRIMARY KEY in the referenced table. To apply check pending status, specify USE in the pd_check_pending operand or omit specification of the pd_check_pending operand.
To define a referential constraint, you first specify FOREIGN KEY in the referencing table. In the FOREIGN KEY clause, you specify the following:
The following subsections explain the actions in the referenced table and the referencing table when CASCADE and RESTRICT are specified.
When CASCADE is specified and a primary key of a referenced table is changed, the foreign key is also changed. When a foreign key of a referencing table is changed, a check is performed to determine if there is a row containing a primary key whose value is the same as the value of the foreign key after the change; the foreign key is not changed if such a change would result in a referential constraint violation.
Figures 5-8 and 5-9 show examples of the actions that occur when CASCADE is specified when SQL code is executed for a referenced table and for a referencing table.
Figure 5-8 Example of the action when update SQL code is executed for a referenced table (with CASCADE specified)
Figure 5-9 Example of the action when update SQL code is executed for a referencing table (with CASCADE specified)
When RESTRICT is specified, then when a primary key of a referenced table is changed, a referential constraint violation occurs if there is a row that contains a foreign key with the same value. If the foreign key can be changed, a check is performed to determine if a row contains a primary key with the same value and, if a referential constraint violation would result, the foreign key is not changed.
Figure 5-10 shows the actions that occur when RESTRICT is specified when SQL code is executed for a referenced table. The action that occurs in a referencing table is the same as when CASCADE is specified (see Figure 5-9).
Figure 5-10 Example of the action when update SQL code is executed for a referenced table (with RESTRICT specified)
When you use a data manipulation SQL (other than PURGE TABLE) to manipulate data in a referenced table or referencing table, HiRDB performs checking when the SQL code is executed in order to guarantee data integrity. However, if you perform the data operations shown below, HiRDB may no longer be able to guarantee data integrity.
If you do perform any of these data operations, you must verify the integrity of the data. For details about how to verify data integrity, see the HiRDB Version 8 Installation and Design Guide. If USE is specified in the pd_check_pending operand, the referencing table is placed in check pending status if you perform any of these data operations.
All Rights Reserved. Copyright (C) 2007, Hitachi, Ltd.