Ruihan Wang |
Referential Integrity
Often, in relational database, we wish to ensure that a value that appears in one relation for a given set of attributes also appears for a certain set of attributes in another relation. This condition is called Referential integrity (RI). It is the concept of relationships between tables, based on the definition of a primary key and a foreign key.
The following table includes the important elements of Referential Integrity:
|
|
|
| Child Table | A table, where the referential constraints are defined. A child table is synonymous with the referencing table. |
| Parent Table | The table being referenced by a Child table. A Parent table is synonymous with the referenced table. |
| Primary Key | A primary key uniquely identifies a row of a table. |
| Foreign Key | A foreign key refers to columns in the Child table. A foreign key may consist of up to 16 columns. |
| Referencing columns | A referencing columns are within a referencing table that are foreign keys for columns in some other referenced table. |
| Referenced columns | A referenced columns are defined as either primary key columns or unique columns in a referenced table. |
In the Teradata RDBMS, there are three choices to implement RI:
By providing specification of columns within a referencing table that are foreign keys for columns in some other referenced table, referential integrity is a reliable mechanism which prevents accidental database corruptions when doing inserts, updates, and deletes. It states that a row cannot exist in a table with a non-null value for a referencing column if an equal value does not exist in a referenced column.
For example, let's suppose in customer_service database, there is a tuple t1 in the employee_phone relation with t1[employee_number] = '1018', but there is no tuple in the employee relation for the 1018. This situation would be undesirable. We expect the employee relation to list all employee_number. Therefore, tuple t1 would refer to an employee that does not exist. Clearly, we would like to have an integrity constraint that prohibits this sort of situation. We could define employee_number in employee relation as a foreign key in employee_phone relation. Under this situation, employee is the referenced table, employee_name of employee table is the referenced column; employee_phone is the referencing table, employee_name of employee_phone is the referencing column.
Once we define the employee_number as a foreign key in employee_phone
relation, if we try to insert a row with a primary key value that does not exit
in employee table, the system will not allow this insertion. This is the way the
Teradata RDBMS maintains referential integrity. The following summarize the
benefits of referential integrity:
For large database system like the Teradata RDBMS, it is critique to
ensure referential integrity.
Defining
Referential Constraints
Referential constraint provide a means of ensuring that changes made to the database by authorized users do not result in a loss of data consistency. The constraints are in the following forms:
Proper definition of referential constraints not only allows us to test values inserted in the database, but also permits us to test queries to ensure that the comparisons made make sense. The check clause in the Teradata RDBMS permit data to be restricted in powerful way that most programming language type systems do not permit. Specifically, the check clause permits the schema designer to specify a predicate that must be satisfied by any value assigned to a variable. The referential constraint can be defined by CREATE TABLE statement.
Referential constraints must meet the following criteria:
Primary and foreign keys can be specified as part of the SQL CREATE TABLE
statement:
| Key word |
|
| Primary key | The primary key clause of the CREATE TABLE statement includes a list of the attributes that constitute the primary key. |
| Unique | The unique clause of the CREATE TABLE statement includes a list of the attributes that constitute a candidate key. |
| Foreign key | The foreign key clause of CREATE TABLE statement includes both a list of the attributes that constitute the foreign key and the name of the relation referenced by the foreign key. |
Using the partial SQL DDL definition of our customer_service database as an example to illustrate the syntax of primary- and foreign-key declarations.
Examples of
SQL data definition for check constraints
Referential
constraint checks
The Teradata RDBMS performs referential constraints checks whenever any of the following occur:
| RDBMS statement | Constraint check performed |
| INSERT | For parent table: None; for child table: Must have matching parent key value if the foreign key is not null |
| DELETE | For parent table: Abort the request if the deleted parent key is referenced by any foreign key; for child table: None |
| UPDATE | For parent table: Abort the request if the parent key is referenced by any foreign key; for child table: New value must match the parent key when the foreign key is updated. |