6.3 Integrity Rules


A Relational Database Management System (RDMS) enforces integrity rules to ensure the accuracy and accessibility of the data.
Such rules govern which operations can be performed on the data in addition to putting constraints on the structure of the database.
There are three integrity rules that a relational database follow which are: the Key Integrity rule, the Entity Integrity rule and the Referential Integrity rule which are described next.

The Key Integrity Rule

The Key Integrity rule specifies that rows in a table must be unique with no row duplication.
This is useful to remove any data ambiguity.
Thus, tables are defined as a unique set of rows with no two rows having the same combination of values for their attributes.
Key constraints enforce this rule as follows (which we discussed earlier):
Candidate Key: that is an attribute or a minimal combination of attributes that uniquely identify any row in a table.

6.3 Integrity Rules


Minimal means that removing an attribute from the candidate key causes the inability of the candidate key to identify any row uniquely, thus not being a candidate key anymore.
There could be more than one candidate key in a table.
Primary Key: is the candidate key chosen in the definition of the table schema to uniquely identify each row in the table.

The Entity Integrity Rule

The Entity Integrity rule states that no attribute that is part of a primary key is allowed to have value NULL.
If NULL values are allowed for primary key attributes, then two rows can have NULL as primary key values and thus the two rows can’t be uniquely identified.
This causes the Key Integrity rule to be violated.
An illustration on how the Entity Integrity rule is shown in Figure 5.3 with the student member table having two rows with NULL values for the primary key attribute.


6.3 Integrity Rules



The Referential Integrity Rule

The Referential Integrity rule is concerned with the linking between tables using foreign keys.
It states that an attribute in a table schema T1 is a foreign key if:
      1. It has a relationship with a primary key in T2, and
      2. The domain of the foreign key in T1 is the same as the domain of the key in T2, and
     3. The foreign key value in T2 must be equal to the value of T2’s primary key in some row. Otherwise,
        each attribute value in the foreign key must be NULL.

6.3 Integrity Rules


Thus, the Referential Integrity rule ensures that any foreign key value is always pointing to a primary key row in the other table.
References to non-existing primary keys are not allowed. If a primary key referred by a foreign key does not exist, the foreign key value must be NULL.