The Relational Model: Relational Calculus

Slides:



Advertisements
Similar presentations
Artificial Intelligence
Advertisements

Relational Calculus and Datalog
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.
The Relational Calculus
Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
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.
R ELATIONAL A LGEBRA M ORE POINTERS FROM T UESDAY.
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.
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 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.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
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.
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.
Handout 2CIS 550, Fall CIS 550, Fall 2001 Handout 2. SQL, Relational Calculus and Datalog.
Relational Algebra.  Introduction  Relational Algebra Operations  Projection and Selection  Set Operations  Joins  Division  Tuple Relational Calculus.
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.
1 CS 430 Database Theory Winter 2005 Lecture 6: Relational Calculus.
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.
Database Management Systems,1 Relational Calculus.
First Order Logic Lecture 2: Sep 9. This Lecture Last time we talked about propositional logic, a logic on simple statements. This time we will talk about.
Relational Calculus Zachary G. Ives University of Pennsylvania CIS 550 – Database & Information Systems September 17, 2007 Some slide content courtesy.
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.
The Relational Calculus (Based on Chapter 9 in Fundamentals of Database Systems by Elmasri and Navathe, Ed. 3)
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module A: Formal Relational.
Lecture 7: Foundations of Query Languages Tuesday, January 23, 2001.
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.
Relational Calculus Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
Database Management Systems, R. Ramakrishnan1 Relational Calculus Chapter 4, Part B.
Relational Calculus Chapter 4, Section 4.3.
Relational Algebra & Calculus
CSE202 Database Management Systems
Relational Calculus Chapter 4, Part B
Chapter 6: Formal Relational Query Languages
The Relational Algebra and Relational Calculus
Relational Calculus Zachary G. Ives November 15, 2018
Relational Algebra & Calculus
Chapter 6: Formal Relational Query Languages
CS 186, Fall 2002, Lecture 8 R&G, Chapter 4
Chapter 6: Formal Relational Query Languages
Relational Algebra & Calculus
Chapter 27: Formal-Relational Query Languages
Relational Calculus Chapter 4, Part B 7/1/2019.
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:

The Relational Model: Relational Calculus Fall 2002 CSE330/CIS550 Handout 2

Relational Calculus First-order logic (FOL) can also be thought of as a query language, and can be used in two ways: Tuple relational calculus Domain relational calculus The difference is the level at which variables are used: for attributes (domains) or for tuples. The calculus is non-procedural (declarative) as compared to the algebra. Fall 2002 CSE330/CIS550 Handout 2

Domain relational calculus Queries have form: {<x1,x2, …, xn>|p} where x1,x2, …, xn are domain variables and p is a predicate which may mention the variables x1,x2, …, xn Example: simple projection {<RN,H>|RI,G,R. <RI,RN,G,R,H>Routes} Example: selection and projection: {<RN,H>|RI,G,R. <RI,RN,G,R,H>Routes  G >15} Fall 2002 CSE330/CIS550 Handout 2

DRC examples, cont Join: {<CI,R>|RI,RN,G,H,RI’,Da,Du. <RI,RN,G,R,H>Routes  <CI,RI’,Da,Du>Climbs  RI=RI’} We could also have written the above as: {<CI,R>|RI,RN,G,H,Da,Du. <RI,RN,G,R,H>Routes  <CI,RI,Da,Du>Climbs} Fall 2002 CSE330/CIS550 Handout 2

Predicate Logic - a quick review The syntax of predicate logic starts with variables, constants and predicates that can be built using a collection of boolean-valued operators (boolean expressions) Examples: 1=2, x  y, prime(x), contains(t,”Joe”). Precisely what operations are available depends on the domain and on the query language. For now we will assume the following boolean expressions: <X,Y,…>  Rel, X op Y, X op constant, or constant op X, where op is , , , , ,  and X,Y,… are domain variables Fall 2002 CSE330/CIS550 Handout 2

Predicate Logic, cont. Starting with these basic predicates (also called atomic) , we can 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 Fall 2002 CSE330/CIS550 Handout 2

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 will be especially important when we study SQL. Fall 2002 CSE330/CIS550 Handout 2

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 x is free in (x>17)  (x.x>2) Note that there are two occurrences of x in the last example. Fall 2002 CSE330/CIS550 Handout 2

