1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.

Slides:



Advertisements
Similar presentations
Relational Algebra Chapter 4, Part A
Advertisements

Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
The Relational Calculus
Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
1 TRC vs DRC 한욱신. 2 Queries in TRC and DRC  TRC Q = {t| f(t)} where t is a (free) tuple variable and f(t) is a well-formed formula  DRC.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus.
1 Lecture 5: Relational calculus
Relational Calculus CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of.
Relational Calculus CS 186, Fall 2005 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Relational Calculus R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey.
Introduction to Database Systems 1 Relational Calculus Relational Model : Topic 2.
Relational Calculus CS 186, Fall 2003, Lecture 6 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
Rutgers University Relational Calculus 198:541 Rutgers University.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Section 4.3.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4.
1 Relational Algebra. 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of data from a database. v Relational model supports.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
Database Management Systems,1 Relational Calculus.
Relational Calculus. Non Procedural or Declarative Calculus has variables, constants, comparison ops, logical connectives and quantifiers. There are TWO.
Relational Calculus R&G, Chapter 4. Relational Calculus Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). Calculus.
Relational Calculus CS 186, Spring 2005, Lecture 9 R&G, Chapter 4   We will occasionally use this arrow notation unless there is danger of no confusion.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)
IST 210 The Relational Language Todd S. Bacastow January 2004.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Copyright © Curt Hill The Relational Calculus Another way to do queries.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
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.
CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
1 CS122A: Introduction to Data Management Lecture 9 SQL II: Nested Queries, Aggregation, Grouping Instructor: Chen Li.
1 Relational Calculus ♦ Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). ♦ Calculus has variables, constants,
1 Relational Algebra. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational model supports.
Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
A First Course in Database Systems
COP Introduction to Database Structures
Relational Calculus Chapter 4, Part B
Basic SQL Lecture 6 Fall
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Calculus.
SQL: The Query Language Part 1
SQL: Structured Query Language
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
Relational Calculus Chapter 4, Part B 7/1/2019.
SQL: The Query Language (Part III)
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B

2 Copyright © Kyu-Young Whang Relational Calculus  Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus has variables, constants, comparison ops, logical connectives and quantifiers.  TRC : Variables range over (i.e., get bound to) tuples.  DRC : Variables range over domain elements (= field values).  Both TRC and DRC are simple subsets of first-order logic.  Expressions in the calculus are called formulas. An answer tuple is essentially an assignment of constants to variables that make the formula evaluate to true.

3 Copyright © Kyu-Young Whang Tuple Relational Calculus  Queries in tuple relational calculus : {t|  (t)}  t: tuple variable   (t): well-formed formula (wff) = conditions  t is the free variable in  (t)  More intuitively, {t 1 | cond(t 1, t 2, …, t n )}, where t 1 : free variable and t 2,.., t n : bound variables  Well-formed formula  Atomic formulas connected by logical connectives, AND, OR, NOT and quantifiers,  (existential quantifier),  (universal quantifier)

4 Copyright © Kyu-Young Whang Atomic formula in TRC 1. R(s)  R is a relation name  s is a tuple variable 2. t i [A]  t j [B]   : comparison operator (=,, , ,  )  t i, t j : tuple variables  A, B: attributes 3. t i [A]  constant

