Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Relational Calculus.

Similar presentations


Presentation on theme: "Chapter 8 Relational Calculus."— Presentation transcript:

1 Chapter 8 Relational Calculus

2 Computational Capabilities SQL Facilities Domain 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.

3 Relational calculus and algebra are logically equivalent
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.

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) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

5 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.

6 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.

7 <bool exp>s are called well-formed formulas, WFFs, or “weffs”
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 RANGEVAR SX RANGES OVER S; RANGEVAR SY RANGES OVER S;
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.

9 RANGEVAR SU RANGES OVER ( SX WHERE SX.CITY = ‘London’ ),
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.

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 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

11 EXISTS is the existential quantifier
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.

12 FORALL is the universal quantifier
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.

13 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.

14 Examples (1) 8.3.1 Get supplier numbers and status for suppliers in Paris with status > 20: {SX.S#, SX.STATUS} WHERE SX.CITY = ‘PARIS’ AND SX.STATUS > 20 8.3.2 Get all pairs of supplier numbers such that the suppliers concerned are colocated (ex 7.5.5) {SX.S# AS SA, SY.S# AS SB} WHERE SX.CITY = SY.CITY AND SX.S# < SY.S# 8.3.3 Get full supplier information for suppliers who supply part P2 ( ex 7.5.1) SX WHERE EXISTS SPX (SPX.S# = SX.S# AND SPX.P# = P#(‘P2’) ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

15 Examples (2) 8.3.4 Get supplier names for suppliers who supply at least one red part (ex 7.5.2): SX.SNAME WHERE EXISTS SPX (SX.S# = SPX.S# AND EXISTS PX (PX.P# = SPX.P# AND PX.COLOR = COLOR (‘Red’))) ≡ (in prenex normal form, all quantifiers appears at the front of WFF) SX.SNAME WHERE EXISTS SPX EXISTS PX (SX.S# = SPX.S# AND SPX.P# =PX.P# AND PX.COLOR = COLOR (‘Red’)) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

16 Examples (3) 8.3.5 Get supplier names for suppliers who supply at least one part supplied by supplier S2: SX.SNAME WHERE EXISTS SPX (EXISTS SPY (SX.S#=SPX.S# AND SPX.P#=SPY.P# AND SPY.S# = S#(‘S2’))) 8.3.6 Get supplier names for suppliers who supply all parts (ex ) SX.SNAME WHERE FORALL PX (EXISTS SPX (SPX.S# = SX.S# AND SPX.P# = PX.P#)) ≡ (Without using FORALL) SX.SNAME WHERE NOT EXISTS PX (NOT EXISTS SPX (SPX.S# = SX.S# AND SPX.P# = PX.P#)) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

17 Examples (4) 8.3.7 Get supplier names for suppliers who do not supply part P2 (ex ): SX.SNAME WHERE NOT EXISTS SPX (SPX.S# = SX.S# AND SPX.P# = P#(‘P2’)) 8.3.8 Get supplier number for suppliers who supply at least all those parts supplied by supplier S2 (ex ): SX.S# WHERE FORALL SPX (SPX.S# ≠ S#(‘S2’) OR EXISTS SPY (SPY.S# = SX.S# AND SPY.P# = SPX.P#)) ≡ (using the logical implication IF p THEN q END IF ≡ (NOT p) OR q ) SX.S# WHERE FORALL SPX (IF SPX.S# = S#(‘S2’) THEN EXISTS SPY (SPY.S# = SX.S# AND SPY.P# = SPX.P#) END IF ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

18 Examples (5) 8.3.9 Get part numbers for parts that either weigh more than 16 pounds or are supplied by supplier S2, or both: RANGEVAR PU RANGES OVER (PX.P# WHERE PX.WEIGHT > WEIGHT (16.0)), (SPX.P# WHERE SPX.S# = S# (‘S2’)); PU.P# ≡ PX.P# WHERE PX.WEIGHT > WEIGHT (16.0) OR EXISTS SPX (SPX.P# = PX.P# AND SPX.S# = S#(‘S2’)) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

19 And the same can be said of the algebra
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.

20 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

21 Evaluation of Calculus expression
Ex: Get names and cities for suppliers who supply at least one Athens project with at least 50 of every part. {SX.SNAME, SX.CITY} WHERE EXISTS JX FORALL PX EXISTS SPJX (JX.CITY = ‘Athens’ AND JX.J# = SPJX.J# AND PX.P# = SPJX.P# AND SX.S# = SPJX.S# AND SPJX.QTY  QTY(50) ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

22 STEP 1:For each range variable, retrieve the range, restricted if possible.
SX: All tuples of S, 5 tuples PX: All tuples of P, 6 tuples JX: Tuples of J where CITY = ‘Athens’, 2 SPJX: Tuples of SPJ where QTY  QTY(50), 24 STEP 2: Construct the Cartesian product of the ranges retrieved in Step 1 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

23 Step 3: Restrict in accordance with the “join portion” of the WHERE clause, i.e.,
JX.J# = SPJX.J# AND PX.P# = SPJX.P# AND SX.S# = SPJX.S# AND Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

24 Apply the quantifiers from right to left
EXIST V (V is a range variable of relation r): project the result to eliminate all attributes of r FORALL V: divide the result by the “restricted range” relation in Step 1. It also eliminate the attribute of r Ex, EXIST SPJX: project away the attributes of SPJ Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

25 Step 5: Project the result as the proto tuple.
Ex, FORALL PX: Divide by P Ex, EXISTS JX: project away the attributes of J Step 5: Project the result as the proto tuple. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

26 SQL Facilities Because the calculus statements can be translated into algebra, they map equally well to SQL 8.6.1 Get color and city for “nonParis” parts with weight greater than 10 pounds: SELECT PX.COLOR, PX.CITY FROM P AS PX WHERE PX.CITY <> ‘Paris’ AND PX.WEIGHT > WEIGHT (10.0); 8.6.2 For all parts, get the part number and the weight of that part in grams (ex, 8.5.1) SELECT P.P#, P.WEIGHT * 454 AS GMWT FROM P; Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

27 8.6.3 Get all combinations of supplier and part information such that the supplier and part in question are colocated. SELECT S.*, P.P#, P.PNAME, P.COLOR, P.WEIGHT FROM S, P WHERE S.CITY = P.CITY; S JOIN P USING CITY; // SQL:1992 S NATURAL JOIN P ; // SQL:1992 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

28 8.6.4 Get all pairs of city names such that a supplier located in the first city supplies a part stored in the second city: SELECT DISTINCT S.CITY AS SCITY, P.CITY AS PCITY FROM S JOIN SP USING S# JOIN P USING P#; SELECT DISTINCT S.CITY AS SCITY, P.CITY AS PCITY FROM S NATURAL JOIN SP NATURAL JOIN P; // wrong!! CITY is included as a joining column in the second join 8.6.5 Get all pairs of supplier numbers such that the suppliers concerned are colocated (ex 8.3.2) SELECT A.S# AS SA, B.S# AS SB FROM S AS A, S AS B WHERE A.CITY = B.CITY AND A.S# < B.S# Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

29 8.6.6 Get the total number of suppliers
SELECT COUNT(*) AS N FROM S; Aggregate operators: COUNT, SUM, AVG, MAX, MIN, EVERY, and ANY 8.6.7 Get the maximum and minimum quantity for part P2: SELECT MAX (SP.QTY) AS MAXQ, MIN (SP.QTY) AS MINQ FROM SP WHERE SP.P# = P# (‘P2’); SELECT AVG ( SUM (SP.QTY)) FROM SP; // illegal, aggregate operator cannot be nested Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

30 8.6.3 For each part supplied, get the part number and the total shipment quantity:
SELECT SP.P#, SUM (SP.QTY) AS TOTQTY FROM SP GROUP BY SP.P#; Alternatively SELECT P.P#, (SELECT SUM (SP.QTY) FROM SP WHERE SP.P# = P.P#) AS TOTQTY FROM P; If the GROUP BY is specified, expressions in the SELECT clause must be single-valued per group Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

31 8.6.9 Get part numbers for parts supplied by more than one supplier:
SELECT SP.P# FROM SP GROUP BY SP.P# HAVING COUNT (SP.S#) > 1; Get supplier names for suppliers who supply part P2 (Ex, 7.5.1): SELECT DISTINCT S.SNAME FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# = P# (‘P2’)); Alternatively SELECT DISTINCT S.SNAME FROM S, SP WHERE S.S# = SP.S# AND SP.P# = P#(‘P2’); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

32 8.6.11 Get supplier names for suppliers who supply at least one red part (ex 8.3.4)
SELECT DISTINCT S.SNAME FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# IN (SELECT P.P# FROM P WHERE P.COLOR = COLOR(‘Red’))); 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.

33 8. 6. 13 Get supplier names for suppliers who supply part P2 (ex, 8. 6
SELECT DISTINCT S.SNAME FROM S WHERE EXISTS (SELECT * FROM SP WHERE SP.S# = S.S# AND SP.P# = P#(‘p2’)); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

34 8.6.14 Get supplier names for suppliers who do not supply part P2 (ex, 8.3.7)
SELECT DISTINCT S.SNAME FROM S WHERE NOT EXISTS (SELECT * FROM SP WHERE SP.S# = S.S# AND SP.P# = P# (‘P2’); Alternatively, SELECT DISTINCT S.SNAME FROM S WHERE S.S# NOT IN (SELECT SP.S# FROM SP WHERE SP.P# = P# (‘P2’); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

35 8. 6. 15 Get supplier names for suppliers who supply all parts (ex, 8
SELECT DISTINCT S.SNAME FROM S WHERE NOT EXISTS (SELECT * FROM P WHERE NOT EXISTS (SELECT * FROM SP WHERE SP.S# = S.S# AND SP.P# = P.P#)); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

36 Comparing two tables are illegal!
SELECT DISTINCT S.SNAME // illegal FROM S WHERE (SELECT SP.P# FROM SP WHERE SP.S# = S.S#) = (SELECT P.P# FROM P); Alternate approach SELECT DISTINCT S.SNAME FROM S WHERE (SELECT COUNT (SP.P#) FROM SP WHERE SP.S# = S.S#) = (SELECT COUNT (P.P#) FROM P) ; The second approach not exactly the same, but it works if referential integrity is in effect Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

37 Get part numbers for parts that either weigh more than 16 pounds or are supplied by supplier S2, or both: SELECT P.P# FROM P WHERE P.WEIGHT > WEIGHT (16.0) UNION SELECT SP.P# FROM SP WHERE SP.S# = S# (‘S2’) Redundant duplicate rows are eliminated from UNION, INTERSECT, or EXCEPT. Duplicates are retained from UNION ALL, INTERSECT ALL, and EXCEPT ALL Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

38 Get the part number and the weight in grams for each part with weight > grams (ex, 8.5.1) SELECT P.P#, P.WEIGHT*454 AS GMWT FROM P WHERE P.WEIGHT*454 > WEIGHT ( ); By using the WITH clause WITH T1 AS (SELECT P.P#, P.WEIGHT*454 AS GMWT FROM P) SELECT T1.P#, T1.GMWT FROM T1 WHERE T1.GMWT > WEIGHT ( ); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

39 SP { S# S#(‘S1’), P# P#(‘P1’) }
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.

40 SX // all supplier numbers
SX WHERE S {S# SX} // all supplier numbers in S SX WHERE S {S# SX, CITY ‘London’} // all supplier numbers whose city is London {SX, CITYX} WHERE S {S# SX, CITY CITYX} AND SP {S# SX, P# P# (‘P2’)} // Get supplier numbers and cities for suppliers // who supply part ‘P2’ {SX, PX} WHERE S {S# SX, CITY CITYX} AND P {P# PX, CITY CITYY} AND CITYX ≠ CITYY // Get supplier-number/part-number pair such that the // supplier and part are not colocated Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

41 8.7.1 Get supplier numbers for suppliers in Paris with status > 20
SX WHERE EXISTS STATUSX (STATUSX > 20 AND S {S# SX, STATUS STATUSX, CITY ‘Paris’} ) 8.7.2 Get all pairs of supplier numbers such that the suppliers concerned are colocated {SX AS SA, SY AS SB} WHERE EXISTS CITYZ (S {S# SX, CITY CITYZ} AND S {S# SY, CITY CITYZ} AND SX < SY) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

42 8.7.3 Get supplier names for suppliers who supply at least one red part
NAMEX WHERE EXISTS SX EXISTS PX (S {S# SX, SNAME NAMEX} AND SP {S# SX, P# PX} AND P {P# PX, COLOR COLOR(‘Red’) } ) 8.7.4 Get supplier names for suppliers who supply at least one part supplied by supplier S2 NAMEX WHERE EXISTS SX EXISTS PX (S {S# SX, SNAME NAMEX} AND SP {S# SX, P# PX} AND SP {S# S#(‘S2’), P# PX} ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

43 8.7.5 Get supplier names for suppliers who supply all parts
NAMEX WHERE EXISTS SX (S {S# SX, SNAME NAMEX} AND FORALL PX ( IF P {P# PX} THEN SP {S# SX, P# PX} END IF) 8.7.6 Get supplier names for suppliers who do not supply part P2: NAMEX WHERE EXISTS SX (S {S# SX, SNAME NAMEX} AND NOT SP {S# SX, P# P#(‘P2’) } ) Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

44 8.7.7 Get supplier numbers for suppliers who supply at least all those parts supplied by supplier S2: SX WHERE FORALL PX (IF SP {S# S#(‘S2’), P# PX} THEN SP {S# SX, P# PX} END IF ) 8.7.8 Get part numbers for parts that either weigh more than 16 pounds or are supplied by supplier S2, or both PX WHERE EXISTS WEIGHTX (P {P# PX, WEIGHT WEIGHTX} AND WEIGHTX > WEIGHT (16.0)) OR SP {S# S#(‘S2’), P# PX} Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

45 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.

46 Get supplier names for suppliers who supply at least one part supplied by supplier S2
Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

47 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

48 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

49 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

50 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

51 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

52 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

53 Copyright © 2004 Pearson Addison-Wesley. All rights reserved.


Download ppt "Chapter 8 Relational Calculus."

Similar presentations


Ads by Google