Renaming variables 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 Fall 2002 CSE330/CIS550 Handout 2

Some queries… Try the following examples: The names and ages of climbers The names and ages of climbers who have climbed route 214 The names of climbers who have climbed “Last Tango” The names of climbers who have climbed all routes with rating greater than 15 The names of climbers who have climbed the same route twice Fall 2002 CSE330/CIS550 Handout 2

Safety There is a problem with what we have done so far. How should we treat a query like: {<CI,CN,S,A>| <CI,CN,S,A>  Climbers>} This presumably means the set of all four-tuples that are not climbers, which is presumably an infinite set (and unsafe query). A query is safe if no matter how we instantiate the relations, it always produces a finite answer. In particular, the query should be domain independent, meaning that the 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. Fall 2002 CSE330/CIS550 Handout 2

Syntactic Safety There are syntactic conditions that are used to define “safe” formulas. In particular: Every “safe” formula is domain independent. It is implementable. The formulas that are expressible in real query languages based on relational calculus are all “safe” The definition is complicated, and is not in the text book. It can be found in Ullman’s book on databases (Principles of Database and Knowledge-Base Systems). Fall 2002 CSE330/CIS550 Handout 2

Translating from RA to DRC Recall that the relational algebra consists of , , , 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} Fall 2002 CSE330/CIS550 Handout 2

Selection Suppose the RA expression is c(e’), where e’ is another RA expression with TR[e’]= {<x1,x2, …, xn>| p} Then the translation of c(e’) is {<x1,x2, …, xn>| pC’}, where C’ is the condition obtained from C by replacing each attribute with the corresponding variable. Example: TR[#1=#2 #4>2.5R] (where R has arity 4) is {<x1,x2, x3, x4>|< x1,x2, x3, x4>  R  x1=x2  x4>2.5} Fall 2002 CSE330/CIS550 Handout 2

Projection 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} Fall 2002 CSE330/CIS550 Handout 2

Union We know that R and S in RS must be union compatible, so they must have the same arity. Therefore we can assume that for e1e2, where e1, e2are algebra expressions, TR[e1]={<x1,…,xn>|p} and TR[e2]={<y1,…yn>|q}. Relabel the variables in the second so that TR[e2]={< x1,…,xn>|q’}. This may involve relabeling bound variables in q to avoid clashes. Then TR[e1e2]={<x1,…,xn>|pq’}. Example: TR[RS]= {< x1,x2, x3,x4>| <x1,x2, x3,x4>R  <x1,x2, x3,x4>S Fall 2002 CSE330/CIS550 Handout 2

Other binary operators Difference: The same conditions hold as for union. So 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 } Fall 2002 CSE330/CIS550 Handout 2

Summary We’ve seen how to translate relational algebra into (domain) relational calculus. There are various syntactic restrictions for guaranteeing the safety of a DRC query. From any of these we can translate back into relational algebra It was this correspondence between an (implementable and optimizable) algebra and first-order logic that was responsible for the initial development of relational databases – a prime example of some theory leading to highly successful practical developments! Fall 2002 CSE330/CIS550 Handout 2

What we cannot compute with relational algebra Aggregate operations, e.g. “The number of climbers who have climbed ‘Last Tango’” or “The average age of climbers.” These are possible in SQL. Recursive queries. Given a relation Parent(Parent, Child) compute the ancestor relation. This appears to call for an arbitrary number of joins. It is known that it cannot be expressed in first-order logic, hence it cannot be expressed in relational algebra. Fall 2002 CSE330/CIS550 Handout 2

What we cannot compute with relational algebra, cont Computing with complex structures that are not (1NF) relations, e.g. lists, arrays, multisets. Of course, we can always compute such things if we can “talk to” a database from a full-blown (Turing complete) programming language, and we’ll see how to do this later. However, communicating with a database in this way may well be inefficient, and adding computational power to a query language remains an important research topic. Fall 2002 CSE330/CIS550 Handout 2