Relational Calculus Zachary G. Ives November 15, 2018

Slides:



Advertisements
Similar presentations
Relational Calculus and Datalog
Advertisements

From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 22, 2005.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
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.
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.
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 23, 2004.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2004 Some slide content.
Introduction to SQL, the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 16, 2003.
1 Lecture 5: Relational calculus
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 21, 2004 Some slide content.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 Relational Algebra and Calculus Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Revision of Midterm 2 Prof. Sin-Min Lee Department of Computer Science.
Relational Algebra Wrap-up and Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 11, 2003.
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.
Relational Algebra & Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 12, 2007 Some slide content.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
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.
Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 17, 2007 Some slide content courtesy.
1 Relational Algebra & Calculus Chapter 4, Part A (Relational Algebra)
1 Relational Algebra and Calculas Chapter 4, Part A.
Relational Algebra.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
From the Calculus to the Structured Query Language Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 19, 2007.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L5_Relational Calculus 1 Relational Calculus Chapter 4 – Part B.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Relational Calculus. Relational calculus query specifies what is to be retrieved rather than how to retrieve it. – No description of how to evaluate a.
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
1 Relational Algebra. 2 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
CSE202 Database Management Systems
Module 2: Intro to Relational Model
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Relational Calculus Chapter 4, Part B
Chapter 6: Formal Relational Query Languages
The Relational Algebra and Relational Calculus
Relational Model & Algebra
Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition
Relational Model & Algebra
Schema Refinement and Normalization
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra & Calculus
The Relational Algebra and Relational Calculus
Chapter 6: Formal Relational Query Languages
Instructor: Mohamed Eltabakh
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 2: Intro to Relational Model
Chapter 6: Formal Relational Query Languages
Example of a Relation attributes (or columns) tuples (or rows)
Chapter 2: Intro to Relational Model
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 B 7/1/2019.
CSE544 Wednesday, March 29, 2006.
CS589 Principles of DB Systems Fall 2008 Lecture 4b: Domain Independence and Safety Lois Delcambre
Relational Calculus Chapter 4 – Part II.
Relational Calculus Chapter 4, Part B
Presentation transcript:

Relational Calculus Zachary G. Ives November 15, 2018 University of Pennsylvania CIS 550 – Database & Information Systems November 15, 2018 Some slide content courtesy of Susan Davidson & Raghu Ramakrishnan

Administrivia Reminder: Homework 1 due 9/28 (next Monday) Upcoming research talks of possible interest: Amol Deshpande, U Maryland, Monday 10/19, 3PM – adding probabilities to databases

