0% found this document useful (0 votes)
21 views34 pages

5 RA and Datalog Post 3

The document outlines important deadlines for a tutorial and milestone submissions, including a recap of relational algebra and Datalog concepts. It discusses the division operation in relational algebra, providing examples and exercises related to querying databases. Additionally, it includes clicker questions to engage participants in understanding the division operation and its applications in database queries.

Uploaded by

Roy Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views34 pages

5 RA and Datalog Post 3

The document outlines important deadlines for a tutorial and milestone submissions, including a recap of relational algebra and Datalog concepts. It discusses the division operation in relational algebra, providing examples and exercises related to querying databases. Additionally, it includes clicker questions to engage participants in understanding the division operation and its applications in database queries.

Uploaded by

Roy Chen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Reminders and Important Dates

Feb 27, 2020


Upcoming deadlines

• February 25 to 28 : Tutorial 4 is due


• February 27 @ 11:59PM: Last day to submit milestone 2 regrade
requests
• February 28 @ 11:59PM Milestone 3 – Normalization

• RA and Datalog practice exercises are available on canvas –


Please check them out!

Please check piazza post @7 for Important Dates and Deadlines

Relational Algebra and Datalog 1


Todays outline

1. Recap

2. Division

3. Try out activities

4. Revisit learning goals

Relational Algebra and Datalog 2


Previously…
• NOT
• We may put NOT in front of a subgoal, to negate its
meaning.
Bad(I) ←Ord (32,𝐼,𝐷,𝑄)
Good(𝐼) ← Ord(C,I,D,Q), ¬Bad(I) .

• Join (Condition, Equi-, Natural) (⋈)

• Assignment ()

• Anonymous variables in Datalog ( _ )

Relational Algebra and Datalog 3


Find the name of actors who have been in
“Indiana Jones” and “Star Wars”
Movie StarsIn MovieStar
Movie Title Year MovieI StarI Character StarID Name Gender
ID D D 1 Harrison Male
1 Star Wars 1977 Ford
1 1 Han Solo
2 Gone with the Wind 1939 2 Vivian Female
4 1 Indiana Jones
3 The Wizard of Oz 1939 Leigh
2 2 Scarlett O’Hara 3 Judy Female
4 Indiana Jones and the 1981
Raiders of the Lost
3 3 Dorothy Gale Garland
Ark

RA?
Indy  π starID((σ Title = “Indiana Jones and …” Movie) ⋈ StarsIn)
StarWars  π starID((σ Title = “Star Wars” Movie) ⋈ StarsIn)

CoolPeopleIndy ∩ StarWars
π name(CoolPeople ⋈ MovieStar)

Relational Algebra and Datalog 4


Clicker Exercise
Movie StarsIn MovieStar
Movie Title Year MovieI StarI Character StarID Name Gender
ID D D 1 Harrison Male
1 Star Wars 1977 Ford
1 1 Han Solo
2 Gone with the Wind 1939 2 Vivian Female
4 1 Indiana Jones
Leigh
3 The Wizard of Oz 1939
2 2 Scarlett O’Hara 3 Judy Female
4 Indiana Jones and the 1981
3 3 Dorothy Gale Garland
Raiders of the Lost
Ark

Find the names of actors who have been in a movie with the same title as
the actor’s name. Which of the following is the correct option?
A. πName((Movie⋈ StarsIn) ⋈ title = name ^ StarID = MovieStar.StarID MovieStar)
B. πName(MovieStar⋈ Name = title ^ MovieStar.StarID = StarID (StarsIn⋈ Movie))
C. πName((StarsIn⋈ (π StarID,NameMovieStar)) ⋈MovieID = Movie.MovieID ^ title =
name Movie)
D. All are correct
E. None are correct
Relational Algebra and Datalog 5
Division
• Notation: r / s or rs
• Useful for expressing queries that include a “for all” or
“for every” phrase,

• e.g., Find movie stars who were in all movies.

• Can be expressed in terms of projection, set difference,


and cross-product

Relational Algebra and Datalog 6


Division
• Goal: Produce the tuples in one relation, r, that match all
tuples in another relation, s
• r (A1, …An, B1, …Bm)
• s (B1 …Bm)

• r  s, with attributes A1, …An, is the set of all tuples <a> such that
for every tuple <b> in s, <a,b> is in r

• r ÷ s contains the (parts of) tuples of r that are associated


with every tuple in s.

Relational Algebra and Datalog 7


Division

Relational Algebra and Datalog 8


Examples of Division A/B

A/B
• Attributes of B must be a subset
of the attributes of A

• This will find set of tuples from A


that has a match combination of
every tuple in B

A/B1?
Relational Algebra and Datalog 9
Examples of Division A/B

A/B
• Attributes of B must be a subset
of the attributes of A

• This will find set of tuples from A


that has a match combination of
every tuple in B

A/B2?
Relational Algebra and Datalog 10
Examples of Division A/B

A/B
• Attributes of B must be a subset
of the attributes of A

• This will find set of tuples from A


that has a match combination of
every tuple in B

A/B3?
Relational Algebra and Datalog 11
Division Clicker Question
Consider the relations
C D E A B ???
R S 1 2
T 2
1 2 1
2 2
2 2 1
3 2
3 2 1
1 1

Which of the following is a possible expression for creating T?


A. X(D) πAS
R(C,D)/X
B. Y(A)πCR
S(B,A)/Y
C. Z(C)πAS
R(E,C)/Z
D. All of the above
E. None of the above

Relational Algebra and Datalog 12


Division Clicker Question
Answer A exposed
C D E A B ???
R S 1 2
T 2
1 2 1
2 2
2 2 1
3 2
3 2 1
1 1

Which of the following is a possible expression for creating T?


A. X(D) πAS
R(C,D)/X C D D

B. Y(A)πCR 1 2 1

S(B,A)/Y 2 2 2

C. Z(C)πAS 3 2 3

R(E,C)/Z
D. All of the above
E. None of the above Nothing
Relational Algebra and Datalog 13
Division Clicker Question
Answer B exposed
C D E A B ???
R S 1 2
T 2
1 2 1
2 2
2 2 1
3 2
3 2 1
1 1

Which of the following is a possible expression for creating T?


A. X(D) πAS
R(C,D)/X B A A

B. Y(A)πCR 2 1 1

S(B,A)/Y 2 2 2

C. Z(C)πAS 2 3 3

R(E,C)/Z 1 1

D. All of the above


E. None of the above Right
Relational Algebra and Datalog 14
Division Clicker Question
Answer C exposed
C D E A B ???
R S 1 2
T 2
1 2 1
2 2
2 2 1
3 2
3 2 1
1 1

Which of the following is a possible expression for creating T?


A. X(D) πAS
R(C,D)/X E C C

B. Y(A)πCR 1 1 1

S(B,A)/Y 1 2 2

C. Z(C)πAS 1 3 3

R(E,C)/Z
D. All of the above
No, its 1 … we want 2
E. None of the above

Relational Algebra and Datalog 15


Division Clicker Question
Consider the relations
C D E A B ???
R S 1 2
T 2
1 2 1
2 2
2 2 1
3 2
3 2 1
1 1

Which of the following is a possible expression for creating T?


A. X(D) πAS
R(C,D)/X nothing
B. Y(A)πCR
S(B,A)/Y right
C. Z(C)πAS
R(E,C)/Z No, 1
D. All of the above
E. None of the above

Relational Algebra and Datalog 16


Division
Consider below relation:
cust(cid, cname, rating, salary)
ord(cid, iid, day, qty)
item(iid, iname, price)
Query: List the ids of items that are ordered by every customer

Numerator:
iid and cid for every order placed by every customer
iid, cid (Ord )
Denominator:
cid of all customers
cid ( Cust )
Result is numerator/denominator
𝜋𝑖𝑖𝑑,𝑐𝑖𝑑 𝑜𝑟𝑑 ÷ 𝜋𝑐𝑖𝑑 (𝐶𝑢𝑠𝑡))
Relational Algebra and Datalog 17
Find the names of actors who have
been in all movies. Try!

RA?

InAll πStarID, MovieID (StarsIn) ÷ πMovieID (Movie)

πName(InAll ⋈MovieStar)

Relational Algebra and Datalog 18


Find the names of actors who have
been in all movies after 1950

RA?

LateMovieIds  π MovieID(σ year > 1950(Movie))

InAll (πStarID, MovieID (StarsIn) ÷ LateMovieIds)

πName(InAll ⋈MovieStar)

Relational Algebra and Datalog 19


Division – without cust(cid, cname, rating, salary)
ord(cid, iid, day, qty)
division operator item(iid, iname, price)

Query: Find items (iid) that are ordered by every customer


