Relational Calculus Chapter 4 – Part II.

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.
1 541: Relational Calculus. 2 Relational Calculus  Comes in two flavours: Tuple relational calculus (TRC) and Domain relational calculus (DRC).  Calculus.
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.
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
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.
1 Relational Algebra. 2 Relational Query Languages Query languages: Allow manipulation and retrieval of data from a database. Relational model supports.
Relational Calculus. Another Theoretical QL-Relational Calculus n Comes in two flavors: Tuple relational calculus (TRC) and Domain 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.
Rutgers University Relational Algebra 198:541 Rutgers University.
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.
CSCD343- Introduction to databases- A. Vaisman1 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.
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, 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.
1 Relational Algebra Chapter 4, Sections 4.1 – 4.2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
CSCD34-Data Management Systems - A. Vaisman1 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 Relational Algebra (Chapter 4)
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.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
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
Relational Calculus Chapter 4, Part B
Relational Algebra Chapter 4, Part A
Basic SQL Lecture 6 Fall
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra.
Relational Calculus.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
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.
Relational Algebra Chapter 4 - part I.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Relational Calculus Chapter 4 – Part II

Formal Relational Query Languages Two mathematical Query Languages form the basis for “real” languages (e.g. SQL), and for implementation: Relational Algebra: how to compute it , operational, Representing execution plans (inside) Relational Calculus: what you want, declarative. Influent SQL design (outside)

Relational Calculus Calculus has Comes in two flavours: variables, constants, comparison ops (<,>,=,,.), logical connectives ( ,) Quantifiers (,) Comes in two flavours: Tuple relational calculus (TRC) : Variables range over tuples. (SQL) Domain relational calculus (DRC). Variables range over domain elements. (QBE) Both formula in TRC and DRC are simple subsets of first-order logic.

Tuple Relational Calculus (TRC) Tuple variable: variable that takes on tuples of a relation schema as values. Query form: T = tuple variable p(T) = formula that describes T Result = set of all tuples t for which p(t) with T=t evaluates to be true Formula P(T) specified in First Order Logic

Find all sailors with a rating above 7 Variable S bound to a tuple in relation Sailors

TRC Formulas Formula: an atomic formula Or recursively defined Terms. R  Rel, R.a op S.b, R.a op constant, costant op R.a Or recursively defined p, pq, pq, pq , where p, q are formulas R(p(R)), where R is a tuple variable R(p(R)), where R is a tuple varialbe Terms. bind --- the quantifier  and  are bind to variable R. free --- a variable is free if no quantifier binds to it in a formula

Semantics of TRC Queries F is an atomic formula R  Rel, and R is assigned a tuple in the instance of relation Rel {S|SSailors} F is a comparison R.a op S.b, R.a op constant, or constant op R.a, and the tuples assigned to R and S have field names R.a and S.b that make the comparison true. F is of the form p, and p is not true; the form p  q, and both p and q are true; the form p  q, and one of them is true; the form p  q, and q is true whenever p is true. F is of the form R(p( R )), and there is some assignment of tuples to the free variables in p ( R ), including the variable R that make the formula p( R ) true; F is of the form R (p ( R )), and there is some assignment of tuples to the free variables in p( R ) that makes the formula p( R ) true no matter what tuple is assigned to R.

TRC Example Reserves Sailors

Find the names and ages of sailors with rating > 7 { P | S  Sailors (S.rating > 7  P.name = S.sname  P.age = S.age)} P is a Tuple variable with two fields: name and age. - the only fields mentioned in P - P does not range over any relation in the query. Sailors name age Lubber 55.0 rusty 35.0

Find the names of the sailors who reserved boat 103 { P | S  Sailors R  Reserves (R.sid = S.sid  R.bid = 103  P.sname = S.sname)} Retrieve all sailor tuples for which there exists a tuple in Reserves having the same value in the sid field and with bid =103 Question: How is the answer tuple looks like? (columns? Rows?) What is difference? { S | S  Sailors  R  Reserves (R.sid = S.sid  R.bid = 103)}

Find the names of the 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)} Retrieve all sailor tuples S for which there exists tuples R in Reserves and B in Boats such that R.sid = S.sid  B.bid = R.bid  B.color ='red' { P | S  Sailors R  Reserves (R.sid = S.sid  P.sname = S.sname  B  Boats(B.bid = R.bid  B.color ='red'))}

Find the names of the sailors who have reserved all boat { P | S  Sailors B  Boats (R  Reserves (S.sid = R.sid  R.bid = B.bid  P.sname = S.sname))} Find sailors S such that for all boats B there is a Reserves tuple showing that sailor S has reserved boat B.

Find the sailors who have reserved all red boat { P | S  Sailors  B  Boats (B.color = 'red'  (R  Reserves (S.sid = R.sid  R.bid = B.bid))} Question: How to change the algebraic representation?

Find the sailors who have reserved all red boat Logically p  q is equivalent to pq (Why?) { P | S  Sailors  B  Boats (B.color = 'red'  (R  Reserves (S.sid = R.sid  R.bid = B.bid))} is rewritten as: { P | S  Sailors  B  Boats (B.color 'red'  (R  Reserves (S.sid = R.sid  R.bid = B.bid))} to restrict attention to those red boat.

Domain Relational Calculus Query has the form: Answer includes all tuples that make the formula be true.

DRC Formulas Or recursively defined Atomic formula: <x1,x2,…,xn>  Rel , where Rel is a relation with n variables X op Y X op constant op is one of comparison ops (<,>,=,,.), Or recursively defined p, pq, pq, pq , where p, q are formulas X(p(X)), where X is a domain variable X(p(X)), where X is a domain varialbe

Free and Bound Variables The use of quantifiers and in a formula is said to bind X. A variable that is not bound is free. Let us revisit the definition of a query: There is an important restriction: the variables x1, ..., xn that appear to the left of `|’ must be the only free variables in the formula p(...).

Find all sailors with a rating above 7 the domain variables I, N, T and A are bound to fields of the same Sailors tuple. that every tuple that satisfies T>7 is in the answer. Question: Modify to answer: Find sailors who are older than 18 or have a rating under 9, and are called ‘Joe’.

Find sailors rating > 7 who’ve reserved boat #103 Reserves Boats bid bname color 101 interlake red 103 marine green We have used as a shorthand for Note the use of to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. {<I,N,T,A>|<I,N,T,A> Sailors T>7   Ir,Br,D (<Ir,103,D>  Reserves  Ir =I )}

Find sailors rated > 7 who’ve reserved a red boat Reserves Boats bid bname color 101 interlake red 103 marine green Observe how the parentheses control the scope of each quantifier’s binding. Rewrite using 'red' as constant BN = red

Find sailors who’ve reserved all boats Reserves Boats bid bname color 101 interlake red 103 marine green Find all sailors I such that for each 3-tuple either it is not a tuple in Boats or there is a tuple in Reserves showing that sailor I has reserved it.

Algebra vs. Calculus Two formal query languages are equal in the power of expressivness? Algebra  Calculus? YES! Calculus  Algebra? Unsafe query {S | (S  Sailors)} Correct??? Safe TRC --- Dom(Q , I) answers for Q contains only values that are in Dom(Q, I) R(p(R)), tuple r contains only constant in Dom(Q,I) R(p(R)), tuple r contains a constant that is not in Dom(Q,I), then p(r) is true.

Summary Algebra and safe calculus have same expressive power Relationally complete --- if a query language can express all the queries that can be expressed in relational algebra. Relational calculus is non-operational, and users define queries in terms of what they want, not in terms of how to compute it. (Declarativeness.)