CSE 132A Discussion

31
CSE 132A Discussion Week 2: Relational Calculus

Transcript of CSE 132A Discussion

CSE 132A DiscussionWeek 2: Relational Calculus

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

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

Logic Examples

Express “Each unicorn has a horn” in relational calculus

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

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.

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

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)

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

SQL?

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

SELECT title

FROM Books

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

{r : title | …. }

SELECT title

FROM Books

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

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

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?

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!

Example 2What are the titles of the newest books?

Example 2What are the titles of the newest books?

{r : title | …. }

Example 2What are the titles of the newest books?

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

Example 2What are the titles of the newest books?

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

Example 2What are the titles of the newest books?

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

Done?

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?

Example 2What are the titles of the newest books?

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

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

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:

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

Example 3

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

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) ]}

{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

Example 4

Which book(s) are the cheapest?

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)]}

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)

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)

Any other questions?