Relational Algebra
Relational Algebra
Database System Concepts - 7th Edition 2.1 ©Silberschatz, Korth and Sudarshan
Select Operation
dept_name=“Physics” (instructor)
Result
Database System Concepts - 7th Edition 2.2 ©Silberschatz, Korth and Sudarshan
Select Operation (Cont.)
Database System Concepts - 7th Edition 2.3 ©Silberschatz, Korth and Sudarshan
Project Operation
A unary operation that returns its argument relation, with certain attributes
left out.
Notation:
Database System Concepts - 7th Edition 2.4 ©Silberschatz, Korth and Sudarshan
Project Operation Example
Example: eliminate the dept_name attribute of instructor
Query:
ID, name, salary (instructor)
Result:
Database System Concepts - 7th Edition 2.5 ©Silberschatz, Korth and Sudarshan
Composition of Relational Operations
Database System Concepts - 7th Edition 2.6 ©Silberschatz, Korth and Sudarshan
Cartesian-Product Operation
The Cartesian-product operation (denoted by X) allows us to combine
information from any two relations.
Example: the Cartesian product of the relations instructor and teaches is
written as:
instructor X teaches
We construct a tuple of the result out of each possible pair of tuples: one
from the instructor relation and one from the teaches relation (see next
slide)
Since the instructor ID appears in both relations we distinguish between
these attribute by attaching to the attribute the name of the relation from
which the attribute originally came.
instructor.ID
teaches.ID
Database System Concepts - 7th Edition 2.7 ©Silberschatz, Korth and Sudarshan
The instructor X teaches table
Database System Concepts - 7th Edition 2.8 ©Silberschatz, Korth and Sudarshan
Join Operation
The Cartesian-Product
instructor X teaches
associates every tuple of instructor with every tuple of teaches.
Most of the resulting rows have information about instructors who did
NOT teach a particular course.
To get only those tuples of “instructor X teaches “ that pertain to
instructors and the courses that they taught, we write:
instructor.id = teaches.id (instructor x teaches ))
Database System Concepts - 7th Edition 2.9 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
The table corresponding to:
instructor.id = teaches.id (instructor x teaches))
Database System Concepts - 7th Edition 2.10 ©Silberschatz, Korth and Sudarshan
Join Operation (Cont.)
Thus
instructor.id = teaches.id (instructor x teaches ))
Database System Concepts - 7th Edition 2.11 ©Silberschatz, Korth and Sudarshan
Union Operation
Database System Concepts - 7th Edition 2.12 ©Silberschatz, Korth and Sudarshan
Union Operation (Cont.)
Result of:
course_id ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.13 ©Silberschatz, Korth and Sudarshan
Set-Intersection Operation
The set-intersection operation allows us to find tuples that are in both
the input relations.
Notation: r s
Assume:
r, s have the same arity
attributes of r and s are compatible
Example: Find the set of all courses taught in both the Fall 2017 and the
Spring 2018 semesters.
course_id ( semester=“Fall” Λ year=2017 (section))
course_id ( semester=“Spring” Λ year=2018 (section))
Result
Database System Concepts - 7th Edition 2.14 ©Silberschatz, Korth and Sudarshan
Set Difference Operation
The set-difference operation allows us to find tuples that are in one relation
but are not in another.
Notation r – s
Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Example: to find all courses taught in the Fall 2017 semester, but not in the
Spring 2018 semester
course_id ( semester=“Fall” Λ year=2017 (section)) −
course_id ( semester=“Spring” Λ year=2018 (section))
Database System Concepts - 7th Edition 2.15 ©Silberschatz, Korth and Sudarshan
The Assignment Operation
It is convenient at times to write a relational-algebra expression by
assigning parts of it to temporary relation variables.
The assignment operation is denoted by and works like assignment in
a programming language.
Example: Find all instructor in the “Physics” and Music department.
Database System Concepts - 7th Edition 2.16 ©Silberschatz, Korth and Sudarshan
The Rename Operation
The results of relational-algebra expressions do not have a name that we
can use to refer to them. The rename operator, , is provided for that
purpose
The expression:
x (E)
returns the result of expression E under the name x
Another form of the rename operation:
x(A1,A2, .. An) (E)
Database System Concepts - 7th Edition 2.17 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
Query 2
dept_name=“Physics” ( salary > 90.000 (instructor))
The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
Database System Concepts - 7th Edition 2.18 ©Silberschatz, Korth and Sudarshan
Equivalent Queries
Query 2
(dept_name=“Physics” (instructor)) instructor.ID = teaches.ID teaches
The two queries are not identical; they are, however, equivalent -- they
give the same result on any database.
Database System Concepts - 7th Edition 2.19 ©Silberschatz, Korth and Sudarshan