Relational Calculus.

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.
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.
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
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.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
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.
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 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.
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.
SQL II.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
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
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
Relational Calculus Chapter 4, Part B
SQL The Query Language R & G - Chapter 5
Basic SQL Lecture 6 Fall
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
CS 405G: Introduction to Database Systems
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:

Relational Calculus

Another Theoretical QL-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. Like SQL. DRC: Variables range over domain elements (= field values). Like Query-By-Example (QBE) Both TRC and DRC are simple subsets of first-order logic. We’ll focus on TRC here Expressions in the calculus are called formulas. Answer tuple is an assignment of constants to variables that make the formula evaluate to true.

Tuple Relational Calculus Query has the form: {T | p(T)} p(T) denotes a formula in which tuple variable T appears. Answer is the set of all tuples T for which the formula p(T) evaluates to true. Formula is recursively defined: start with simple atomic formulas (get tuples from relations or make comparisons of values) build bigger formulas using logical connectives.

TRC Formulas An Atomic formula is one of the following: R  Rel R[a] op S[b] or R.a =S.b R[a] op constant where R[a] denotes attr.a of Rel R. Where op is one of A formula can be: an atomic formula where p and q are formulas where variable R is a tuple variable

DB Formula F Result Relational Calculus If TRUE i.e., in DB Is predicut true for ths tuple DB (smith, A101, 1000) Formula F Result F (name, acct-no, Amt) If TRUE i.e., in DB F (smith, A101, 1000)

Free and Bound Variables Quantifiers and in a formula are said to bind X in the formula. A variable that is not bound is free. Let us revisit the definition of a query: {T | p(T)} Important restriction the variable T that appears to the left of `|’ must be the only free variable in the formula p(T). in other words, all other tuple variables must be bound using a quantifier.

Sailors (sid, sname, age, rating) Boats (bid, color) Example Schema Sailors (sid, sname, age, rating) Boats (bid, color) Reserves (sid, bid)

Selection and Projection Find all sailors with rating above 7 Modify this query to answer: Find sailors who are older than 18 or have a rating under 9, and are named ‘Bob’. Find names and ages of sailors with rating above 7. Note: S is a tuple variable with 2 attributes (i.e. {S} is a projection of Sailors) only 2 attributes are ever mentioned and S is never used to range over any relations in the query. {S | S Sailors  S[rating] > 7} {S | S1 Sailors(S1[rating] > 7  S[sname] = S1[sname]  S[age] = S1[age])}

{S | SSailors  S[rating] > 7  RReserves Joins Find sailors and their rating for sailors rated > 7 who’ve reserved boat #103 Note the use of  to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. {S | SSailors  S[rating] > 7  RReserves (R[sid] = S[sid]  R[bid] = 103)}

{S | SSailors  S[rating] > 7  RReserves (R[sid] = S[sid] Joins (continued) Find sailors rated > 7 who’ve reserved a red boat {S | SSailors  S[rating] > 7  RReserves (R[sid] = S[sid]  BBoats (B[bid] = R[bid]  B[color] = ‘red’))} This may look cumbersome, but it’s not so different from SQL!

Division (makes more sense here???) Find sailors who’ve reserved all boats (hint, use ) {S | SSailors  BBoats (RReserves (S[sid] = R[sid]  B[bid] = R[bid]))} Find all sailors S such that for all tuples B in Boats there is a tuple in Reserves showing that sailor S has reserved B.

Unsafe Queries, Expressive Power  syntactically correct calculus queries that have an infinite number of answers! Unsafe queries. e.g., Solution???? Don’t do that! Expressive Power (Theorem due to Codd): 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. (actually, SQL is more powerful, as we will see…)

Tuple Relational Calculus Join Queries Find the names of customers w/ loans at the Perry branch. Answer has form {t | P(t)}. Strategy for determining P(t): 1. What tables are involved? borrower (s), loan (u) 2. What are the conditions? (a) Projection: t [cname] = s [cname] (b) Join: s [lno] = u [lno] (c) Selection: u [bname] = “Perry”

Tuple Relational Calculus Join Queries Find the names of customers w/ loans at the Perry branch. A. {t |  s  borrower (P(t,s))} such that: P(t,s) º t [cname] = s [cname]   u  loan (Q(t,s,u)) Q(t,s,u) º s [lno] = u [lno]  u [bname] = “Perry” OR unfolded version (either is ok) {t |  s  borrower ( t [cname] = s [cname]   u  loan (s [lno] = u [lno]  u [bname] = “Perry”))}