SlideShare a Scribd company logo
1 of 13
Download to read offline
IT2002 (Semester 1, 2004/5) 67
Relational Algebra
(Reference: Chapter 4 of Ramakrishnan & Gehrke)
IT2002 (Semester 1, 2004/5): Relational Algebra 68
Example Database
Movies
title director myear rating
Fargo Coen 1996 8.2
Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
Actors
actor ayear
Cage 1964
Hanks 1956
Maguire 1975
McDormand 1957
Acts
actor title
Cage Raising Arizona
Maguire Spiderman
Maguire Wonder Boys
McDormand Fargo
McDormand Raising Arizona
McDormand Wonder Boys
Directors
director dyear
Coen 1954
Hanson 1945
Raimi 1959
IT2002 (Semester 1, 2004/5): Relational Algebra 69
Some Queries
• Find movies made after 1997
• Find movies made by Hanson after 1997
• Find all movies and their ratings
• Find all actors and directors
• Find Coen’s movies with McDormand
• Find movies with Maguire but not McDormand
• Find actors who have acted in some Coen’s movie
• Find (director, actor) pairs where the director is younger than
the actor
• Find actors who have acted in all of Coen’s movies
IT2002 (Semester 1, 2004/5): Relational Algebra 70
Relational Algebra
• A formal query language for asking questions
• A query is composed of a collection of operators called
relational operators
• Unary operators: selection, projection, renaming
• Binary operators: union, intersect, difference, cartesian product,
join
• Relations are closed under relational operators
• Operators can be composed to form relational algebra
expressions
IT2002 (Semester 1, 2004/5): Relational Algebra 71
Selection: σ
• σc(R) selects rows from relation R that satisfy selection condition c
• Example: Find movies made after 1997
title director myear rating
Fargo Coen 1996 8.2
Movies Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
title director myear rating
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
σmyear>1997(Movies)
IT2002 (Semester 1, 2004/5): Relational Algebra 72
Selection Condition
• Selection condition is a boolean combination of terms
• A term is one of the following forms:
1. attribute op constant op ∈ {=, =, <, ≤, >, ≥}
2. attribute1 op attribute2
3. term1 ∧ term2
4. term1 ∨ term2
5. ¬ term1
6. (term1)
• Operator precedence: (), op, ¬, ∧, ∨
• Examples:
IT2002 (Semester 1, 2004/5): Relational Algebra 73
Selection Condition (cont.)
• Example: Find movies made by Hanson after 1997
title director myear rating
Fargo Coen 1996 8.2
Movies Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
title director myear rating
Wonder Boys Hanson 2000 7.6
σmyear>1997 ∧ director=‘Hanson (Movies)
IT2002 (Semester 1, 2004/5): Relational Algebra 74
Projection: π
• πL(R) projects columns given by list L from relation R
• Example: Find all movies and their ratings
title director myear rating
Fargo Coen 1996 8.2
Movies Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
title rating
Fargo 8.2
Raising Arizona 7.6
Spiderman 7.4
Wonder Boys 7.6
πtitle, rating(Movies)
IT2002 (Semester 1, 2004/5): Relational Algebra 75
Renaming: ρ
• Given relation R(A, B, C), ρS(X,Y,Z)(R) renames it to S(X, Y, Z)
actor ayear
Cage 1964
Actors Hanks 1956
Maguire 1975
McDormand 1957
name yob
Cage 1964
Stars Hanks 1956
Maguire 1975
McDormand 1957
ρStars(name,yob)(Actors)
IT2002 (Semester 1, 2004/5): Relational Algebra 76
Set Operations
• Union: R ∪ S returns a relation containing all tuples that
occur in R or S (or both)
• Intersection: R ∩ S returns a relation containing all tuples
that occur in both R and S
• Set-difference: R − S returns a relation containing all tuples in
R but not in S
• Two relations are union compatible if
– they have the same arity, and
– the corresponding attributes have same domains
• union (∪), intersection (∩), and set-difference (−) operators
require input relations to be union compatible
IT2002 (Semester 1, 2004/5): Relational Algebra 77
actor ayear
Cage 1964
Actors Hanks 1956
Maguire 1975
McDormand 1957
director dyear
Coen 1954
Directors Hanson 1945
Raimi 1959
actor
Cage
Hanks
Maguire
McDormand
director
Coen
Raimi
Hanson
πactor(Actors) ∪ πdirector(Directors)
actor
Cage
Hanks
Maguire
McDormand
Coen
Raimi
Hanson
Union Example:
Find all actors & directors
πactor(Actors) ∪ πdirector(Directors)
πactor(Actors) πdirector(Directors)
IT2002 (Semester 1, 2004/5): Relational Algebra 78
actor title
Cage Raising Arizona
Maguire Spiderman
Acts Maguire Wonder Boys
McDormand Fargo
McDormand Raising Arizona
McDormand Wonder Boys
title director myear rating
Fargo Coen 1996 8.2
Movies Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
title
Fargo
Raising Arizona
Wonder Boys
title
Fargo
Raising Arizona
e1 ∩ e2
title
Fargo
Raising Arizona
Intersection Example:
Find Coen’s movies with McDormand
e1 = πtitle(σactor=‘McDormand (Acts))
e2 = πtitle(σdirector=‘Coen (Movies))
result = e1 ∩ e2
e1
e2
IT2002 (Semester 1, 2004/5): Relational Algebra 79
actor title
Cage Raising Arizona
Maguire Spiderman
Acts Maguire Wonder Boys
McDormand Fargo
McDormand Raising Arizona
McDormand Wonder Boys
title
Spiderman
Wonder Boys
title
Fargo
Raising Arizona
Wonder Boys
σactor=‘Maguire (Acts) -
σ
actor=‘McDormand
(Acts)
title
Spiderman
Set-difference Example:
Find movies with Maguire but not McDormand
σactor=‘Maguire (Acts) - σactor=‘McDormand (Acts)
σactor=‘Maguire (Acts)
σactor=‘McDormand (Acts)
IT2002 (Semester 1, 2004/5): Relational Algebra 80
Set Operations (cont.)
• Consider R(A, B, C) and S(X, Y )
• Cross-product: R × S returns a relation with attribute list
(A, B, C, X, Y ) defined as follows:
R × S = {(a, b, c, x, y) | (a, b, c) ∈ R, (x, y) ∈ S}
• Cross-product operation is also known as cartesian product
IT2002 (Semester 1, 2004/5): Relational Algebra 81
Cross-product Example
• Find actors who have acted in some Coen’s movies
• e1 = ρT (title2)(πtitle(σdirector=‘Coen (Movies)))
Movies
title director myear rating
Fargo Coen 1996 8.2
Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
T
title2
Fargo
Raising Arizona
e1
IT2002 (Semester 1, 2004/5): Relational Algebra 82
Cross-product Example (cont.)
e2 =
Acts
actor title
Cage Raising Arizona
Maguire Spiderman
Maguire Wonder Boys
McDormand Fargo
McDormand Raising Arizona
McDormand Wonder Boys
×
T
title2
Fargo
Raising Arizona
=
actor title title2
Cage Raising Arizona Fargo
Cage Raising Arizona Raising Arizona
Maguire Spiderman Fargo
Maguire Spiderman Raising Arizona
Maguire Wonder Boys Fargo
Maguire Wonder Boys Raising Arizona
McDormand Fargo Fargo
McDormand Fargo Raising Arizona
McDormand Raising Arizona Fargo
McDormand Raising Arizona Raising Arizona
McDormand Wonder Boys Fargo
McDormand Wonder Boys Raising Arizona
IT2002 (Semester 1, 2004/5): Relational Algebra 83
Cross-product Example (cont.)
actor title title2
Cage Raising Arizona Fargo
Cage Raising Arizona Raising Arizona
Maguire Spiderman Fargo
Maguire Spiderman Raising Arizona
Maguire Wonder Boys Fargo
Maguire Wonder Boys Raising Arizona
e2 McDormand Fargo Fargo
McDormand Fargo Raising Arizona
McDormand Raising Arizona Fargo
McDormand Raising Arizona Raising Arizona
McDormand Wonder Boys Fargo
McDormand Wonder Boys Raising Arizona
actor title title2
Cage Raising Arizona Raising Arizona
e3 McDormand Fargo Fargo
McDormand Raising Arizona Raising Arizona
actor
Cage
McDormand
e3 = σtitle=title2(e2)
πactor(e3)
IT2002 (Semester 1, 2004/5): Relational Algebra 84
Cross-product Example (cont.)
• Query: Find actors who have acted in some Coen’s movie
• Answer: πactor( σtitle=title2 ( Acts × ρT (title2)( πtitle(
σdirector=‘Coen (Movies) ) ) ) )
πactor
σtitle=title2
×
Acts ρT (title2)
πtitle
σdirector=‘Coen
Movies
IT2002 (Semester 1, 2004/5): Relational Algebra 85
Join
• Combines cross-product, selection, and projection
• Join operator is more useful than the plain cross-product
operator
• Three types of join:
– Condition join
– Equijoin
– Natural join
IT2002 (Semester 1, 2004/5): Relational Algebra 86
Condition Join: R c S
• Condition join = Cross-product followed by selection
R c S = σc(R × S)
• Example: Find (director,actor) pairs where the director is
younger than the actor
• Answer: πdirector,actor( Directors dyear>ayear Actors)
Directors
director dyear
Coen 1954
Hanson 1945
Raimi 1959
Actors
actor ayear
Cage 1964
Hanks 1956
Maguire 1975
McDormand 1957
e1 = Directors dyear>ayear Actors
director dyear actor ayear
Raimi 1959 Hanks 1956
Raimi 1959 McDormand 1957
director actor
Raimi Hanks
Raimi McDormand
πdirector,actor(e1)
IT2002 (Semester 1, 2004/5): Relational Algebra 87
Equijoin: R c S
• Equijoin = Condition join of the form
R c S = πL(σc(R × S))
where
– c is a conjunction of equality conditions of the form
R.Ai = S.Aj
– L is a sequence of attributes consisting of L1 followed by L2
– L1 is a sequence of attributes in schema of R
– L2 is a sequence of attributes in schema of S that are not
referenced in c
IT2002 (Semester 1, 2004/5): Relational Algebra 88
Equijoin (cont.)
• Example: Find actors who have acted in some Coen’s movie
• πactor( σdirector=‘Coen ( Acts Acts.title = Movies.title Movies ) )
e1 = Acts Acts.title = Movies.title Movies
actor title director myear rating
Cage Raising Arizona Coen 1987 7.6
Maguire Spiderman Raimi 2002 7.4
Maguire Wonder Boys Hanson 2000 7.6
McDormand Fargo Coen 1996 8.2
McDormand Raising Arizona Coen 1987 7.6
McDormand Wonder Boys Hanson 2000 7.6
actor
Cage
McDormand
πactor(σdirector=‘Coen ((e1))
IT2002 (Semester 1, 2004/5): Relational Algebra 89
Natural Join: R S
• Natural join = Equijoin of the form
R S = R c S
where c is specified for all attributes having the same name in R
and S
• Example: Find actors who have acted in some Coen’s movie
πactor( σdirector=‘Coen ( Acts Movies ) )
• Example: Find the name and the year of birth of all actors who
were in some Coen’s movie
πactor,ayear( σdirector=‘Coen (Movies) Acts Actors )
IT2002 (Semester 1, 2004/5): Relational Algebra 90
Example: Condition, Equi-, Natural Joins
R
A B X
0 6 x1
1 9 x2
2 7 x3
S
A B Y
0 8 y1
1 5 y2
2 7 y3
• R A=A ∧ B<B ρS (A ,B ,Y )(S) A B X A’ B’ Y
0 6 x1 0 8 y1
• R A=A ρS (A ,B ,Y )(S)
A B X B’ Y
0 6 x1 8 y1
1 9 x2 5 y2
2 7 x3 7 y3
• R S A B X Y
2 7 x3 y3
IT2002 (Semester 1, 2004/5): Relational Algebra 91
Quiz
• Query: Find actors who have acted in all Coen’s movies
• CMovies = πtitle(σdirector=‘Coen (Movies))
Movies
title director myear rating
Fargo Coen 1996 8.2
Raising Arizona Coen 1987 7.6
Spiderman Raimi 2002 7.4
Wonder Boys Hanson 2000 7.6
CMovies
title
Fargo
Raising Arizona
actor title
Cage Raising Arizona
Maguire Spiderman
Acts Maguire Wonder Boys
McDormand Fargo
McDormand Raising Arizona
McDormand Wonder Boys
IT2002 (Semester 1, 2004/5): Relational Algebra 92
Summary
• Relational algebra: simple and powerful query language
• Basic operators: σ, π, ∪, −, ×
• Additional operators: ρ, ∩, , ÷
• Relational algebra is closed: operator’s output is a relation
• Relational operators can be composed to form complex
relational algebra expressions

More Related Content

What's hot

Multivalued dependency
Multivalued dependencyMultivalued dependency
Multivalued dependency
avniS
 
Data flow diagrams (2)
Data flow diagrams (2)Data flow diagrams (2)
Data flow diagrams (2)
Ujjwal 'Shanu'
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
emailharmeet
 

What's hot (20)

Organization and team structures
Organization and team structuresOrganization and team structures
Organization and team structures
 
Multivalued dependency
Multivalued dependencyMultivalued dependency
Multivalued dependency
 
Two phase commit protocol in dbms
Two phase commit protocol in dbmsTwo phase commit protocol in dbms
Two phase commit protocol in dbms
 
relational algebra
relational algebrarelational algebra
relational algebra
 
Data Manipulation Language
Data Manipulation LanguageData Manipulation Language
Data Manipulation Language
 
Data flow diagrams (2)
Data flow diagrams (2)Data flow diagrams (2)
Data flow diagrams (2)
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
 
Remote backup system
Remote backup systemRemote backup system
Remote backup system
 
Past Papers (Compiler Construction).pdf
Past Papers (Compiler Construction).pdfPast Papers (Compiler Construction).pdf
Past Papers (Compiler Construction).pdf
 
Normalization
NormalizationNormalization
Normalization
 
Distributed DBMS - Unit 3 - Distributed DBMS Architecture
Distributed DBMS - Unit 3 - Distributed DBMS ArchitectureDistributed DBMS - Unit 3 - Distributed DBMS Architecture
Distributed DBMS - Unit 3 - Distributed DBMS Architecture
 
Software requirement and specification
Software requirement and specificationSoftware requirement and specification
Software requirement and specification
 
Join dependency
Join dependencyJoin dependency
Join dependency
 
DBMS UNIT IV.pptx
DBMS UNIT IV.pptxDBMS UNIT IV.pptx
DBMS UNIT IV.pptx
 
DBMS - FIRST NORMAL FORM
DBMS - FIRST NORMAL FORMDBMS - FIRST NORMAL FORM
DBMS - FIRST NORMAL FORM
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Design notation
Design notationDesign notation
Design notation
 
Court Case Management System
Court Case Management SystemCourt Case Management System
Court Case Management System
 
Requirements analysis
Requirements analysisRequirements analysis
Requirements analysis
 
embedded-static-&dynamic
embedded-static-&dynamicembedded-static-&dynamic
embedded-static-&dynamic
 

Recently uploaded

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
MayuraD1
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
💚Trustworthy Call Girls Pune Call Girls Service Just Call 🍑👄6378878445 🍑👄 Top...
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 

03 ra-examples3(1)

  • 1. IT2002 (Semester 1, 2004/5) 67 Relational Algebra (Reference: Chapter 4 of Ramakrishnan & Gehrke) IT2002 (Semester 1, 2004/5): Relational Algebra 68 Example Database Movies title director myear rating Fargo Coen 1996 8.2 Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 Actors actor ayear Cage 1964 Hanks 1956 Maguire 1975 McDormand 1957 Acts actor title Cage Raising Arizona Maguire Spiderman Maguire Wonder Boys McDormand Fargo McDormand Raising Arizona McDormand Wonder Boys Directors director dyear Coen 1954 Hanson 1945 Raimi 1959
  • 2. IT2002 (Semester 1, 2004/5): Relational Algebra 69 Some Queries • Find movies made after 1997 • Find movies made by Hanson after 1997 • Find all movies and their ratings • Find all actors and directors • Find Coen’s movies with McDormand • Find movies with Maguire but not McDormand • Find actors who have acted in some Coen’s movie • Find (director, actor) pairs where the director is younger than the actor • Find actors who have acted in all of Coen’s movies IT2002 (Semester 1, 2004/5): Relational Algebra 70 Relational Algebra • A formal query language for asking questions • A query is composed of a collection of operators called relational operators • Unary operators: selection, projection, renaming • Binary operators: union, intersect, difference, cartesian product, join • Relations are closed under relational operators • Operators can be composed to form relational algebra expressions
  • 3. IT2002 (Semester 1, 2004/5): Relational Algebra 71 Selection: σ • σc(R) selects rows from relation R that satisfy selection condition c • Example: Find movies made after 1997 title director myear rating Fargo Coen 1996 8.2 Movies Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 title director myear rating Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 σmyear>1997(Movies) IT2002 (Semester 1, 2004/5): Relational Algebra 72 Selection Condition • Selection condition is a boolean combination of terms • A term is one of the following forms: 1. attribute op constant op ∈ {=, =, <, ≤, >, ≥} 2. attribute1 op attribute2 3. term1 ∧ term2 4. term1 ∨ term2 5. ¬ term1 6. (term1) • Operator precedence: (), op, ¬, ∧, ∨ • Examples:
  • 4. IT2002 (Semester 1, 2004/5): Relational Algebra 73 Selection Condition (cont.) • Example: Find movies made by Hanson after 1997 title director myear rating Fargo Coen 1996 8.2 Movies Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 title director myear rating Wonder Boys Hanson 2000 7.6 σmyear>1997 ∧ director=‘Hanson (Movies) IT2002 (Semester 1, 2004/5): Relational Algebra 74 Projection: π • πL(R) projects columns given by list L from relation R • Example: Find all movies and their ratings title director myear rating Fargo Coen 1996 8.2 Movies Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 title rating Fargo 8.2 Raising Arizona 7.6 Spiderman 7.4 Wonder Boys 7.6 πtitle, rating(Movies)
  • 5. IT2002 (Semester 1, 2004/5): Relational Algebra 75 Renaming: ρ • Given relation R(A, B, C), ρS(X,Y,Z)(R) renames it to S(X, Y, Z) actor ayear Cage 1964 Actors Hanks 1956 Maguire 1975 McDormand 1957 name yob Cage 1964 Stars Hanks 1956 Maguire 1975 McDormand 1957 ρStars(name,yob)(Actors) IT2002 (Semester 1, 2004/5): Relational Algebra 76 Set Operations • Union: R ∪ S returns a relation containing all tuples that occur in R or S (or both) • Intersection: R ∩ S returns a relation containing all tuples that occur in both R and S • Set-difference: R − S returns a relation containing all tuples in R but not in S • Two relations are union compatible if – they have the same arity, and – the corresponding attributes have same domains • union (∪), intersection (∩), and set-difference (−) operators require input relations to be union compatible
  • 6. IT2002 (Semester 1, 2004/5): Relational Algebra 77 actor ayear Cage 1964 Actors Hanks 1956 Maguire 1975 McDormand 1957 director dyear Coen 1954 Directors Hanson 1945 Raimi 1959 actor Cage Hanks Maguire McDormand director Coen Raimi Hanson πactor(Actors) ∪ πdirector(Directors) actor Cage Hanks Maguire McDormand Coen Raimi Hanson Union Example: Find all actors & directors πactor(Actors) ∪ πdirector(Directors) πactor(Actors) πdirector(Directors) IT2002 (Semester 1, 2004/5): Relational Algebra 78 actor title Cage Raising Arizona Maguire Spiderman Acts Maguire Wonder Boys McDormand Fargo McDormand Raising Arizona McDormand Wonder Boys title director myear rating Fargo Coen 1996 8.2 Movies Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 title Fargo Raising Arizona Wonder Boys title Fargo Raising Arizona e1 ∩ e2 title Fargo Raising Arizona Intersection Example: Find Coen’s movies with McDormand e1 = πtitle(σactor=‘McDormand (Acts)) e2 = πtitle(σdirector=‘Coen (Movies)) result = e1 ∩ e2 e1 e2
  • 7. IT2002 (Semester 1, 2004/5): Relational Algebra 79 actor title Cage Raising Arizona Maguire Spiderman Acts Maguire Wonder Boys McDormand Fargo McDormand Raising Arizona McDormand Wonder Boys title Spiderman Wonder Boys title Fargo Raising Arizona Wonder Boys σactor=‘Maguire (Acts) - σ actor=‘McDormand (Acts) title Spiderman Set-difference Example: Find movies with Maguire but not McDormand σactor=‘Maguire (Acts) - σactor=‘McDormand (Acts) σactor=‘Maguire (Acts) σactor=‘McDormand (Acts) IT2002 (Semester 1, 2004/5): Relational Algebra 80 Set Operations (cont.) • Consider R(A, B, C) and S(X, Y ) • Cross-product: R × S returns a relation with attribute list (A, B, C, X, Y ) defined as follows: R × S = {(a, b, c, x, y) | (a, b, c) ∈ R, (x, y) ∈ S} • Cross-product operation is also known as cartesian product
  • 8. IT2002 (Semester 1, 2004/5): Relational Algebra 81 Cross-product Example • Find actors who have acted in some Coen’s movies • e1 = ρT (title2)(πtitle(σdirector=‘Coen (Movies))) Movies title director myear rating Fargo Coen 1996 8.2 Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 T title2 Fargo Raising Arizona e1 IT2002 (Semester 1, 2004/5): Relational Algebra 82 Cross-product Example (cont.) e2 = Acts actor title Cage Raising Arizona Maguire Spiderman Maguire Wonder Boys McDormand Fargo McDormand Raising Arizona McDormand Wonder Boys × T title2 Fargo Raising Arizona = actor title title2 Cage Raising Arizona Fargo Cage Raising Arizona Raising Arizona Maguire Spiderman Fargo Maguire Spiderman Raising Arizona Maguire Wonder Boys Fargo Maguire Wonder Boys Raising Arizona McDormand Fargo Fargo McDormand Fargo Raising Arizona McDormand Raising Arizona Fargo McDormand Raising Arizona Raising Arizona McDormand Wonder Boys Fargo McDormand Wonder Boys Raising Arizona
  • 9. IT2002 (Semester 1, 2004/5): Relational Algebra 83 Cross-product Example (cont.) actor title title2 Cage Raising Arizona Fargo Cage Raising Arizona Raising Arizona Maguire Spiderman Fargo Maguire Spiderman Raising Arizona Maguire Wonder Boys Fargo Maguire Wonder Boys Raising Arizona e2 McDormand Fargo Fargo McDormand Fargo Raising Arizona McDormand Raising Arizona Fargo McDormand Raising Arizona Raising Arizona McDormand Wonder Boys Fargo McDormand Wonder Boys Raising Arizona actor title title2 Cage Raising Arizona Raising Arizona e3 McDormand Fargo Fargo McDormand Raising Arizona Raising Arizona actor Cage McDormand e3 = σtitle=title2(e2) πactor(e3) IT2002 (Semester 1, 2004/5): Relational Algebra 84 Cross-product Example (cont.) • Query: Find actors who have acted in some Coen’s movie • Answer: πactor( σtitle=title2 ( Acts × ρT (title2)( πtitle( σdirector=‘Coen (Movies) ) ) ) ) πactor σtitle=title2 × Acts ρT (title2) πtitle σdirector=‘Coen Movies
  • 10. IT2002 (Semester 1, 2004/5): Relational Algebra 85 Join • Combines cross-product, selection, and projection • Join operator is more useful than the plain cross-product operator • Three types of join: – Condition join – Equijoin – Natural join IT2002 (Semester 1, 2004/5): Relational Algebra 86 Condition Join: R c S • Condition join = Cross-product followed by selection R c S = σc(R × S) • Example: Find (director,actor) pairs where the director is younger than the actor • Answer: πdirector,actor( Directors dyear>ayear Actors) Directors director dyear Coen 1954 Hanson 1945 Raimi 1959 Actors actor ayear Cage 1964 Hanks 1956 Maguire 1975 McDormand 1957 e1 = Directors dyear>ayear Actors director dyear actor ayear Raimi 1959 Hanks 1956 Raimi 1959 McDormand 1957 director actor Raimi Hanks Raimi McDormand πdirector,actor(e1)
  • 11. IT2002 (Semester 1, 2004/5): Relational Algebra 87 Equijoin: R c S • Equijoin = Condition join of the form R c S = πL(σc(R × S)) where – c is a conjunction of equality conditions of the form R.Ai = S.Aj – L is a sequence of attributes consisting of L1 followed by L2 – L1 is a sequence of attributes in schema of R – L2 is a sequence of attributes in schema of S that are not referenced in c IT2002 (Semester 1, 2004/5): Relational Algebra 88 Equijoin (cont.) • Example: Find actors who have acted in some Coen’s movie • πactor( σdirector=‘Coen ( Acts Acts.title = Movies.title Movies ) ) e1 = Acts Acts.title = Movies.title Movies actor title director myear rating Cage Raising Arizona Coen 1987 7.6 Maguire Spiderman Raimi 2002 7.4 Maguire Wonder Boys Hanson 2000 7.6 McDormand Fargo Coen 1996 8.2 McDormand Raising Arizona Coen 1987 7.6 McDormand Wonder Boys Hanson 2000 7.6 actor Cage McDormand πactor(σdirector=‘Coen ((e1))
  • 12. IT2002 (Semester 1, 2004/5): Relational Algebra 89 Natural Join: R S • Natural join = Equijoin of the form R S = R c S where c is specified for all attributes having the same name in R and S • Example: Find actors who have acted in some Coen’s movie πactor( σdirector=‘Coen ( Acts Movies ) ) • Example: Find the name and the year of birth of all actors who were in some Coen’s movie πactor,ayear( σdirector=‘Coen (Movies) Acts Actors ) IT2002 (Semester 1, 2004/5): Relational Algebra 90 Example: Condition, Equi-, Natural Joins R A B X 0 6 x1 1 9 x2 2 7 x3 S A B Y 0 8 y1 1 5 y2 2 7 y3 • R A=A ∧ B<B ρS (A ,B ,Y )(S) A B X A’ B’ Y 0 6 x1 0 8 y1 • R A=A ρS (A ,B ,Y )(S) A B X B’ Y 0 6 x1 8 y1 1 9 x2 5 y2 2 7 x3 7 y3 • R S A B X Y 2 7 x3 y3
  • 13. IT2002 (Semester 1, 2004/5): Relational Algebra 91 Quiz • Query: Find actors who have acted in all Coen’s movies • CMovies = πtitle(σdirector=‘Coen (Movies)) Movies title director myear rating Fargo Coen 1996 8.2 Raising Arizona Coen 1987 7.6 Spiderman Raimi 2002 7.4 Wonder Boys Hanson 2000 7.6 CMovies title Fargo Raising Arizona actor title Cage Raising Arizona Maguire Spiderman Acts Maguire Wonder Boys McDormand Fargo McDormand Raising Arizona McDormand Wonder Boys IT2002 (Semester 1, 2004/5): Relational Algebra 92 Summary • Relational algebra: simple and powerful query language • Basic operators: σ, π, ∪, −, × • Additional operators: ρ, ∩, , ÷ • Relational algebra is closed: operator’s output is a relation • Relational operators can be composed to form complex relational algebra expressions