Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on...
Transcript of Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on...
![Page 1: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/1.jpg)
1
Introduction to Data ManagementCSE 344
Lecture 13: Relational Calculus
CSE 344 - Fall 2016
![Page 2: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/2.jpg)
Announcements
• WQ 4 is out• HW 4 is out
• Midterm review session in class next Fri (11/4)
• OHs changes next few weeks– Check website for most updated info
CSE 344 - Fall 2016 2
![Page 3: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/3.jpg)
Big Picture• Relational data model
– Instance– Schema– Query language
• SQL• Relational algebra• Relational calculus• Datalog
CSE 344 - Fall 2016 3
• Query processing– Logical & physical
plans– Indexes– Cost estimation– Query optimization
![Page 4: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/4.jpg)
Why bother with another QL?• SQL and RA are good for query planning
– They are not good for formal reasoning– How do you show that two SQL queries are
equivalent / non-equivalent?– Two RA plans?
• RC was the first language proposed with the relational model (Codd)
• Influenced the design of datalog as we will see
CSE 344 - Fall 2016 4
![Page 5: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/5.jpg)
Relational Calculus
• Aka predicate calculus or first order logic– 311 anyone?
• TRC = Tuple Relational Calculus– See book
• DRC = Domain Relational Calculus– We study only this one– Also see Query Language Primer on course website
CSE 344 - Fall 2016 5
![Page 6: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/6.jpg)
Relational Calculus
P ::= atom | P ∧ P | P ∨ P | P⇒ P | not(P) | ∀x.P | ∃x.P
Relational predicate P is a formula given by this grammar:Q(x1, …, xk) = P
Query Q:
CSE 344 - Fall 2016 6
Atomic predicate is either a relational or interpreted predicate:atom ::= R(x1, …, xk) | x = y | x > k | ... R(x,y) means (x,y) is in R
![Page 7: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/7.jpg)
Relational Calculus
Q(f,l) = ∃x. ∃y. ∃z. (Actor(z,f,l) ∧Casts(z,x)∧Movie(x,y,1940))
Example: find the first/last names of actors who acted in 1940
What does this query return ?7CSE 344 - Fall 2016Q(f,l) = ∃z. (Actor(z,f,l) ∧∀x.(Casts(z,x)⇒∃y.Movie(x,y,1940)))
Actor(pid,fName,lName)Casts(pid,mid)Movie(mid,title,year)
P ::= atom | P ∧ P | P ∨ P | P⇒ P | not(P) | ∀x.P | ∃x.P
Relational predicate P is a formula given by this grammar:Q(x1, …, xk) = P
Query Q:
Atomic predicate is either a relational or interpreted predicate:atom ::= R(x1, …, xk) | x = y | x > k | ... R(x,y) means (x,y) is in R
![Page 8: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/8.jpg)
Important Observation
Find all bars that serve all beers that Fred likes
• Note: P ⇒ Q (read P implies Q) is the same as (not P) ∨ Q
In this query: If Fred likes a beer the bar must serve it (P ⇒ Q) In other words: Either Fred does not like the beer (not P) OR the bar serves that beer (Q).
8
A(x) = ∀y. Likes("Fred", y) ⇒ Serves(x,y)
A(x) = ∀y. not(Likes("Fred", y)) ∨ Serves(x,y)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
![Page 9: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/9.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Average Joe
CSE 344 - Fall 2016 9
![Page 10: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/10.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Average Joe
CSE 344 - Fall 2016 10
![Page 11: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/11.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
CSE 344 - Fall 2016 11
![Page 12: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/12.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Q(x) = ∀y. Frequents(x, y)⇒ (∃z. Serves(y,z)∧Likes(x,z))
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
CSE 344 - Fall 2016 12
![Page 13: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/13.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Find drinkers that frequent some bar that serves only beers they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Q(x) = ∀y. Frequents(x, y)⇒ (∃z. Serves(y,z)∧Likes(x,z))
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
Cautious Carl
CSE 344 - Fall 2016 13
![Page 14: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/14.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Find drinkers that frequent some bar that serves only beers they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Q(x) = ∀y. Frequents(x, y)⇒ (∃z. Serves(y,z)∧Likes(x,z))
Q(x) = ∃y. Frequents(x, y)∧∀z.(Serves(y,z) ⇒ Likes(x,z))
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
Cautious Carl
CSE 344 - Fall 2016 14
![Page 15: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/15.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Find drinkers that frequent only bars that serves only beer they like.
Find drinkers that frequent some bar that serves only beers they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Q(x) = ∀y. Frequents(x, y)⇒ (∃z. Serves(y,z)∧Likes(x,z))
Q(x) = ∃y. Frequents(x, y)∧∀z.(Serves(y,z) ⇒ Likes(x,z))
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
Cautious Carl
Paranoid Paul
CSE 344 - Fall 2016 15
![Page 16: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/16.jpg)
More ExamplesFind drinkers that frequent some bar that serves some beer they like.
Find drinkers that frequent only bars that serves some beer they like.
Find drinkers that frequent only bars that serves only beer they like.
Find drinkers that frequent some bar that serves only beers they like.
Q(x) = ∃y. ∃z. Frequents(x, y)∧Serves(y,z)∧Likes(x,z)
Q(x) = ∀y. Frequents(x, y)⇒ (∃z. Serves(y,z)∧Likes(x,z))
Q(x) = ∃y. Frequents(x, y)∧∀z.(Serves(y,z) ⇒ Likes(x,z))
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
Prudent Peter
Average Joe
Cautious Carl
Paranoid Paul
16CSE 344 - Fall 2016Q(x) = ∀y. Frequents(x, y)⇒ ∀z.(Serves(y,z) ⇒ Likes(x,z))
![Page 17: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/17.jpg)
• A ⇒ B = not(A) ∨ B• not(A ∧ B) = not(A) ∨ not(B)• not(A ∨ B) = not(A) ∧ not(B)• ∀x. P(x) = not(∃x. not(P(x)))
• Example:– ∀z. Serves(y,z) ⇒ Likes(x,z) – ∀z. not(Serves(y,z)) ∨ Likes(x,z)– not (∃z. Serves(y,z) ∧ not(Likes(x,z))
Remember your logical equivalences!
CSE 344 - Fall 201617
![Page 18: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/18.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
Make sure x is a beer
CSE 344 - Fall 2016 18
![Page 19: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/19.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A2(x,y) = Likes("Fred", x) ∨ Serves("Bar", y)
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
Make sure x is a beer
CSE 344 - Fall 2016 19
![Page 20: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/20.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A2(x,y) = Likes("Fred", x) ∨ Serves("Bar", y)
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
A2(x,y) = ∃u Serves(u,x)∧∃w Serves(w,y)∧[Likes("Fred", x)∨Serves("Bar", y)]
Make sure x is a beer
Same here
CSE 344 - Fall 2016 20
![Page 21: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/21.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A3(x) = ∀y. Serves(x,y)
A2(x,y) = Likes("Fred", x) ∨ Serves("Bar", y)
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
A2(x,y) = ∃u Serves(u,x)∧∃w Serves(w,y)∧[Likes("Fred", x)∨Serves("Bar", y)]
Make sure x is a beer
Same here
CSE 344 - Fall 2016 21
![Page 22: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/22.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A2(x,y) = Likes("Fred", x) ∨ Serves("Bar", y)
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
A2(x,y) = ∃u Serves(u,x)∧∃w Serves(w,y)∧[Likes("Fred", x)∨Serves("Bar", y)]
Make sure x is a beer
Same here
CSE 344 - Fall 2016 22
A3(x) =∃u.Serves(x,u)∧∀y.∃z.Serves(z,y) à Serves(x,y)
Likewise
A3(x) = ∀y. Serves(x,y)
![Page 23: Introduction to Data Management CSE 344 · 2016. 10. 28. · – Also see Query Language Primer on course website CSE 344 - Fall 2016 5. Relational Calculus P ... Find all bars that](https://reader034.fdocuments.in/reader034/viewer/2022051804/5fee18c30d2c7d1d3162a520/html5/thumbnails/23.jpg)
Domain Independent Relational Calculus
• An unsafe RC query, aka domain dependent,returns an answer that does not depend just on the relations, but on the entire domain of possible values
A1(x) = not Likes("Fred", x)
Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)
A3(x) = ∀y. Serves(x,y)
A2(x,y) = Likes("Fred", x) ∨ Serves("Bar", y)
Lesson: make sure your RC queries are domain independent
A1(x) = ∃y Serves(y,x) ∧not Likes("Fred", x)
A2(x,y) = ∃u Serves(u,x)∧∃w Serves(w,y)∧[Likes("Fred", x)∨Serves("Bar", y)]
A3(x) =∃u.Serves(x,u)∧∀y.∃z.Serves(z,y) à Serves(x,y)
Make sure x is a beer
Same here
Likewise