Integrity constraints provide a means of ensuring that changes made to the database by authorized users do not result in a loss of data consistency.
In the network model and the E-R model, we saw integrity constraints in the form of:
Key declarations, the stipulation that certain attributes form a candidate key for a given entity set constrains the set of legal insertions.
Form of a relationship, many-to?many, one?to?many, one?to?one. A one-to? one or one?to-many relationship restricts the set of legal relationships among entities of a collection of entity sets.
Another example of an integrity constraint is set retention in the network model.
In general, an integrity constraint can be an arbitrary predicate pertaining to the database. However, arbitrary predicates may be costly to test. Thus, we usually limit ourselves to integrity constraints that can be tested with minimal overhead. This is the purpose behind dependency ? preserving decompositions of relation schemes. Recall that in a dependency ? preserving decomposition, it is possible to test for satisfaction of the data dependencies without the need to compute any joins. Domain ? key normal is an ideal design from the point of view of efficient testing of integrity constraints, since the only forms of constraint that need be tested are key constraints and domain constraints.
If the key and domain constraints are satisfied, and the database scheme is in DKNF, then all integrity constraints on the database are satisfied.
Key constraints are one of the most easily tested forms of consistency constraint, especially ifan index is maintained on that candidate key. During the process of inserting a record into the database a lookup must be performed using the index and any duplicate key values that may exist are found. Since not all index search keys are candidate keys for the relation (Indices may be for secondary keys), we need to declare an index to be either
Unique. Only one record may exist for a key value
Non-unique. Multiple records are allowed to have the same key value
Another form of constraint that is easy to test is domain constraints. Testing domain constraints is analogous to runtime ? type checking in a programming language. A form of constraint closely related to domain constraints involves the admissibility of null values. We may forbid null values for certain attributes but allow them for others.
Relatively few systems allow the expression of constraints that are more complex than key declarations or domain constraints. The original proposal for the SQL language included a general purpose construct called the assert statement for the expression of integrity constraints.
An assertion pertaining to a single relation takes the form:
For example, if we wish to define an integrity constraint that no account balance is negative we write:
In its most general form, the assert statement takes the form: