Date`s An Introduction to Database Systems, 8th ed

Chapter 8
Relational Calculus
Topics in this Chapter
•
•
•
•
•
•
Tuple Calculus
Calculus vs. Algebra
Computational Capabilities
SQL Facilities
Domain Calculus
Query-By-Example
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-2
Relational Calculus
• Based on predicate calculus, the relational
calculus is a more natural language expression
of the relational algebra
• Instead of operators used by the system to
construct a result relation, the calculus offers a
notation to express the result relation in terms
of the source relations
• Relational calculus and algebra are logically
equivalent
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-3
Relational Calculus Implementations
• Codd proposed a language called ALPHA to
implement the relational calculus
• QUEL, an early competitor to SQL, was
based on ALPHA
• Relational calculus came to be called tuple
calculus, in distinction from domain calculus
• Domain calculus has been implemented by
Query By Example (QBE)
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-4
Tuple Calculus - Syntax
<relation expression>
:= RELATION {<tuple expression commalist>}
| <relvar name>
| <relation op inv> -- “relation operator
invoked”
| <with exp> -- “with expression”
| <introduced name>
| ( <relation exp>)
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-5
Tuple Calculus - Syntax
• Identical to the syntax of the algebra
• Relation operator invoked now is interpreted
to mean relation definition
• In addition,
<range var def> -- “range variable
definition”
::= RANGEVAR <range var name>
RANGES OVER <relation exp
commalist> ;
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-6
Tuple Calculus - WFFs
•
<bool exp>s are called well-formed
formulas, WFFs, or “weffs”
• Every reference to a range variable is either
free or bound, within a context, and in
particular, within a WFF
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-7
Range Variables
•
•
•
•
•
RANGEVAR SX RANGES OVER S;
RANGEVAR SY RANGES OVER S;
RANGEVAR SPX RANGES OVER SP;
RANGEVAR SPY RANGES OVER SP;
RANGEVAR PX RANGES OVER P;
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-8
Range Variables
• RANGEVAR SU RANGES OVER
( SX WHERE SX.CITY = ‘London’ )
( SX WHERE EXISTS SPX
(SPX.S# = SX.S# AND
SPX.P# = P# (‘P1’) ) ) ;
• In this example, SU ranges over the union of
the set of supplier tuples for suppliers located
in London, and those that supply P1
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-9
Free and Bound Variables
• Every reference to a range variable is either
free or bound
• A bound reference can be replaced by a
reference to some other variable without
changing the meaning of the expression
• A free reference is not so free
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-10
Quantifiers - EXISTS
• EXISTS is the existential quantifier
• EXISTS V ( p ) -- There exists at least one
value of V that makes p true
• Formally this is an iterated OR
• FALSE OR p (t1) OR … OR p (tm)
-- will evaluate to false if m = 0
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-11
Quantifiers - FORALL
• FORALL is the universal quantifier
• FORALL V ( p ) -- for all values of v, p is
true
• Formally this is an iterated AND
• TRUE AND p (t1) AND … AND p (tm)
-- will evaluate to true as long as all are true
• This will evaluate to TRUE when the set is
empty
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-12
Relational Operations
• <relation op inv> in the calculus context is
more a definition than an operator invocation
• <relation op inv>
::= <proto tuple> [WHERE <bool exp>]
• For example:
• SX.S# WHERE SX.CITY = ‘London’
-- Get supplier numbers for suppliers in London
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-13
Calculus vs. Algebra
• Codd’s reduction algorithm reduces
expressions of the calculus to algebra
• A language is said to be relationally complete
if it is at least as powerful as the calculus
• And the same can be said of the algebra
• QUEL, based on the calculus, can be
implemented by applying the algorithm to it,
and then implementing the underlying algebra
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-14
SQL Facilities
• Because the calculus statements can be
translated into algebra, they map equally well
to SQL
• Example: Get supplier numbers for suppliers
with status less than the current maximum
status in the supplier table:
SELECT S.S# FROM S WHERE
S.STATUS < (SELECT MAX (S.STATUS)
FROM S) ;
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-15
Domain Calculus
• Its range variables range over domains (i.e.
types) instead of relations
• Based on checking values in the attribute
domain, a bool membership condition
• SP { S#, S#(‘S1’), P# P#(‘P1’) }
-- evaluates to true iff the shipment contains
part P1 shipped by supplier S1
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-16
Query-By-Example (QBE)
• Language based on the domain calculus
• Notation which is intuitive and easy to use
• By making entries in blank tables, the user
specifies the conditions needed in the result
• A condition box is used to allow more
complex conditions
• Easy to express comparisons, uniqueness,
AND and OR, and EXISTS
• Doesn’t express NOT EXISTS well, and so is
not relationally complete
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.
8-17