Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.

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.
Relational Algebra Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
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.
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.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
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.
Introduction to Database Systems 1 Relational Algebra Relational Model: Topic 3.
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 Algebra Chapter 4 - part I. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.  Relational.
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)) }
Relational Algebra, R. Ramakrishnan and J. Gehrke (with additions by Ch. Eick) 1 Relational Algebra.
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.
Database Management Systems,1 Relational Calculus.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Lecture#6: 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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
IST 210 The Relational Language Todd S. Bacastow January 2004.
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.
1 SQL: The Query Language (Part II). 2 Expressions and Strings v Illustrates use of arithmetic expressions and string pattern matching: Find triples (of.
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 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
Relational Calculus Chapter 4, Part B
SQL The Query Language R & G - Chapter 5
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.
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides

Relational Calculus Tuple Relational Calculus (TRC)  Variables in TRC take on tuples as values. Domain Relational Calculus (DRC)  Variables in DRC range over field values.

Tuple Relational Calculus Query has the form: {T | p(T)}  T is a tuple variable.  p(T) is a formula containing T. Answer to the Query = the set of all tuples t for which p(T = t) = true.

Syntax of TRC Queries Atomic formula: T  Relation T.a op T.b T.a op constant … op is one of A formula can be:  an atomic formula  , where R is a tuple variable

Free and Bound Variables Quantifiers:  and  Use of or binds X.  A variable that is not bound is free. Recall our definition of a query:  {T | p(T)} Important restriction: — T must be the only free variable in p(T). — all other variables must be bound using a quantifier.

Some Notations  T  Relation ( p ( T )) represents   T ( T  Relation  ( p ( T ))  T  Relation ( p ( T )) represents   T ( T  Relation  ( p ( T ))

Simple Queries Find all sailors with rating above 7 Find names and ages of sailors with rating above 7.  Note: S is a variable of 2 fields (i.e. S is a projection of Sailors) { S |S  Sailors  S.rating > 7} { S |  S1  Sailors ( S1.rating > 7  S.sname = S1.sname  S.age = S1.age )}

Find sailors with rating above 7 who have reserved boat #103 { S | S  Sailors  S.rating > 7   R(R  Reserves  R.sid = S.sid  R.bid = 103) } Joins

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

Universal Quantification Find sailors who’ve reserved all boats { S | S  Sailors   B  Boats (  R  Reserves (S.sid = R.sid  B.bid = R.bid)) }

A trickier example … { S | S  Sailors   B  Boats ( B.color = ‘red’   R(R  Reserves  S.sid = R.sid  B.bid = R.bid)) } Find sailors who’ve reserved all Red boats { S | S  Sailors   B  Boats ( B.color  ‘red’   R(R  Reserves  S.sid = R.sid  B.bid = R.bid)) } Alternatively…

a  b is the same as  a  b a T F T F b T TT F

A Remark: Unsafe Queries  syntactically correct calculus queries that have an infinite number of answers! Unsafe queries.  e.g.,  Solution???? Don’t do that!

Expressive Power Expressive Power (Theorem due to Codd):  Every query that can be expressed in relational algebra can be expressed as a safe query in relational calculus; 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…)

Summary Formal query languages — simple and powerful.  Relational algebra is operational used as internal representation for query evaluation plans.  Relational calculus is “declarative” query = “what you want”, not “how to compute it”  Same expressive power --> relational completeness. Several ways of expressing a given query  a query optimizer should choose the most efficient version.