Constraints on Relations

Constraints on Relations

Relational algebra offers a means to express common constraints, such as the referential integrity constraints introduced in "The Modeling of Constraints". Actually, we shall see that relational algebra provides us convenient ways to express a large variety of other constraints. Even functional dependencies can be expressed in relational algebra, as we shall see in "Additional Constraint Examples" Example 1. Constraints are very important in database programming, and we shall cover in "Multimedia Networking" how SQL database systems can enforce the same types of constraints as we can express in relational algebra.

Relational Algebra as a Constraint Language

There are two ways in which we can use expressions of relational algebra to express constraints.

1. If R is an expression of relational algebra, then is a constraint that says "The value of R must be empty", or equivalently "There are no tuples in the result of R".

2. If R and S are expressions of relational algebra, then is a constraint that says "Every tuple in the result of R must also be in the result of  S". Of course the result of S may contain additional tuples not produced by R.

These ways of expressing constraints are in fact equivalent in what they can express, but sometimes one or the other is clearer or more brief. That is, the constraint could just as well have been written   To see why, notice that if every tuple in R is also in S, then surely R - S is empty. On the other hand, if R - S contains no tuples, then every tuple in R must be in S (or else it would be in R - S).

However, a constraint of the first form, , could just as well have been written . Technically, is not an expression of relational algebra, but since there are expressions that evaluate to , such as R - R, there is no harm in using as a relational-algebra expression. Note that these equivalences hold even if R and S are bags, provided we make the conventional interpretation o.f : each tuple t appears in S at least as many times as it appears in R.

In the next sections, we shall see how to express important constraints in one of these two styles. As we shall see in "Multimedia Networking", it is the first style - equal-to-the-emptyset - that is most frequently used in SQL programming. On the other hand, as shown above, we are free to think in terms of set-containment if we wish and later convert our constraint to the equal-to-the-emptyset style.