Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
Advertisements

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.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A Modified by Donghui Zhang.
1 Relational Calculus Chapter 4 – Part II. 2 Formal Relational Query Languages  Two mathematical Query Languages form the basis for “real” languages.
Relational Algebra Content based on Chapter 4 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
1 Relational Algebra & Calculus. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian.
R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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.
Exercises Find the names of sailors who’ve reserved boat #103 { N |  S  Sailors (S.name = N.name   R  Reserves(S.sid = R.sid  R.bid = 103)) }
Introduction to SQL Basics; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Introduction to SQL Basics --- SQL in 45 Minutes Chapter 5.
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.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.
CMPT 258 Database Systems SQL Queries (Chapter 5).
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
1 Relational Calculus ♦ Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC). ♦ Calculus has variables, constants,
Relational Algebra. CENG 3512 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
COP Introduction to Database Structures
© פרופ' יהושע שגיב, האוניברסיטה העברית
Relational Calculus Chapter 4, Part B
SQL The Query Language R & G - Chapter 5
Basic SQL Lecture 6 Fall
Introduction to Database Systems
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra.
CS 405G: Introduction to Database Systems
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
CENG 351 File Structures and Data Managemnet
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:

Relational Calculus Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 4

RA and RC RA is procedural – must apply appropriate operators in appropriate order. RC is declarative – describe what you want rather than specifying particular operations. Their expressive power is identical. A database language is relationally complete if it has at least the retrieval power of relational calculus. Relational algebra is used as the yardstick for database languages because it is an easier yardstick.

Tuple Relational Calculus Tuple variable ranges over tuples of particular relation; i.e., its value can be the value of any tuple in relation. Query: Specify tuple variable and condition or formula that describes that variable. {T | p(T)}

Example Retrieve those employees whose salary is over $50,000. {E | E  Employee  E.Salary > 50000}

Example (continued) Similar to SQL query: Select * From Employee Where Salary > 50000; “Employee” is both name of relation and name of tuple variable ranging over that relation. Compare to QUEL: Range of E is Employee Retrieve (E.all) Where E.Salary > 50000

Formulas Language for writing formulas is subset of predicate logic. Let Rel be a relation name, R and S be tuple variables, a be an attribute of R, and b be an attribute of S. Let op denote an operator in the set {<, >, =, ≤, ≥, ≠}.

Formulas (continued) An atomic formula is one of the following: R  Rel R.a op S.b R.a op constant, or constant op R.a A formula is recursively defined to be one of the following, where p and q are formulas and p(R) denotes a formula in which the variable R appears: any atomic formula ¬p, p  q, p  q, or p⇒ q ∃R(p(R)), where R is a tuple variable ∀R(p(R)), where R is a tuple variable

Queries and Answers A query is an expression of form {T | p(T)} where T is the only free variable in the formula p. The answer to a query is the set of all tuples t for which the formula p(T) evaluates to true with variable T assigned to tuple value t.

Simple Notation for Quantifiers Textbook has simplified notation for ∃ and ∀: ∃R  Rel(p(R)) instead of ∃R(R  Rel  p(R)) ∀R  Rel(p(R)) instead of ∀R(R  Rel ⇒ p(R))

Tuple Calculus Examples Examples in textbook use the following relations: Sailors (sid, sname, rating, age) Boats (bid, bname, color) Reserves (sid, bid, day) Sample tuples are shown on page 111.

