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.

Slides:



Advertisements
Similar presentations
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Relational Calculus Chapter 4, Part B.
Advertisements

Relational Calculus   Logic, like whiskey, loses its beneficial effect when taken in too large quantities. --Lord Dunsany.
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.
INFS614, Fall 08 1 Relational Algebra Lecture 4. INFS614, Fall 08 2 Relational Query Languages v Query languages: Allow manipulation and retrieval of.
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.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
FALL 2004CENG 351 File Structures and Data Managemnet1 Relational Algebra.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
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.
By relieving the brain of all unnecessary work, a good notation sets it free to concentrate on more advanced problems, and, in effect, increases the mental.
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.
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.
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.
1.1 CAS CS 460/660 Introduction to Database Systems Relational Algebra.
Database Management Systems 1 Raghu Ramakrishnan Relational Algebra Chpt 4 Xin Zhang.
Relational Algebra.
ICS 321 Fall 2011 The Relational Model of Data (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 8/29/20111Lipyeow.
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.
Database Management Systems, R. Ramakrishnan1 Relational Algebra Module 3, Lecture 1.
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.
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.
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
Relational Algebra Chapter 4, Part A
CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth
Relational Algebra.
Relational Calculus.
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 CS 186, Spring 2007, Lecture 6 R&G, Chapter 4 Mary Roth   We will occasionally use this arrow notation unless there is danger of no confusion. Ronald Graham Elements of Ramsey Theory

Administrivia Homework 1 due in 1 week –Thursday, Feb 8 10 p.m. New syllabus on web site Questions?

Review Database Systems have both theory and practice It’s a systems course, so we are heavy on the practice But our practice has to have theory to back it up 8-) …so we will be looking at both of them in parallel

Review: Where have we been? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Lecture 2 Relational Algebra Relational Model Lecture 5 Lectures 3 &4

Review: Where have we been? Where are we going next? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Lecture 2 Relational Algebra Relational Model Lecture 5 Lectures 3 &4 Relational Calculus Today

Where are we going next? Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Practice SQL On Deck: Practical ways of evaluating SQL

Review – Why do we need Query Languages anyway? Two key advantages –Less work for user asking query –More opportunities for optimization Relational Algebra –Theoretical foundation for SQL –Higher level than programming language but still must specify steps to get desired result Relational Calculus –Formal foundation for Query-by-Example –A first-order logic description of desired result –Only specify desired result, not how to get it

Additional operations: Intersection (  ) Join ( ) Division ( / ) Relational Algebra Review ReservesSailorsBoats Basic operations: Selection ( σ ) Projection ( π ) Cross-product (  ) Set-difference ( — ) Union (  ) :tuples in both relations. :like  but only keep tuples where common fields are equal. :tuples from relation 1 with matches in relation 2 : gives a subset of rows. : deletes unwanted columns. : combine two relations. : tuples in relation 1, but not 2 : tuples in relation 1 and 2. Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Prediction: These relational operators are going to look hauntingly familiar when we get to them…!

Additional operations: Intersection (  ) Join ( ) Division ( / ) Relational Algebra Review ReservesSailorsBoats Basic operations: Selection ( σ ) Projection ( π ) Cross-product (  ) Set-difference ( — ) Union (  ) Find names of sailors who’ve reserved a green boat σ ( color=‘Green’ Boats) ( Sailors) π ( sname ) ( Reserves)

Relational Algebra Review ReservesSailorsBoats Find names of sailors who’ve reserved a green boat * Given the previous algebra, a query optimizer would replace it with this! σ ( color=‘Green’ Boats) ( Sailors) π ( sname ) ( Reserves) π ( bid ) π ( sid ) Or better yet:

Intermission Some algebra exercises for you to practice with are out on the class web site Algebra and calculus exercises make for good exam questions!

Today: Relational Calculus High-level, first-order logic description –A formal definition of what you want from the database e.g. English: “Find all sailors with a rating above 7” In Calculus: {S |S  Sailors  S.rating > 7} “From all that is, find me the set of things that are tuples in the Sailors relation and whose rating field is greater than 7.” Two flavors: –Tuple relational calculus (TRC) (Like SQL) –Domain relational calculus (DRC) (Like QBE)

Relational Calculus Building Blocks Variables TRC: Variables are bound to tuples. DRC: Variables are bound to domain elements (= column values) Constants 7, “Foo”, , etc. Comparison operators =, <>,, etc. Logical connectives  - not  – and  - or  - implies  - is a member of Quantifiers  X(p(X)): For every X, p(X) must be true  X(p(X)): There exists at least one X such that p(X) is true

