Top Banner
CSE 132A Discussion Week 2: Relational Calculus
31

CSE 132A Discussion

Apr 09, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 132A Discussion

CSE 132A DiscussionWeek 2: Relational Calculus

Page 2: CSE 132A Discussion

Definition

● A nonprocedural query language, where each query is of the form:

{t | P(t)}

● Results: the set of all tuples t such that predicate P is true for t● t ∈ r denotes that tuple t is in relation r● t is a tuple variable, t(A) denotes the value of tuple t on attribute A● P is a formula similar to that of the predicate calculus

Page 3: CSE 132A Discussion

Predicate Calculus Formula● A set of attributes and constants● A set of comparison operators: (e.g. <, ≤, =, ≠, ≥, >)● A set of connectives: and (^) or (v), not (¬)● Implication (→): x → y, if x is true, then y is true.● A set of quantifiers:

○ ∃ t ∈ r (Q (t )) ≡ ”there exists” a tuple in t in relation r such that predicate Q (t) is true

○ ∀t ∈ r (Q (t )) ≡ Q is true “for all” tuples t in relation r

Page 4: CSE 132A Discussion

Logic Examples

Express “Each unicorn has a horn” in relational calculus

Page 5: CSE 132A Discussion

Logic Examples

Each unicorn has a horn.

∀a ∈ Animals,a is a unicorn → a has a horn

∀a ∈ Animals, a is a unicorn ∧ a has a horn

Page 6: CSE 132A Discussion

Logic Examples

Each unicorn has a horn.

∀a ∈ Animals,a is a unicorn → a has a horn

∀a ∈ Animals, a is a unicorn ∧ a has a horn

Some horses have horns.

Page 7: CSE 132A Discussion

Logic Examples

Each unicorn has a horn.

∀a ∈ Animals,a is a unicorn → a has a horn

∀a ∈ Animals, a is a unicorn ∧ a has a horn

Some horses have horns.

∃a ∈ Animals, a is a horse ∧ a has a horn

Page 8: CSE 132A Discussion

Our schemaConsider the following database schema for a BOOKSTORE database:

● Books (bookid, title, author, year) ● Customers (customerid, name, email) ● Purchases (customerid, bookid, year) ● Reviews (customerid, bookid, rating) ● Pricing (bookid, format, price)

Page 9: CSE 132A Discussion

Example 1 : from SQL to Relational CalculusFind books (show their titles) written by ’EDMUND MORGAN’ since year 1990.

SQL?

Page 10: CSE 132A Discussion

Example 1 : from SQL to Relational CalculusFind books (show their titles) written by ’EDMUND MORGAN’ since year 1990.

SELECT title

FROM Books

WHERE author = ‘EDMUND MORGAN’ AND year >= 1990

Page 11: CSE 132A Discussion

SELECT title

FROM Books

WHERE author = ‘EDMUND MORGAN’ AND year >= 1990

{r : title | …. }

Page 12: CSE 132A Discussion

SELECT title

FROM Books

WHERE author = ‘EDMUND MORGAN’ AND year >= 1990

{r : title | ∃b ∈ Books [....] }

Page 13: CSE 132A Discussion

SELECT title

FROM Books

WHERE author = ‘EDMUND MORGAN’ AND year >= 1990

{r : title | ∃b ∈ Books [ b(author)= ‘EDMUND MORGAN’ ^ b(year) ≥ 1990] }

Are we done here?

Page 14: CSE 132A Discussion

SELECT title

FROM Books

WHERE author = ‘EDMUND MORGAN’ AND year >= 1990

{r : title | ∃b ∈ Books [ b(author)= ‘EDMUND MORGAN’ ^ b(year) ≥ 1990 ^ b(title) = r(title)] }

Don’t forget about this!

Page 15: CSE 132A Discussion

Example 2What are the titles of the newest books?

Page 16: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | …. }

Page 17: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books [....] }

Page 18: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books [∀o ∈ Books[...]]}

Page 19: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books [∀o ∈ Books[b(years) ≥ o(years)]]}

Done?

Page 20: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books[∀o ∈ Books[b(years) ≥ o(years)]

^ b(title) = r(title)]}

Is this the only solution?

Page 21: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books[¬∃o ∈ Books[b(year)<o(year)]

∧b(title) = r(title)]}

Page 22: CSE 132A Discussion

Example 2What are the titles of the newest books?

{r : title | ∃b ∈ Books[¬∃o ∈ Books[b(year)<o(year)]

∧b(title) = r(title)]}

Translate back to SQL:

Page 23: CSE 132A Discussion

Example 2{r : title | ∃b ∈ Books[¬∃o ∈ Books[b(year)<o(year)] ∧b(title) = r(title)]}

SQL:

SELECT b.title

FROM Books b

WHERE NOT EXISTS (SELECT *

FROM Books o

WHERE b.year < o.year);All books that are newer than b

If there doesn’t exist book that are newer than b, then b is the newest book

Page 24: CSE 132A Discussion

Example 3

What are the titles of the Books which have been Purchased by every Customer?

Page 25: CSE 132A Discussion

Example 3

What are the titles of the Books which have been Purchased by every Customer?

{r :title | ∃b ∈ Books, ∀c ∈ Customers, ∃p ∈ Purchases[c(customerid) = p(customerid) ∧ p(bookid) = b(bookid)∧ b(title) = r(title) ]}

Page 26: CSE 132A Discussion

{r :title | ∃b ∈ Books, ¬∃c ∈ Customers, ¬ (∃p ∈ Purchases[ c(customerid) = p(customerid) ∧ p(bookid) = b(bookid) ∧ b(title) = r(title)) ]}

SELECT b.Title

FROM Books b

WHERE NOT EXISTS (

SELECT ∗

FROM Customers c

WHERE NOT EXISTS (

SELECT ∗

FROM Purchases p

WHERE c . customerid = p . customerid AND p. bookid = b. bookid ));

All purchases records of customer c brought the book b

Customer who didn’t buy book b

Book b that purchased by every customer

Page 27: CSE 132A Discussion

Example 4

Which book(s) are the cheapest?

Page 28: CSE 132A Discussion

Example 4

Which book(s) are the cheapest?

{r :title | ∃b ∈ Books, ∃p ∈ Pricing, ∀ c ∈ Pricing

[p(price) <= c(price) ∧

p(bookid) = b(bookid) ∧

b(title) = r(title)]}

Page 29: CSE 132A Discussion

Example 5

Which battleships launched before 1930 had 16-inch guns? List their names, their country, and the number of guns they carried?

Ships(name, yearLaunched, country, numGuns, gunSize, displacement)

Page 30: CSE 132A Discussion

Example 5

Which battleships launched before 1930 had 16-inch guns? List their names, their country, and the number of guns they carried?

{t :names, country, numGuns|∃s ∈ Ships[ t(name) = s(name) ∧ t(country) = s(country) ∧ t(numGuns) = s(numGuns)∧ s(yearLaunched) < 1930 ∧ s(gunSize) = 16]}

Ships(name, yearLaunched, country, numGuns, gunSize, displacement)

Page 31: CSE 132A Discussion

Any other questions?