5 Copyright © Kyu-Young Whang Example: TRC query  Consider two relations  EMP(Name, MGR, DEPT, SAL)  CHILDREN(Ename, Cname, Age)  Q1: Retrieve Salary and Children ’ s name of Employees whose manager is ‘ white ’ {r|(  e)(  c)(EMP(e) AND CHILDREN(c) AND /* initiate tuple variables */ e[Name] = c[Name] AND /* join condition */ e[MGR] = ‘ white ’ AND /* selection cond. */ r[1 st attr] = e[SAL] AND r[2 nd attr] = c[Cname] }/* projection */

6 Copyright © Kyu-Young Whang Find the names and ages of sailors with a rating above 7 {p|(  s)(Sailors(s) AND /* initiate tuple variables */ s[rating] > 7 AND /* selection condition */ p[1 st attr] = s[sname] AND p[2 nd attr] = s[age]) }/* projection */

7 Copyright © Kyu-Young Whang Find the sailor name, boat id, and reservation date for each reservation {p|(  r)(  s)(Reserves(r) AND Sailors(s) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ p[1 st attr] = s[sname] AND p[2 nd attr] = r[bid] AND p[3 rd attr] = r[day] }/* projection */

8 Copyright © Kyu-Young Whang Find the names of sailors who have reserved boat 103 {p|(  r)(  s)(Reserves(r) AND Sailors(s) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ r[bid] = 103 AND /* selection condition */ p[1 st attr] = s[sname] }/* projection */

9 Copyright © Kyu-Young Whang Find the names of sailors who have reserved a red boat {p| (  r)(  s)(  b)(Reserves(r) AND Sailors(s) AND Boats(b) AND /* initiate tuple variables */ r[sid] = s[sid] AND /* join condition */ b[bid] = r[bid] AND /* join condition */ b[color] = ‘ red ’ AND /* selection condition */ p[1 st attr] = s[sname] }/* projection */

10 Copyright © Kyu-Young Whang Find the names of sailors who have reserved at least two boats {p| (  s)(  r1)(  r2)(Sailor(s) AND Reserves(r1) AND Reserves(r2) AND /* initiate tuple variables */ s[sid] = r1[sid] AND /* join condition */ r1[sid] = r2[sid] AND r1[bid]  r2[bid] AND /* self-join conditions */ p[1 st attr] = s[sname] }/* projection */

11 Copyright © Kyu-Young Whang Equivalent Query in Relational Algebra  See page 115.

12 Copyright © Kyu-Young Whang Find sailors who have reserved all red books {s| Sailors(s) AND (  b)(NOT Boats(b) OR /* initiate tuple variables */ b[color] = ‘ red ’  ( (  r)(Reserves(r) AND r[bid] = b[bid] AND s[1 st attr] = r[sid])))}/* projection */

13 Copyright © Kyu-Young Whang Another representation {s| Sailors(s) AND (  b)(NOT Boats(b) OR /* initiate tuple variables */ b[color]  ‘ red ’ OR ( (  r)(Reserves(r) AND r[bid] = b[bid] AND s[1 st attr] = r[sid])))}/* projection */

14 Copyright © Kyu-Young Whang Domain Relational Calculus  Queries in domain relational calculus : {x 1, x 2,..., x k |  (t)}  x 1, x 2, …, x k : domain variables; these are only free variables in  (t)   (t): well-formed formula (wff) = conditions  Well-formed formula  Atomic formulas connected by logical connectives, AND, OR, NOT and quantifiers,  (existential quantifier),  (universal quantifier)

15 Copyright © Kyu-Young Whang Atomic formula in DRC  R(x 1, x 2, …, x k )  R is a k-ary relation  x i : domain variable or constant  x  y   : comparison operator (=,, , ,  )  x, y: domain variables  A, B: attributes  x  constant

16 Copyright © Kyu-Young Whang DRC Examples  Consider two relations  EMP(Name, MGR, DEPT, SAL)  CHILDREN(Ename, Cname, Age)  Q1: Retrieve Salary and Children ’ s name of Employees whose manager is ‘ white ’ {q,s|(  u)(  v)(  w)(  x)(  y)(EMP(u,v,w,q) AND CHILDREN(x,s,y) AND /* initiate domain variables */ u = x AND /* join condition */ v = ‘ white ’ }/* selection condition */ /* projection is implied (q, s) */

17 Copyright © Kyu-Young Whang Find all sailors with a rating above 7 {i,n,t,a| Sailors(i,n,t,a) AND t > 7}

18 Copyright © Kyu-Young Whang Find sailors rated > 7 who ’ ve reserved boat #103 {i,n,t,a|(  ir)(  br)(  d) (Sailors(i,n,t,a) AND Reserve(ir,br,d) AND ir = i AND /* join condition */ t > 7 AND br = 103)}/* selection condition */

19 Copyright © Kyu-Young Whang Find sailors rated > 7 who ’ ve reserved a red boat {i,n,t,a| (  ir)(  br)(  d) (Sailors(i,n,t,a) AND (Reserve(ir,br,d) AND t>7 AND (  b)(  bn)(  c)(Boats(b,bn,c) AND b = br AND c= ‘ red ’ ) )}

20 Copyright © Kyu-Young Whang Find sailors who ’ ve reserved all boats  {i,n,t,a|Sailers(i,n,t,a) AND (  b)(  bn)(  c)(NOT Boats(b,bn,c) OR (  ir) (  br)(  d)(Reserves(ir,br,d) AND i = ir AND br = b) ) )}

21 Copyright © Kyu-Young Whang Unsafe Queries, Expressive Power  It is possible to write syntactically correct calculus queries that have an infinite number of answers! Such queries are called unsafe.  e.g.,  It is known that every query that can be expressed in relational algebra can be expressed as a safe query in DRC / TRC; the converse is also true.  Relational Completeness : Query language (e.g., SQL) can express every query that is expressible in relational algebra/calculus.

22 Copyright © Kyu-Young Whang Summary  Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.)  Algebra and safe calculus have same expressive power, leading to the notion of relational completeness.

23 Copyright © Kyu-Young Whang Exercises  4.3, 4.6