Chapter 8 Relational Calculus.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Database Languages Chapter 7. The Relational Algebra.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
Domains, Relations & Base RelVars (Ch
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
Chapter 8 Relational Calculus. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.8-2 Topics in this Chapter Tuple Calculus Calculus vs. Algebra.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter 5 Relational Algebra Pearson Education © 2014.
1 Algebra of Queries Classical Relational Algebra It is a collection of operations on relations. Each operation takes one or two relations as its operand(s)
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Integrity Prof. Yin-Fu Huang CSIE, NYUST Chapter 9.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Chapter 4 An Introduction to SQL.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Calculus Chapter 4, Section 4.3.
Chapter 8 Relational Calculus.
Relational Algebra & Calculus
CSE202 Database Management Systems
STRUCTURE OF PRESENTATION :
More SQL: Complex Queries,
Basic SQL Queries Go over example queries, like 10 > ALL.
Chapter 10 Views.
COP Introduction to Database Structures
UNIT 11 Query Optimization
Relational Algebra - Part 1
Relational Calculus Chapter 4, Part B
The Relational Algebra and Relational Calculus
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
The Relational Algebra and Relational Calculus
Relational Algebra Chapter 4 - part I.
پايگاه داده ها.
Φροντιστήριο SQL (από το βιβλίο του Date)
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Advanced Database System
SQL: Structured Query Language
Unit 7 Normalization (表格正規化).
STRUCTURE OF PRESENTATION :
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
Relational Calculus Chapter 4 – Part II.
Presentation transcript:

Chapter 8 Relational Calculus

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.

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.

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.

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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. 7.5.3) 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.

Examples (4) 8.3.7 Get supplier names for suppliers who do not supply part P2 (ex. 7.5.6): 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. 7.5.4): 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.

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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; 8.6.10 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.

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’))); 8.6.12 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. 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.

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.

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.

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.

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

8.6.17 Get the part number and the weight in grams for each part with weight > 10000 grams (ex, 8.5.1) SELECT P.P#, P.WEIGHT*454 AS GMWT FROM P WHERE P.WEIGHT*454 > WEIGHT (10000.0); 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 (10000.0); Copyright © 2004 Pearson Addison-Wesley. All rights reserved.

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.

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.

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.

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.

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.

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.

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.

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

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

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

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

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

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

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

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