(Q12) Find names & ages of sailors with rating > 7. {P | ∃S  Sailors (S.rating > 7  P.name = S.name  P.age = S.age} (Q13) Find the sailor name, boat id, and reservation date for each reservation. {P | ∃R  Reserves ∃S  Sailors (R.sid = S.sid  P.bid = R.bid  P.day = R.day  P.sname = S.sname)}

(Q1) Find names of sailors who have reserved boat 103. {P | ∃S  Sailors ∃R  Reserves (R.sid = S.sid  R.bid = 103  P.sname = S.sname} (Q2) Find the names of sailors who have reserved a red boat. {P | ∃S  Sailors ∃R  Reserves ∃B  Boats (R.sid = S.sid  B.bid = R.bid  B.color = ‘red’  P.sname = S.sname)}

(Q7) Find names of sailors who have reserved at least two boats. {P | ∃S  Sailors ∃R1  Reserves ∃R2  Reserves (S.sid = R1.sid  R1.sid = R2.sid  R1.bid ≠ R2.bid  P.sname = S.sname)} (Q9) Find the names of sailors who have reserved all boats. {P | ∃S  Sailors ∀B  Boats (∃R  Reserves (S.sid = R.sid  R.bid = B.bid  P.sname = S.sname))}

(Q14) Find sailors who have reserved all red boats. If boat is red, sailor must have reserved it. {S | S  Sailors  ∀B  boats (B.color = ‘red’ ⇒ (∃R  Reserves (S.sid = R.sid  R.bid = B.bid))} Recall: p ⇒ q is equivalent to ¬p  q. {S | S  Sailors  ∀B  Boats (B.color ≠ ‘red  (∃R  Reserves (S.sid = R.sid  R.bid = B.bid)))}

(Q14) Find sailors who have reserved all red boats. Common error on this type of query: {S | S  Sailors  ∀B  Boats (B.color = ‘red  (∃R  Reserves (S.sid = R.sid  R.bid = B.bid)))}

Domain Relational Calculus A domain variable is a variable that ranges over the values in the domain of some attribute.

Formulas Let op denote an operator in the set {<, >, =, ≤, ≥, =, ≠} and let X and Y be domain variables. An atomic formula is one of the following: <x1, x2, …, xn>  Rel, where Rel is a relation with n attributes; each xi, 1 ≤ i ≤ n, is either a variable or a constant X op Y X op constant, or constant op X

Formulas (continued) A formula is recursively defined to be one of the following, where p and q are formulas and p(X) denotes a formula in which the variable X appears: any atomic formula ¬p, p  q, p  q, or p ⇒ q ∃X (p(X)), where X is a domain variable ∀X (p(X)), where X is a domain variable

Domain Calculus Examples (Q11) Find all sailors with rating above 7. {<I, N, T, A> | <I, N, T, A>  Sailors  T > 7} (Q1) Find names of sailors who have reserved boat 103. {<N> | ∃I, T, A (<I, N, T, A>  Sailors  ∃Ir, Br, D(<Ir, Br, D>  Reserves  Ir = I  Br = 103))} Note: Only N is a free variable. Also note notation: ∃Ir, Br, D(…) is shorthand for: ∃Ir (∃B (∃D(…) Even more compact form preferred in textbook: ∃<Ir, Br, D>  Reserves

(Q2) Find names of sailors who have reserved a red boat. {<N> | ∃I, T, A (<I, N, T, A>  Sailors  ∃<I, Br, D>  Reserves  ∃<Br, BN, ‘red>  Boats)} (Q7) Find names of sailors who have reserved at least two boats. {<N> | ∃I, T, A (<I, N, T, A>  Sailors  ∃Br1, Br2, D1, D2 (<I, Br1, D1>  Reserves  <I, Br2, D2>  Reserves  Br1 ≠ Br2))} How do we know that the same sailor has reserved both boats in question?

(Q9) Find names of sailors who have reserved all boats. {<N> | ∃I, T, A (<I, N, T, A>  Sailors  ∀B, BN, C (¬ (<B, BN, C>  Boats)  (∃<Ir, Br, D>  Reserves (I = Ir  Br = B))))} Alternate solution: {<N> | ∃I, T, A (<I, N, T, A>  Sailors  ∀<B, BN, C>  Boats) (∃<Ir, Br, D>  Reserves (I = Ir  Br = B)))}

(Q14) Find sailors who have reserved all red boats. {<I, N, T, A> | <I, N, T, A>  Sailors  ∀<B, BN, C>  Boats (C = ‘red’ ⇒ ∃<Ir, Br, D>  Reserves (I = Ir  Br = B))} I.e., find all sailors such that, for every red boat, there is a tuple in Reserves that shows the sailor has reserved it.