The Relational Algebra as “Virtual Machine” Instructions Six basic operations: Projection  (R) Selection  (R) Union R1 [ R2 Difference R1 – R2 Product R1 £ R2 (Rename) ->b (R) And some other useful ones: Join R1 ⋈ R2 Intersection R1 Å R2 STUDENT Takes COURSE Calculus SELECT * FROM STUDENT, Takes, COURSE WHERE STUDENT.sid = Takes.sID AND Takes.cID = cid

Our Example Data Instance STUDENT Takes COURSE sid name 1 Jill 2 Qun 3 Nitin 4 Marty sid exp-grade cid 1 A 550-0109 520-1009 3 C 501-0109 4 cid subj sem 550-0109 DB F09 520-1009 AI S09 501-0109 Arch PROFESSOR Teaches fid name 11 Ives 12 Taskar 18 Martin fid cid 11 550-0109 12 520-1009 18 501-0109

Relational Calculus, Variant I: Domain Relational Calculus (DRC) Queries have form: {<x1,x2, …, xn>| p} Predicate: Boolean expression over x1,x2, …, xn Precise operations depend on the domain and query language – may include special functions, etc. Assume the following at minimum: <xi,xj,…>  R X op Y X op const const op X where op is , , , , ,  xi,xj,… are domain variables domain variables predicate

Complex Predicates in the Calculus Starting with these atomic predicates, build up new predicates by the following rules: Logical connectives: If p and q are predicates, then so are p  q, p  q, p, and p  q (x>2)  (x<4) (x>2)  (x>0) Existential quantification: If p is a predicate, then so is x.p x. (x>2) (x<4) Universal quantification: If p is a predicate, then so is x.p x.x>2 x. y.y>x

Some Examples Faculty ids Subjects for courses with students expecting a “C” All course numbers for which there exists a smaller course number

Logical Equivalences There are two logical equivalences that will be heavily used: p  q  p  q (Whenever p is true, q must also be true.) x. p(x)  x. p(x) (p is true for all x) The second can be a lot easier to check! Example: The highest course number offered

Terminology: Free and Bound Variables A variable v is bound in a predicate p when p is of the form v… or v… A variable occurs free in p if it occurs in a position where it is not bound by an enclosing  or  Examples: x is free in x > 2 x is bound in x. x > y

Can Rename Bound Variables Only When a variable is bound one can replace it with some other variable without altering the meaning of the expression, providing there are no name clashes Example: x. x > 2 is equivalent to y. y > 2 Otherwise, the variable is defined outside our “scope”…

Safety Pitfall in what we have done so far – how do we interpret: {<sid,name>| <sid,name>  STUDENT} Set of all binary tuples that are not students: an infinite set (and unsafe query) A query is safe if no matter how we instantiate the relations, it always produces a finite answer Domain independent: answer is the same regardless of the domain in which it is evaluated Unfortunately, both this definition of safety and domain independence are semantic conditions, and are undecidable

Safety and Termination Guarantees There are syntactic conditions that are used to guarantee “safe” formulas The definition is complicated, and we won’t discuss it; you can find it in Ullman’s Principles of Database and Knowledge-Base Systems The formulas that are expressible in real query languages based on relational calculus are all “safe” Many DB languages include additional features, like recursion, that must be restricted in certain ways to guarantee termination and consistent answers

Mini-Quiz How do you write: Which students have taken more than one course from the same professor?

Translating from RA to DRC Core of relational algebra: , , , x, - We need to work our way through the structure of an RA expression, translating each possible form. Let TR[e] be the translation of RA expression e into DRC. Relation names: For the RA expression R, the DRC expression is {<x1,x2, …, xn>| <x1,x2, …, xn>  R}

Selection: TR[ R] Suppose we have (e’), where e’ is another RA expression that translates as: TR[e’]= {<x1,x2, …, xn>| p} Then the translation of c(e’) is {<x1,x2, …, xn>| p’} where ’ is obtained from  by replacing each attribute with the corresponding variable Example: TR[#1=#2 #4>2.5R] (if R has arity 4) is {<x1,x2, x3, x4>| < x1,x2, x3, x4>  R  x1=x2  x4>2.5}

Projection: TR[i1,…,im(e)] If TR[e]= {<x1,x2, …, xn>| p} then TR[i1,i2,…,im(e)]= {<x i1,x i2, …, x im >|  xj1,xj2, …, xjk.p}, where xj1,xj2, …, xjk are variables in x1,x2, …, xn that are not in x i1,x i2, …, x im Example: With R as before, #1,#3 (R)={<x1,x3>| x2,x4. <x1,x2, x3,x4> R}

Union: TR[R1  R2] R1 and R2 must have the same arity For e1  e2, where e1, e2 are algebra expressions TR[e1]={<x1,…,xn>|p} and TR[e2]={<y1,…yn>|q} Relabel the variables in the second: TR[e2]={< x1,…,xn>|q’} This may involve relabeling bound variables in q to avoid clashes TR[e1e2]={<x1,…,xn>|pq’}. Example: TR[R1  R2] = {< x1,x2, x3,x4>| <x1,x2, x3,x4>R1  <x1,x2, x3,x4>R2

Other Binary Operators Difference: The same conditions hold as for union If TR[e1]={<x1,…,xn>|p} and TR[e2]={< x1,…,xn>|q} Then TR[e1- e2]= {<x1,…,xn>|pq} Product: If TR[e1]={<x1,…,xn>|p} and TR[e2]={< y1,…,ym>|q} Then TR[e1 e2]= {<x1,…,xn, y1,…,ym >| pq} Example: TR[RS]= {<x1,…,xn, y1,…,ym >| <x1,…,xn> R  <y1,…,ym > S }

What about the Tuple Relational Calculus (TRC)? We’ve been looking at the Domain Relational Calculus The Tuple Relational Calculus is nearly the same, but variables are at the level of a tuple, not an attribute {Q | 9 S  COURSES, 9 T 2 Takes (S.cid = T.cid Æ Q.cid = S.cid Æ Q.exp-grade = T.exp-grade)} The use of the “output” variable is a bit unintuitive, but otherwise it should be very similar

Limitations of the Relational Algebra / Calculus Can’t do: Aggregate operations (average, sum, count) Track the number of duplicate elements (bag semantics) Recursive queries Complex (non-tabular) structures Most of these are expressible in SQL, OQL, XQuery – using other special operators Sometimes we even need the power of a Turing-complete programming language

Summary Can translate relational algebra into relational calculus DRC and TRC are slightly different syntaxes but equivalent Given syntactic restrictions that guarantee safety of DRC query, can translate back to relational algebra These are the principles behind initial development of relational databases SQL is close to calculus; query plan is close to algebra Great example of theory leading to practice!