cust item ord
𝜋𝑖𝑖𝑑,𝑐𝑖𝑑 𝑜𝑟𝑑 ÷ 𝜋𝑐𝑖𝑑 (𝐶𝑢𝑠𝑡)) C1 … I1 … C1 I1 … …
C2 … I2 … C2 I1 … …
C3 … C3 I1 … …
C1 I2 … …
all combos
all items (cid, iid) pairs
(of cid’s and iid’s)
Such that iid was not ordered by cid.

𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 − 𝜋𝑖𝑖𝑑 ((𝜋𝑐𝑖𝑑 𝑐𝑢𝑠𝑡 × 𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 ) - 𝜋𝑐𝑖𝑑,𝑖𝑖𝑑 𝑜𝑟𝑑 )

items ordered by
every customer
Witness
(that an Item was ordered by
items not ordered by someone someone)

Relational Algebra and Datalog 20


Division – without cust(cid, cname, rating, salary)
ord(cid, iid, day, qty)
division operator item(iid, iname, price)

Query: Find items (iid) that are ordered by every customer


cust item ord
𝜋𝑖𝑖𝑑,𝑐𝑖𝑑 𝑜𝑟𝑑 ÷ 𝜋𝑐𝑖𝑑 (𝐶𝑢𝑠𝑡)) C1 … I1 … C1 I1 … …
C2 … I2 … C2 I1 … …
C3 … C3 I1 … …
C1 I2 … …

𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 − 𝜋𝑖𝑖𝑑 ((𝜋𝑐𝑖𝑑 𝑐𝑢𝑠𝑡 × 𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 ) - 𝜋𝑐𝑖𝑑,𝑖𝑖𝑑 𝑜𝑟𝑑 )

Relational Algebra and Datalog 21


cust(cid, cname, rating, salary)
ord(cid, iid, day, qty)
Datalog - Division item(iid, iname, price)

𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 − 𝜋𝑖𝑖𝑑 ((𝜋𝑐𝑖𝑑 𝑐𝑢𝑠𝑡 × 𝜋𝑖𝑖𝑑 𝑜𝑟𝑑 ) - 𝜋𝑐𝑖𝑑,𝑖𝑖𝑑 𝑜𝑟𝑑 )

Relational Algebra and Datalog 22


Try(1)!
Consider a database consisting of the relations, where the primary key of each
relation is underlined.
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)

Query : Find the sin and name of persons who bought every product in the
category “electronics”.

RA

Relational Algebra and Datalog 23


Try(1)!
Consider a database consisting of the relations, where the primary key of each
relation is underlined.
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)

Query : Find the sin and name of persons who bought every product in the
category “electronics”.

RA

goodPersons.buyer-sin = Person.sin

Relational Algebra and Datalog 24


Try(1) - Alternate Solution
Find the sin and name of persons who bought every product
in the category “electronics”.

Relational Algebra and Datalog 25


Try(2)!
Consider a database consisting of the relations, where the primary key of each
relation is underlined.
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)

Query : Find the sin and name of persons who bought every product in the
category “electronics”.

Datalog

Relational Algebra and Datalog 26


Try(2)!
Consider a database consisting of the relations, where the primary key of each
relation is underlined.
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)

Query : Find the sin and name of persons who bought every product in the
category “electronics”.

Datalog 𝑤𝑖𝑡𝑛𝑒𝑠𝑠 𝑆, 𝑃 ← 𝑝𝑢𝑟𝑐ℎ𝑎𝑠𝑒 𝑆, _, _, 𝑃 .


