Download free books at BookBooN.com
An Introduction to Relational Database Theory
159
Constraints and Updating
Faced with such a plethora of choice for general methods of expressing constraints, Tutorial D does not
arbitrate in favour of any of the noted candidates, allowing the user to choose freely from among them
whichever is deemed most suitable for each particular purpose. The availability of logical connectives
gives the user the further freedom to decide how best to arrange the database constraint into declared
constraints, individually named and formulated. Sadly, we cannot say the same for the commercially
available DBMSs at the time of writing (2009), for we are not aware of any widely available SQL
implementation that supports any of the noted candidates for use in constraints.
xviii
Typically, the SQL user
is restricted to certain special-purpose shorthands of the kinds described in the next section.
6.4 Useful Shorthands for Expressing Constraints
In Chapter 5 I showed how a relational database language can be extended by defining new relational
operators“shorthands”in terms of the existing ones. If the existing language is relationally complete,
then such extensions do not increase the language’s expressive powerthere is no need for thatbut,
judiciously chosen, they do make some problems easier to solve by providing shorthands that are not only
convenient but, by raising the level of abstraction, might also be easier to understand than the longhands
on which they are defined. In Chapter 5 I illustrated this point by showing you the handful of such
operators that have been “judiciously chosen” for Tutorial D, these having been proposed by various
writers over the years. Unfortunately, very little in the way of useful shorthands has been proposed for use
in constraints; and what little there is is subject to a certain amount of controversy. Yet the requirement for
shorthands seems to be compelling, not just for the convenience of users but also for performance, as I
will now explain.
Suppose that we require every constraint to be expressed using an expression of the form IS_EMPTY(r).
Then consider a simple constraint such as the one to make sure every exam mark is in the range of 0 to
100 and assume it is expressed as shown in either of Examples 6.8 and 6.9. Suppose that a certain update
statement is used to add a single tuple to EXAM_MARK. Whether IS_EMPTY or aggregate AND is chosen
for the constraint declaration, a naïve evaluation would involve the system in examining each existing
EXAM_MARK tuple as well as the one being added. But the existing tuples are all known to satisfy the
condition Mark ~ 0 AND Mark x 100, for if one of them didn’t the database would have been
visibly inconsistent at the previous statement boundary. If the system could somehow work out that it is
sufficient just to check incoming tuples, then simple update operations would be executed very much
more quickly. But such optimizations involve sophisticated expression analysis. While we rightly expect
industrial-strength DBMSs to attempt such optimizations, their degree of success is likely to be limited in
practice. When we can identify a certain class of constraints that lend themselves to more efficient
methods of evaluation, one way of guaranteeing that the system will adopt those more efficient methods is
to provide an alternative way of expressing the constraint, applicable only to constraints of that class. If
that alternative method is easier for the user to write, and perhaps clearer for the reader too, then the
addition to the language can be justified even though it is theoretically redundant.