Download presentation
Presentation is loading. Please wait.
Published byBetty Hardy Modified over 9 years ago
1
Chapter 8 Relational Calculus
2
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra Computational Capabilities SQL Facilities Domain Calculus Query-By-Example
3
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-3 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
4
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-4 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)
5
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-5 Tuple Calculus - Syntax := RELATION { } | | -- “relation operator invoked” | -- “with expression” | | ( )
6
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-6 Tuple Calculus - Syntax Identical to the syntax of the algebra Relation operator invoked now is interpreted to mean relation definition In addition, -- “range variable definition” ::= RANGEVAR RANGES OVER ;
7
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-7 Tuple Calculus - WFFs 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
8
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-8 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;
9
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-9 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
10
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-10 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
11
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-11 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
12
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-12 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
13
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-13 Relational Operations in the calculus context is more a definition than an operator invocation ::= [WHERE ] For example: SX.S# WHERE SX.CITY = ‘London’ -- Get supplier numbers for suppliers in London
14
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-14 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
15
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-15 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) ;
16
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-16 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
17
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-17 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.