Relational Calculus English example: Find all sailors with a rating above 7 –Tuple R.C.: {S |S  Sailors  S.rating > 7} “From all that is, find me the set of things that are tuples in the Sailors relation and whose rating field is greater than 7.” –Domain R.C.: { |  Sailors  R > 7} “From all that is, find me column values S, N, R, and A, where S is an integer, N is a string, R is an integer, A is a floating point number, such that is a tuple in the Sailors relation and R is greater than 7.”

Tuple Relational Calculus Query form: {T | p(T)} –T is a tuple and p(T) denotes a formula in which tuple variable T appears. Answer: –set of all tuples T for which the formula p(T) evaluates to true. Formula is recursively defined: –Atomic formulas get tuples from relations or compare values –Formulas built from other formulas using logical operators.

An atomic formula is one of the following: R  Rel R.a op S.b R.a op constant, where op is one of A formula can be: – an atomic formula – where p and q are formulas – where variable R is a tuple variable TRC Formulas

Free and Bound Variables The use of quantifiers  X and  X in a formula is said to bind X in the formula. –A variable that is not bound is free. Important restriction {T | p(T)} –The variable T that appears to the left of `|’ must be the only free variable in the formula p(T). –In other words, all other tuple variables must be bound using a quantifier.

Use of  (For every)  x (P(x)): only true if P(x) is true for every x in the universe: e.g.  x ((x.color = “Red”)  means everything that exists is red Usually we are less grandiose in our assertions:  x ( (x  Boats)  (x.color = “Red”)  is a logical implication a  b means that if a is true, b must be true a  b is the same as  a  b

If a is true, b must be true! –If a is true and b is false, the expression evaluates to false. If a is not true, we don’t care about b –The expression is always true. a T F T F b T TT F

Quantifier Shortcuts  x ( (x  Boats)  (x.color = “Red”)) “For every x in the Boats relation, the color must be Red.” Can also be written as:  x  Boats(x.color = “Red”)  x ( (x  Boats)  (x.color = “Red”)) “There exists a tuple x in the Boats relation whose color is Red.” Can also be written as:  x  Boats (x.color = “Red”)

Selection and Projection Selection Find all sailors with rating above 8 { S |S  Sailors  S.rating > 8} { S |  S1  Sailors ( S1.rating > 8  S.sname = S1.sname  S.age = S1.age )} S is a tuple variable of 2 fields (i.e. {S} is a projection of Sailors) snameage Projection Find names and ages of sailors with rating above 8. S S1 yuppy35.0 S1 S rusty35.0

Note the use of  to find a tuple in Reserves that `joins with’ the Sailors tuple under consideration. {S | S  Sailors  S.rating > 7   R(R  Reserves  R.sid = S.sid  R.bid = 103)} Joins Find sailors rated > 7 who’ve reserved boat #103 S S S R R What if there was another tuple {58, 103, 12/13/96} in the Reserves relation?

Joins (continued) Notice how the parentheses control the scope of each quantifier’s binding. {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 What does this expression compute?

Division Find all sailors S such that… A value x in A is disqualified if by attaching a y value from B, we obtain an xy tuple that is not in A. (e.g: only give me A tuples that have a match in B. {S | S  Sailors   B  Boats (  R  Reserves (S.sid = R.sid  B.bid = R.bid))} e.g. Find sailors who’ve reserved all boats: Recall the algebra expression A/B… In calculus, use the  operator: For all tuples B in Boats… There is at least one tuple in Reserves… showing that sailor S has reserved B.

More Calculus exercises on the web site…

Unsafe Queries, Expressive Power  syntactically correct calculus queries that have an infinite number of answers! These are unsafe queries. –e.g., –Solution???? Don’t do that! Expressive Power (Theorem due to Codd): –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 languages (e.g., SQL) can express every query that is expressible in relational algebra/calculus. (actually, SQL is more powerful, as we will see…)

Relational Completeness means… Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB PracticeTheory Relational Algebra Relational Model Relational Calculus

Now we can study SQL! Query Optimization and Execution Relational Operators Files and Access Methods Buffer Management Disk Space Management DB Practice SQL

Summary The relational model has rigorously defined query languages that are simple and powerful. –Algebra and safe calculus have same expressive power Relational algebra is more operational –useful as internal representation for query evaluation plans.  … they’ll be baa-aack…. Relational calculus is more declarative –users define queries in terms of what they want, not in terms of how to compute it. Almost every query can be expressed several ways –and that’s what makes query optimization fun!