𝑏𝑎𝑑 𝑆 ← 𝑝𝑒𝑟𝑠𝑜𝑛 𝑆, _, _, _ , 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃, _, _, `𝑒𝑙𝑒𝑐𝑡𝑟𝑜𝑛𝑖𝑐𝑠 ′ , _ ,
¬𝑤𝑖𝑡𝑛𝑒𝑠𝑠 𝑆, 𝑃 .
𝑔𝑜𝑜𝑑 𝑆 ← 𝑝𝑒𝑟𝑠𝑜𝑛 𝑆, _, _, _ , ¬𝑏𝑎𝑑 𝑆 .
𝑎𝑛𝑠 𝑆, 𝑃 ← 𝑔𝑜𝑜𝑑 𝑆 , 𝑝𝑒𝑟𝑠𝑜𝑛 𝑆, 𝑁, _, _ .

Relational Algebra and Datalog 27


Try(3)!
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)
Query : Find pairs of products (pid’s) of the same category but with
different makers. Avoid duplicate pairs, e.g., (p1, p2) and (p2, p1) shouldn’t
both appear in the answer.

RA

Datalog

Relational Algebra and Datalog 28


Try(3)!
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)
Query : Find pairs of products (pid’s) of the same category but with
different makers. Avoid duplicate pairs, e.g., (p1, p2) and (p2, p1) shouldn’t
both appear in the answer.

RA 𝜋𝑝𝑖𝑑,𝑃1.𝑝𝑖𝑑 𝜎𝑝𝑖𝑑<𝑃1.𝑝𝑖𝑑
[𝑃𝑟𝑜𝑑𝑢𝑐𝑡 ⋈𝑐𝑎𝑡𝑒𝑔𝑜𝑟𝑦=𝑃1.𝑐𝑎𝑡𝑒𝑔𝑜𝑟𝑦∧𝑚𝑎𝑘𝑒𝑟𝑖𝑑≠𝑃1.𝑚𝑎𝑘𝑒𝑟𝑖𝑑 𝜌𝑃1 (𝑃𝑟𝑜𝑑𝑢𝑐𝑡)]

Dat 𝑎𝑛𝑠 𝑃, 𝑃′ ← 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃, _, _, 𝐶, 𝑀 , 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃′ , _, _, 𝐶, 𝑀′ , 𝑀 ≠ 𝑀′ , 𝑃 < 𝑃′ .


alog

Relational Algebra and Datalog 29


Try(4)!
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)
Query : Find the pid of the product under category laptop that had
the highest price within its category.

RA

Datalog

Relational Algebra and Datalog 30


Try(4)!
• Product ( pid, name, price, category, maker-cid)
• Purchase (buyer-sin, seller-sin, store, pid)
• Company (cid, name, stock price, country)
• Person(sin, name, phone number, city)
Query : Find the pid of the product under category laptop that had
the highest price within its category.

RA 𝑙𝑎𝑝𝑡𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑠 ← 𝜋𝑝𝑖𝑑,𝑝𝑟𝑖𝑐𝑒 𝜎𝑐𝑎𝑡𝑒𝑔𝑜𝑟𝑦=`𝑙𝑎𝑝𝑡𝑜𝑝′ 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 .


𝑛𝑜𝑡𝑇𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑑← 𝜋𝑝𝑖𝑑 𝑙𝑎𝑝𝑡𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑠 ⋈𝑝𝑟𝑖𝑐𝑒<𝐿1.𝑝𝑟𝑖𝑐𝑒 𝜌𝐿1 𝑙𝑎𝑝𝑡𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑠 .
𝑎𝑛𝑠𝑤𝑒𝑟← 𝜋𝑝𝑖𝑑 𝑙𝑎𝑝𝑡𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑠 − 𝑛𝑜𝑡𝑇𝑜𝑝𝑃𝑟𝑖𝑐𝑒𝑑.

Datalog 𝑏𝑎𝑑 𝑃 ← 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃, _, 𝑅, `𝑙𝑎𝑝𝑡𝑜𝑝′ , _ , 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃′ , _, 𝑅′ , `𝑙𝑎𝑝𝑡𝑜𝑝′ , _ , 𝑅 < 𝑅′ .


𝑎𝑛𝑠 𝑃 ← 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 𝑃, _, _, `𝑙𝑎𝑝𝑡𝑜𝑝′ , _ , ¬𝑏𝑎𝑑 𝑃 .

Relational Algebra and Datalog 31


RA and Datalog diff
Comparison between Datalog and relational algebra

1) Every expression in the basic relational algebra can be


expressed as a Datalog query.

2) But operations in the extended relational algebra (grouping,


aggregation, and sorting) have no corresponding features in the
version of Datalog.

3) Similarly, Datalog can express recursion, which relational


algebra cannot.

Relational Algebra and Datalog 32


Summary
• Relational algebra is a procedural language providing a set of
operations on relations. A query is expressed as a sequence of
these operations.

• RA is complete in the sense that any “reasonable” query can be


expressed in it.

• RA is used by commercial languages to define query evaluation


plans.

• Datalog is a declarative language based on logic.

Relational Algebra and Datalog 33


Learning Goals Revisited
1. Identify the basic operators in Relational Algebra (RA).

2. Use RA to create queries that include combining RA operators.

3. Given an RA query and table schemas and instances, compute the


result of the query.

4. Express a DB query in Datalog translate RA queries to Datalog

5. Translate queries from a fragment of Datalog to RA.

Relational Algebra and Datalog 34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy