relaonal( algebra&(...
TRANSCRIPT
![Page 1: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/1.jpg)
rela%onal algebra & calculus
![Page 2: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/2.jpg)
Relational DB: The Origins
2
Frege: FO logic Tarski: Algebra for FO
Codd: Rela%onal databases
![Page 3: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/3.jpg)
rela%onal calculus
![Page 4: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/4.jpg)
Relational Calculus (aka FO) • Models data manipulation core of SQL
Idea: specify “what” not “how”
• General form: {t | property (t)}
• property (t) is described by a language based on predicate calculus (first-order logic)
4
![Page 5: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/5.jpg)
Relational Calculus Example
In SQL
SELECT * FROM Movie
In words (making answer tuple explicit)
The answer consists of tuples m such that m is a tuple in Movie
Need to say
“tuple m is in relation R”: m ∈ R
Display the movie table
5
![Page 6: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/6.jpg)
Relational Calculus Example
In SQL SELECT m.Director, m.Actor FROM movie m, schedule s WHERE m.Title = s.Title
In words (making answer tuple explicit)
“The answer consists of tuples t s.t. there exist tuples m in movie and s in schedule for which t.Director = m.Director and t.Actor = m.Actor and m.Title = s.Title”
Need to say
“there exists a tuple x in relation R”: ∃ x ∈ R Refer to the value of attribute A of tuple x: x(A) Boolean combinations
Find the directors and actors of currently playing movies
6
![Page 7: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/7.jpg)
Relational Calculus Example
Need to say
“there exists a tuple x in relation R”: ∃ x ∈ R Refer to the value of attribute A of tuple x: x(A) Boolean combinations
Find the directors and actors of currently playing movies
In logic notation (tuple relational calculus)
{ t: Director, Actor | ∃ m ∈ movie ∃ s ∈ schedule [ t(Director) = m(Director) ∧ t(Actor) = m(Actor) ∧ m(Title) = s(Title) ] }
7
![Page 8: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/8.jpg)
Quantifiers ∃ m ∈ R: Existential quantification “there exists some tuple m in relation R ….” Sometimes need to say: “for every tuple m ….”
e.g., “every director is also an actor”
Need to say: “for every tuple m in movie there exists a tuple t in movie Such that m.Director = t.Actor”
∀ m ∈ movie ∃ t ∈ movie [ m(Director) = t(Actor) ]
(The answer to this query is true or false) ∀ m ∈ R: Universal quantification “for every tuple m in relation R ….”
8
![Page 9: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/9.jpg)
Tuple Relational Calculus • In the style of SQL: language talks about tuples
• What you can say: - Refer to tuples: tuple variables t, s, … - A tuple t belongs to a relation R: t ∈ R - Conditions on attributes of a tuple t and s:
• t(A) = (≠)(≥) constant • t(A) = s(B) • t(A) ≠ s(B) • etc.
• Simple expressions above: atoms
9
![Page 10: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/10.jpg)
Tuple Relational Calculus • Combine properties using Boolean operators ∧, ∨, ¬ (abbreviation: p → q ≡ ¬ p ∨ q)
• Quantifiers there exists: ∃t ∈ R ϕ(t) for every: ∀t ∈ R ϕ(t) where ϕ(t) a formula in which t not quantified (it is “free”)
10
![Page 11: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/11.jpg)
More on quantifiers • Scope of quantifier:
scope of ∃t ∈ R ϕ(t) is ϕ scope of ∀t ∈ R ϕ(t) is ϕ
• Free variable: not in scope of any quantifier free variables are the “parameters” of the formula
• Rule: in quantification ∃t ∈ R ϕ(t), ∀t ∈ R ϕ(t) t must be free in ϕ
11
![Page 12: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/12.jpg)
Quantifier Examples { t: Director, Actor | ∃ m ∈ movie ∃ s ∈ schedule [ t(Director) = m(Director) ∧ t(Actor) = m(Actor) ∧ m(Title) = s(Title) ] }
[ t(Director) = m(Director) ∧ t(Actor) = m(Actor) ∧ m(Title) = s(Title) ] free: t, m, s
∃ s ∈ schedule [ t(Director) = m(Director) ∧ t(Actor) = m(Actor) ∧ m(Title) = s(Title) ]
free: t, m
∃ m ∈ movie ∃ s ∈ schedule [ t(Director) = m(Director) ∧ t(Actor) = m(Actor) ∧ m(Title) = s(Title) ]
free: t
12
![Page 13: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/13.jpg)
Example in predicate logic
13
A statement about numbers: ∃ x ∀ y ∀ z [ x = y * z ((y = 1) ∨ (z = 1))]
“there exists at least one prime number x”
A “query” on numbers:
ϕ(x): ∀ y ∀ z [ x = y * z ((y = 1) ∨ (z = 1))] This defines the set {x | ϕ(x)} of prime numbers. It consists of all x that make ϕ(x) true.
![Page 14: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/14.jpg)
Semantics of Tuple Calculus • Active domain:
A set of values in the database, or mentioned in the query result. Tuple variables range over the active domain
• Note: A query without free variables always evaluates to true or false
e.g., “Sky is by Berto” is expressed without free variables: ∃m ∈ movie [m(title) = “Sky” ∧ m(director) = “Berto”] This statement is true or false
14
![Page 15: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/15.jpg)
Tuple Calculus Query {t: <att> | ϕ(t)} where ϕ is a calculus formula with only one free variable t produces as answer a table with attributes <att> consisting of all tuples v in active domain with make ϕ(v) true Note: ϕ(v) has no free variables so it evaluates to true or false
15
![Page 16: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/16.jpg)
Movie Examples Revisited Find titles of currently playing movies
"Find the titles and the directors of all currently playing movies
Find the titles of all movies by “Berto”
select Titlefrom Schedule
select Title from Movie where Director=“Berto”
select Movie.Title, Directorfrom Movie, Schedule where Movie.Title = Schedule.Title"
16
![Page 17: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/17.jpg)
Movie Examples Revisited Find titles of currently playing movies
"Find the titles and the directors of all currently playing movies
Find the titles of all movies by “Berto”
{t: title | ∃s ∈schedule [s(title) = t(title)]}!
{t: title| ∃m ∈ movie [m(director) = “Berto” ∧ t(title) = m(title)]}!
{t: title, director | ∃s ∈schedule ∃m ∈ movie![s(title) = m(title) ∧ t(title) = m(title) ∧ t(director) = m(director)]}!
17
![Page 18: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/18.jpg)
Movie Examples Revisited • Find actors playing in every movie by Berto
{a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie [m(director) = “Berto” → ∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
Is the following correct?
{a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧
∀m ∈ movie [m(director) = “Berto” ∧ ∃t ∈ movie (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]} A: YES B: NO
18
![Page 19: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/19.jpg)
Movie Examples Revisited • Find actors playing in every movie by Berto
{a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie [m(director) = “Berto” → ∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
Typical use of ∀: ∀ m ∈ R [ filter(m) → property(m)]
Intui%on: check property(m) for those m that sa%sfy filter(m) we don’t care about the m’s that do not sa%sfy filter(m)
19
![Page 20: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/20.jpg)
Movie Examples Revisited
Is this correct? {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie ∃t ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]}
A: YES B: NO
20
• Find actors playing in every movie by Berto
{a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie [m(director) = “Berto” → ∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
![Page 21: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/21.jpg)
Movie Examples Revisited
∃t (ϕ ∨ ψ) = ∃t ϕ ∨ ∃t ψ ∃t ϕ = ϕ if t does not occur in ϕ
Is the following correct: ∃t (ϕ ∧ ψ) = ∃t ϕ ∧ ∃t ψ A: YES B: NO
21
Is this correct? {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie ∃t ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]}
A: YES B: NO
![Page 22: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/22.jpg)
Movie Examples Revisited
∃t (ϕ ∨ ψ) = ∃t ϕ ∨ ∃t ψ ∃t ϕ = ϕ if t does not occur in ϕ
∃t ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))] = ∃t ∈ movie [ ¬m(director) = “Berto” ∨ (m(%tle) = t(%tle) ∧ t(actor) = y(actor))] = [∃t ∈ movie (¬m(director) = “Berto” ) ∨ ∃t ∈ movie (m(%tle) = t(%tle) ∧ t(actor) = y(actor))] = [¬m(director) = “Berto” ∨ ∃t ∈ movie (m(%tle) = t(%tle) ∧ t(actor) = y(actor))] = [m(director) = “Berto” → ∃t ∈ movie (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]
22
Correct: {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie ∃t ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]}
![Page 23: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/23.jpg)
Movie Examples Revisited
Is this also correct (can we switch ∀ and ∃)? {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∃t ∈ movie ∀m ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]}
A: YES B: NO
23
Correct: {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie ∃t ∈ movie [m(director) = “Berto” → (m(%tle) = t(%tle) ∧ t(actor) = y(actor))]]}
![Page 24: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/24.jpg)
Tuple Calculus and SQL • Example:
“Find theaters showing movies by Bertolucci”: SQL:
SELECT s.theater FROM schedule s, movie m WHERE s.title = m.title AND m.director = “Bertolucci”
tuple calculus: { t: theater | ∃ s ∈ schedule ∃ m ∈ movie [ t(theater) = s(theater) ∧ s(title) = m(title) ∧ m(director) = Bertolucci ] }
24
![Page 25: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/25.jpg)
Basic SQL Query SQL • SELECT A1, …, An
FROM R1, …, Rk WHERE cond(R1, …, Rk)
Tuple Calculus • {t: A1, …, An | ∃r1 ∈ R1 … ∃rk ∈ Rk [∧j t(Aj) = rij(Aj) ∧ cond(r1, …, rk)]} • Note:
- Basic SQL query uses only ∃ - No explicit construct for ∀
25
![Page 26: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/26.jpg)
Using Tuple Calculus to Formulate SQL Queries Example: “Find actors playing in every movie by Berto”
• Tuple calculus {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ∀m ∈ movie [m(dir) = “Berto” → ∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
• Eliminate ∀: {a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ¬∃m ∈ movie [m(dir) = “Berto” ∧ ¬∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
• Rule: ∀x ∈ R ϕ(x) ≡ ¬∃x ∈ R ¬ϕ(x) “every x in R sa%sfies ϕ(x) iff there is no x in R that violates ϕ(x)”
26
![Page 27: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/27.jpg)
Convert to SQL query • Basic rule: one level of nesting for each “¬∃” SELECT y.actor FROM movie y WHERE NOT EXISTS
(SELECT * FROM movie m WHERE m.dir = ‘Berto’ AND NOT EXISTS (SELECT * FROM movie t WHERE m.title = t.title AND t.actor = y.actor ))
{a: actor | ∃y ∈ movie [a(actor) = y(actor) ∧ ¬∃m ∈ movie [m(dir) = “Berto” ∧ ¬∃t ∈ movie (m(title) = t(title) ∧ t(actor) = y(actor))]]}
27
![Page 28: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/28.jpg)
Another possibility (with similar nesting structure) SELECT actor FROM movie WHERE actor NOT IN
(SELECT s.actor FROM movie s, movie m WHERE m.dir = ‘Berto’ AND s.actor NOT IN (SELECT t.actor FROM movie t WHERE m.title = t.title ))
• Note: Calculus is more flexible than SQL because of
the ability to mix ∃ and ∀ quantifiers
28
![Page 29: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/29.jpg)
rela%onal algebra
![Page 30: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/30.jpg)
Query Processing 3 steps:
• Parsing & Translation • Optimization • Evaluation
30
![Page 31: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/31.jpg)
Relational Algebra • Simple set of algebraic operations on relations
Journey of a query
SQL select … from…where
Rela%onal algebra π13(P⨝Q) ⨝ … Query rewri%ng π14(P⨝S) ⨝ Q ⨝ R
• We use set seman2cs (no duplicates) and no nulls • There are extensions with bag seman%cs and nulls
31
![Page 32: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/32.jpg)
Projection Relational Algebra
Eliminate some columns
πX(R) Display only attributes X of relation R
32
where R: table name & X ⊆ attributes(R)
Example: Find titles of current movies
πTITLE(SCHEDULE)
![Page 33: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/33.jpg)
Projection Relational Algebra
Eliminate some columns
πX(R) Display only attributes X of relation R
33
where R: table name & X ⊆ attributes(R)
Example:
A B C 0 1 2 0 2 2 1 3 1 0 1 3
R A 0 1
A B 0 1 0 2 1 3
πA(R) = πAB(R) = No repetitions of tuples!
![Page 34: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/34.jpg)
Selection Relational Algebra
Compute set union
σcond(R) Select tuples of R satisfying condition cond
34
where cond: condition involving only attributes of R (e.g., attr = value, attr ≠ value, attr1 = attr2, attr1 ≠ attr2, etc.)
Example:
σΑ=0(R) = A B C 0 1 2 0 2 2 1 3 1 0 1 3
R A B C 0 1 2 0 2 2 0 1 3
σB=C(R) = A B C 0 2 2
![Page 35: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/35.jpg)
Selection Relational Algebra
Compute set union
σcond(R) Select tuples of R satisfying condition cond
35
where cond: condition involving only attributes of R (e.g., attr = value, attr ≠ value, attr1 = attr2, attr1 ≠ attr2, etc.)
Example:
σΑ≠0(R) = A B C 0 1 2 0 2 2 1 3 1 0 1 3
R A B C 1 3 1
![Page 36: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/36.jpg)
Union Relational Algebra
Compute set union
R∪S Union of sets of tuples in R and S
36
where R, S: tables with same attributes
Example:
A B α 1 α 2 β 1
R R∪S = A B α 2 β 3
S A B α 1 α 2 β 1 β 3
![Page 37: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/37.jpg)
Difference Relational Algebra
Compute set difference
R - S Difference of sets of tuples in R and S
37
where R, S: tables with same attributes
Example:
A B α 1 α 2 β 1
R R - S = A B α 2 β 3
S A B α 1 β 1
![Page 38: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/38.jpg)
Join Relational Algebra
Compute join
R⨝S Natural Join of R, S
38
where R, S: tables
Example:
A B R R⨝S = B C S A B C
Note: More than one common attributes allowed!
![Page 39: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/39.jpg)
Join Relational Algebra
Compute join
R⨝S Natural Join of R, S
39
where R, S: tables
Example:
A B 0 1 0 2 5 3
R R⨝S = B C 1 2 1 3 2 2
S A B C0 1 20 1 30 2 2
![Page 40: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/40.jpg)
Let r and s be relations on schemas R and S respectively. Then, r⨝s is a relation with attributes att(R) ∪ att(S) obtained as follows: Consider each pair of tuples tr from r and ts from s. If tr and ts have the same value on each of the attributes in att(R) ∩ att(S),
add a tuple t to the result, where
• t has the same value as tr on r • t has the same value as ts on s
Note: if R ∩ S is empty, the join consists of all combinations of tuples from R and S, i.e. their cross-product
Definition of Join
![Page 41: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/41.jpg)
Attribute Renaming Relational Algebra
Rename attributes
δA1→A2(R) Change name of attribute A1 in rel. R to A2
41
where R: relation and A1: attribute in R
Example:
A B α 1 α 2 β 1
R δA→C(R) = C B α 1 α 2 β 1
Contents remain unchanged!
Note: Can rename several attributes at once
![Page 42: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/42.jpg)
Relational Algebra • Basic set of operations:
π, σ, ∪, -, ⨝, δ • Back to movie example queries:
1. Titles of currently playing movies: πTITLE(schedule) 2. Titles of movies by Berto: πTITLE(σDIR=BERTO(movie)) 3. Titles and directors of currently playing movies: πTITLE, DIR (movie ⨝ schedule)
42
![Page 43: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/43.jpg)
4. Find the pairs of actors acting together in some movie πactor1, actor2 (δ actor à actor1 (movie) ⨝ δ actor à actor2 (movie)) 5. Find the actors playing in every movie by Berto πactor (movie) – πactor [(πactor (movie) ⨝ πtitle (σdir =BERTO(movie))) - πactor,title (movie)]
actors for which there is a movie by Berto in which they do not act
actor %tle by Berto actor acts in %tle
43
Relational Algebra
In this case (not in general): Same as cartesian product
![Page 44: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/44.jpg)
Cartesian Product Relational Algebra
Compute cartesian product
R×S Cartesian Product of R, S
44
where R, S: tables
Example:
A B 0 1 0 2
R R⨝S = C D 1 2 1 3
S A B C D0 1 1 20 1 1 30 2 1 20 2 1 3Same as R⨝S, when R and S
have no common attributes
![Page 45: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/45.jpg)
Cartesian Product Relational Algebra
Compute cartesian product
R×S Cartesian Product of R, S
45
where R, S: tables
Example:
A B 0 1 0 2
R R⨝S = A C 1 2 1 3
S R.A B S.A C0 1 1 20 1 1 30 2 1 20 2 1 3If 2 attributes in R, S have the same
name A, they are renamed to R.A and S.A in the output
![Page 46: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/46.jpg)
Other useful operations • Intersection R ∩ S • Division (Quotient) R ÷ S
R ÷ S: {a | <a, b> ∈R for every b∈S}
46
A B 0 α 0 β 1 α 1 β 1 γ 2 α
R B α β
S Α 0 1
Example: R ÷ S =
A B R B S
![Page 47: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/47.jpg)
Another Division Example
Find the actors playing in every movie by Berto"
πTITLE, ACTOR(movie) ÷ πTITLE(σDIR=BERTO(movie))
![Page 48: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/48.jpg)
Division by multiple attributes
A! B!
α!α!α!β!β!γ!γ!γ!
a"a"a"a"a"a"a"a!
C! D!
α!γ!γ!γ!γ!γ!γ!β!
a"a"b"a"b"a"b"b!
E!
1!1!1!1!3!1!1!1!
Relations r, s:"
r ÷ s:"
D!
a"b!
E!
1!1!
A! B!
α!γ!
a"a!
C!
γ!γ!
r! s!
![Page 49: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/49.jpg)
Relational Algebra • Note: π is like ∃ “there exists”… ÷ is like ∀ “for all”…
• Expressing ÷ using other operators:
R A B S B
Similar to: ∀x ϕ(x) ≡ ¬∃x ¬ϕ(x)
R ÷ S = πA(R) -‐ πA((πA(R) ⨝ S) -‐ R)
49
![Page 50: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/50.jpg)
Calculus Vs. Algebra • Theorem: Calculus and Algebra are equivalent
• Basic Correspondence:
51
Algebra Operation Calculus Operation
π
σ
∪
⨝
-
÷
∃ t(A) comp c
∨
∧
¬
∀
![Page 51: relaonal( algebra&( calculus(cseweb.ucsd.edu/classes/fa19/cse132A-a/slides/relational...Relational Algebra Compute set union σ cond(R) Select tuples of R satisfying condition cond](https://reader034.vdocument.in/reader034/viewer/2022042412/5f2b9af4d8584522e531dea9/html5/thumbnails/51.jpg)
Example • “Find theaters showing movies by Bertolucci”:
SQL: • SELECT s.theater FROM schedule s, movie m
WHERE s.title = m.title AND m.director = ‘Berto’ tuple calculus:
• { t: theater | ∃ s ∈ schedule ∃ m ∈ movie [ t(theater) = s(theater) ∧ s(title) = m(title) ∧ m(director) = Berto ] }
relational algebra: πtheater (schedule ⨝ σdir = Berto (movie))
Note: number of items in FROM clause = (number of joins + 1)
52