Lecture 4 Relational Model and Algebra
Lecture 4 Relational Model and Algebra
Course Website
• Blackboard IU
• Please check frequently for updates!
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
1
International University, VNU-HCMC
Outline
• Relational model
• Relational algebra
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Acknowledgement
• The following slides are referenced from Dr. Sudeepa
Roy, Duke University.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
2
International University, VNU-HCMC
[Link]
Duke CS, Fall 202
2022 5
Drinkers Beers
Each has an address (Later in ER diagram – how to Each has a brewer
design a relational database)
Duke CS, Fall 202
2022 6
3
International University, VNU-HCMC See online database for more tuples
4
International University, VNU-HCMC
Beer Frequents
Name brewer drinker bar times_a_week
Budweiser Anheuser-Busch Inc. Ben Satisfaction 2
Corona Grupo Modelo Dan The Edge 1
Dixie Dixie Brewing Dan Satisfaction 2
5
International University, VNU-HCMC
Example
• Schema (metadata)
• Beer (name string, brewer string)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Instance
• Beer {<Budweiser, Anheuser-Busch Inc.>, <corona, Grupo Modelo>,…}
• Serves {<the Edge, Budweiser, 2.50>,<The Edge, Corona, 3.0>,..}
• Frequents {<Ben, Satisfaction,2>,<Dan, The Edge, 1>,…}
Relational algebra
A language for querying relational data
based on “operators”
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
RelOp
RelOp
• Core operators:
• Selection, projection, cross product, union, difference,
and renaming
• Additional, derived operators:
• Join, natural join, intersection, etc.
• Compose operators to make complex queries
Duke CS, Fall 202
2022 12
6
International University, VNU-HCMC
Selection
• Input: a table 𝑅
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Notation: 𝜎P 𝑅
• 𝑝 is called a selection condition (or predicate)
• Purpose: filter rows according to some criteria
• Output: same columns as 𝑅, but only rows of 𝑅 that
satisfy 𝑝 (𝑠𝑒𝑡!)
Selection example
Find beers with price < 2.75
𝝈𝒑𝒓𝒊𝒄𝒆 Serves
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
7
International University, VNU-HCMC
More on selection
• Selection condition can include any column of 𝑅, constants,
comparison (=, ≤, etc.) and Boolean connectives (∧: and, ∨:
or, ¬: not)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Projection
• Input: a table 𝑅
• Notation: 𝜋L 𝑅
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• 𝐿 is a list of columns in 𝑅
• Purpose: output chosen columns
• Output: same rows, but only the columns in 𝐿 (𝑠𝑒𝑡!)
8
International University, VNU-HCMC
Projection
Example: Find all the prices for each beer
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Serves
𝝅𝒃𝒆𝒆𝒓,𝒑𝒓𝒊𝒄𝒆 Serves
bar beer price
The Edge Budweiser 2.50 beer price
Output of 𝜋beerServes?
More on Projection
𝝅𝒃𝒆𝒆𝒓 Serves
Serves
bar beer price beer
The Edge Budweiser 2.50 Budweiser
9
International University, VNU-HCMC
Cross product
• Input: two tables 𝑅 and 𝑆
• Natation: 𝑅×𝑆
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Cross product
Bar Frequents
name address drinker bar times_a_week
The Edge 108 Morris Street Ben Satisfaction 2
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Bar x Frequents
name address drinker bar times_a_
w eek
The Edge 108 Morris Street Ben Satisfaction 2
10
International University, VNU-HCMC
Cross product
• Ordering of columns is unimportant as far as
contents are concerned.
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
name address drink bar tim drinker bar time name address
er es_ s_a_
a_w w
eek eek
The Edge 108 Morris Street Ben Satisfaction 2 Ben Satisfaction 2 The Edge 108 Morris Street
The Edge 108 Morris Street Dan The Edge 1 Dan The Edge 1 The Edge 108 Morris Street
The Edge 108 Morris Street Dan Satisfaction 2 = Dan Satisfaction 2 The Edge 108 Morris Street
Satisfaction 905 W. Main Street Ben Satisfaction 2 Ben Satisfaction 2 Satisfaction 905 W. Main Street
Satisfaction 905 W. Main Street Dan The Edge 1 Dan The Edge 1 Satisfaction 905 W. Main Street
Satisfaction 905 W. Main Street Dan Satisfaction 2 Dan Satisfaction 2 Satisfaction 905 W. Main Street
• Notation: 𝑅 ⋈P 𝑆
• 𝑝 is called a join condition (or predicate)
• Purpose: relate rows from two tables according
to some criteria
• Output: for each row 𝑟 in 𝑅 and each row 𝑠 in 𝑆,
output a row 𝑟𝑠 if 𝑟 and 𝑠 satisfy 𝑝
• Shorthand for 𝜎P (R×𝑆)
• Predicate p only has equality (A = 5 ∧ B = 7) : equijoin
Duke CS, Fall 202
2022 22
11
International University, VNU-HCMC
Join example
• Extend Frequents relation with addresses of the bars
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠 ⋈bar = name 𝐵𝑎𝑟
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Ambiguous attribute? Prefix a column reference with table name and “.” to
disambiguate identically named columns from different tables. Ex. Use [Link]
Bar Frequents
name address drinker bar times_a_week
The Edge 108 Morris Street Ben Satisfaction 2
Satisfaction 905 W. Main Street Dan The Edge 1
Dan Satisfaction 2
Join Types
• Theta Join
• Equi-Join
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Natural Join
12
International University, VNU-HCMC
Serves Likes
drinker beer
bar beer price
The Edge Budweiser 2.50 Amy Corona
The Edge Corona 3.00 Dan Budweiser
Satisfaction Budweiser 2.25 Dan Corona
Ben Budweiser
Serves ⋈ 𝐿𝑖𝑘𝑒𝑠
bar beer price drinker
The Edge Budweiser 2.50 Dan Natural Join is on beer. Only one
The Edge Budweiser 2.50 Ben column for beer in the output
The Edge Corona 3.00 Amy What happens if the tables
The Edge Corona 3.00 Dan have two or more common columns?
... …. …..
13
International University, VNU-HCMC
Union
• Input: two tables 𝑅 and 𝑆 Important for set operations:
Union Compatibility
• Notation: 𝑅 ∪ 𝑆
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example on board
Difference
• Input: two tables 𝑅 and 𝑆 Important for set operations:
• Notation: 𝑅 − 𝑆 Union Compatibility
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Example on board
14
International University, VNU-HCMC
Renaming
• Input: a table 𝑅
• Notation: 𝜌S 𝑅, 𝜌( A 1, A2 ,…)𝑅, or 𝜌S (A1,A 2 ,…)R
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
15
International University, VNU-HCMC
Renaming example
Frequents
• Find drinkers who drinker bar times_a_week
frequent both “The Ben Satisfaction 2
Edge” and “Satisfaction”
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
!
WRONG
𝜋drinker 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠 ⋈ Bar= ‘The Edge ∧ 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
Bar = ‘Satisfaction’∧
drinker = drinker
!
Rename
𝜌 d1, b1, t1 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
𝜋uid1 ⋈b 1 = ‘The Edge’∧ b2 = Satisfaction’ ∧ d1=d2
𝜌 d2, b2, t2 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
Duke CS, Fall 202
2022 31
Bar
name address
Also called logical Plan tree
The Edge 108 Morris Street 𝜋address
Satisfaction 905 W. Main Street
⋈bar=name
Frequents
drinker bar times_a_week 𝜎drinker = ‘Dan’
Ben Satisfaction 2
Dan The Edge 1
𝐵𝑎𝑟 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
Dan Satisfaction 2
Equivalent to
𝜋 address(𝐵𝑎𝑟 ⋈bar = name (𝜎drinker =‘Dan’𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠))
Duke CS, Fall 2022 32
16
International University, VNU-HCMC
• Intersection: 𝑅 ∩ 𝑆
• Many more
• Semijoin, anti-semijoin, quotient, …
17
International University, VNU-HCMC
Frequents(drinker, bar, times_of_week)
Exercise Bar(name, address)
Drinker(name, address)
Bar(name, address)
Exercise Drinker(name, address)
𝜌bar 𝜋bar
⋈ drinker = 𝑛𝑎𝑚𝑒
𝜋n a m e
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠 𝜎address=‘300 [Link] Street
𝐵𝑎𝑟
𝐷𝑟𝑖𝑛𝑘𝑒𝑟
Duke CS, Fall 2022 36
18
International University, VNU-HCMC Frequents(drinker, bar, times_of_week) 41
Bar(name, address)
A trickier Exercise Drinker(name, address)
• For each bar, find the drinkers who frequent it max no. times a
week
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Bar(name, address)
A trickier Exercise Drinker(name, address)
• For each bar, find the drinkers who frequent it max no.
times a week
• Who do NOT visit a bar max no. of times?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
𝜋[Link], [Link]
𝜋bar, drinker
⋈[Link]-of-week < [Link]-of-week
∧[Link]=[Link]
𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
𝜌F1 𝜌F2
A deeper question: 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠 𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑡𝑠
When (and why) is “−” needed?
Duke CS, Fall 2022 38
19
International University, VNU-HCMC
20
International University, VNU-HCMC
Find sailors who’ve reserved a red or a green
boat
Sailors(sid, sname, rating, age)
Boats(bid, bname, color) Use of rename operation
Reserves(sid, bid, day)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
r (Tempboats, (s color =' red ' Ú color =' green ' Boats))
Division
• Not supported as a primitive operator, but useful for
expressing queries like:
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
21
International University, VNU-HCMC
s1 p2 B1 p4 p2
s1 p3 B2 p4
s1 p4
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1
22
International University, VNU-HCMC
Find the name of sailors who’ve reserved
all boats
Sailors(sid, sname, rating, age)
Boats(bid, bname, color)
Reserves(sid, bid, day)
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
Monotone operators
RelOp What happens
Add more rows to the output?
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
to the input...
23
International University, VNU-HCMC 44
• Selection: 𝜎P 𝑅 Monotone
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
• Projection: 𝜋L 𝑅 Monotone
• Cross product: 𝑅×𝑆 Monotone
• Join: 𝑅 ⋈P 𝑆 Monotone
• Natural join: 𝑅 ⋈ 𝑆 Monotone
• Union: 𝑅 ∪ 𝑆 Monotone
• Difference: 𝑅 − 𝑆 Monotone w.r.t. 𝑅; non-monotone w.r.t 𝑆
• Intersection: 𝑅 ∩ 𝑆 Monotone
24
International University, VNU-HCMC
25
International University, VNU-HCMC
26
International University, VNU-HCMC
Assoc. Prof. Nguyen Thi Thuy Loan, PhD
27