Relational Calculus Chapter 4, Part B 7/1/2019.

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.
Chapter 3 Tuple and Domain Relational Calculus. Tuple Relational Calculus.
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 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
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.
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.
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.
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 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.
1 Relational Algebra and Calculus Chapter 4. 2 Relational Query Languages  Query languages: Allow manipulation and retrieval of data from a database.
The Relational Model: Relational Calculus
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,1 Relational Calculus.
Relational Calculus. Non Procedural or Declarative Calculus has variables, constants, comparison ops, logical connectives and quantifiers. There are TWO.
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.
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.
IST 210 The Relational Language Todd S. Bacastow January 2004.
1 Copyright © Kyu-Young Whang Relational Calculus Chapter 4, Part B.
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.
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
Relational Calculus Chapter 4, Part B
Chapter 6: Formal Relational Query Languages
Relational Algebra Chapter 4, Part A
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.
Database Applications (15-415) Relational Calculus Lecture 6, September 6, 2016 Mohammad Hammoud.
Lecture#5: Relational calculus
Relational Calculus.
Relational Algebra Chapter 4, Sections 4.1 – 4.2
Relational Calculus and QBE
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Relational Calculus and QBE
Chapter 6: Formal Relational Query Languages
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
Chapter 27: Formal-Relational Query Languages
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Relational Calculus Chapter 4, Part B 7/1/2019

SQL is based on the relational calculus Relational Algebra provides a set of operations (𝜎, 𝜋, ⋈, 𝑒𝑡𝑐.) that can be used to compute a desired relation from the database (i.e., procedural language) Relational Calculus provides notations for formulating the definition of that desired relation without stating the operations to be performed (i.e., declarative language) SQL is based on the relational calculus 7/1/2019

Relational Calculus We discuss only DRC Calculus has variables, constants, comparison ops, logical connectives, and quantifiers. Comes in two flavors: Tuple relational calculus (TRC) and Domain relational calculus (DRC) TRC: Variables range over (i.e., get bound to) tuples. DRC: Variables range over domain elements (= field values). 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. 7/1/2019

Domain Relational Calculus Query has the form: { <x1, x2, …, xn> | p(<x1, x2, …, xn>) } tuple formula Answer includes all tuples <x1, x2, …, xn> that make the formula p(<x1, x2, …, xn>) true. Training Name Example: ID Age 7/1/2019

DRC and SQL SELECT * FROM Sailors S WHERE S.T > 7 {<I,N,T,A> | <I,N,T,A> ∈𝑺𝒂𝒊𝒍𝒐𝒓𝒔 𝑻>𝟕} Output schema Input relation Predicate 7/1/2019

DRC and SQL {<I,N,T,A> | <I,N,T,A> ∈𝑺𝒂𝒊𝒍𝒐𝒓𝒔 𝑻>𝟕} ‘|’ should be read as ‘such that’ It says that every tuple <I,N,T,A> of Sailors that satisfies T > 7 is in the answer. {<I,N,T,A> | <I,N,T,A> ∈𝑺𝒂𝒊𝒍𝒐𝒓𝒔 𝑻>𝟕} Output schema Input relation Predicate 7/1/2019

Formula starting with simple atomic formulas, and Formula is recursively defined, starting with simple atomic formulas, and building bigger and better formulas using the logical connectives. 7/1/2019

DRC Formulas Atomic formula: getting tuples from relations: or making comparisons of values X op Y, or X op constant where op is one of ˂, ˃, =, ≤, ≥, ≠. Formula: recursively defined starting with atomic formulas an atomic formula, or If p and q are formulae, so are ¬p, pΛq, and pVq. If p is a formula with domain variable X, then and are also formulae. 7/1/2019

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. Important restriction: The variables x1, x2, …, xn that appear to the left of “|” must be the only free variables in the formula p(…), All other variables must be bound using a quantifier. 7/1/2019

Find sailors rated > 7 who have reserved boat #103 Variables that appear to the left of “|” must be the only free variables in the formula - Do not quantify them Ir, Br, and D are bound 7/1/2019

Find sailors rated > 7 who have reserved boat #103 Find all sailor I such that his/her rating is better than 7, and … for boat 103 there exist a reservation … … and the reservation is for I … 7/1/2019

Find sailors rated > 7 who have reserved boat #103 The use of Ǝ is to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. Use “Ǝ Ir, Br, D (…)” as a short hand for: “Ǝ Ir ( Ǝ Br ( Ǝ D (…)))” 7/1/2019

Who are older than 18 or have a rating under 9 Find all sailors who are older than 18 or have a rating under 9, and are called ‘joe’ Find all sailors {<I,N,T,A> | <I,N,T,A> ∈ Sailors Λ (A>18 V T<9) Λ N=“joe” } are called ‘joe’ Who are older than 18 or have a rating under 9 Quantifiers are not required 7/1/2019

Find sailors rated > 7 who’ve reserved a red boat Find a sailor I rated better than 7 There exists a reservation of Br for I Br is a boat and its color is read ( A … (B … (C … ) ) ) BLUE ZONE GREEN ZONE BLACK ZONE 7/1/2019

Find sailors rated > 7 who’ve reserved a red boat A can be referenced in all three zones B can be referenced in only two zones ( A … ( B … (C … ) ) ) BLUE ZONE RED ZONE BLACK ZONE 7/1/2019

Find sailors rated > 7 who’ve reserved a red boat Short hand A shorter way to write the above query: {<I,N,T,A> | <I,N,T,A> ∈𝑆𝑎𝑖𝑙𝑜𝑟𝑠 ∧ T > 7 ∧ ∃<Ir,Br,D> ∈𝑅𝑒𝑠𝑒𝑟𝑣𝑒𝑠( ∃<B,BN,C> ∈𝐵𝑜𝑎𝑡𝑠( I = Ir ∧ Br = B ∧ C =‘red’) ) } 7/1/2019

Find sailors rated > 7 who’ve reserved a red boat <𝐼,𝑁,𝑇,𝐴> <𝐼,𝑁,𝑇,𝐴> ∈𝑆𝑎𝑖𝑙𝑜𝑟𝑠 ⋀ 𝑇>7 ∧ ∃ 𝐼𝑟, 𝐵𝑟, 𝐷 [<𝐼𝑟,𝐵𝑟,𝐷> ∈𝑅𝑒𝑠𝑒𝑟𝑣𝑒𝑠 ⋀ 𝐼𝑟=𝐼] ∧ ∃ 𝐵, 𝐵𝑁,𝐶 [<𝐵,𝐵𝑁,𝐶> ∈𝐵𝑜𝑎𝑡𝑠 ⋀ B=𝐵𝑟 ∧ 𝐶 = ′ 𝑟𝑒𝑑′]} Br is not defined in this scope { A … [B … ] ⋀ [C … ] } The parentheses control the scope of each quantifier’s binding. This may look cumbersome, but with a good user interface, it is very intuitive (e.g., MS Access, QBE) 7/1/2019

Find sailors who’ve reserved all boats Find all sailors I such that for each <B,BN,C> tuple, either it is not a tuple in Boats, or Output I N T A Sailors there is a tuple in Reserves showing that sailor I has reserved it. Ir Br D Reserves B BN C Boats 7/1/2019

Find sailors who’ve reserved all boats Not(A) V B is equivalent to A → B B If ˂B,BN,C> is a boat, then there is a reservation of this boat Br for sailor I 7/1/2019

Find sailors who’ve reserved all boats (again!) There exists a reservation for sailor I This condition is true for all boats Simpler notation, same query. To find sailors who’ve reserved all red boats: ..... Unless the boat is not red, or sailor I has reserved it If the boat is red then sailor I has reserved it 7/1/2019

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. 7/1/2019

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. 7/1/2019