Chapter 2
Chapter 2
Relational Algebra
An algebra consists of operators and atomic
operands
Relational algebra is an example of an algebra,
its atomic operands are
Variables that stand for relations
Constants, which are finite relations
Relational algebra is a set of operations on
relations
Operations operate on one or more relations to
create new relation
THE RELATIONAL MODEL OF DATA 13
2.3 An Algebraic Query Language
Relational algebra fall into four classes
Set operations – union(giao), intersection(hợp),
difference(hiệu or loại trừ)
Selection and projection (chọn và chiếu)
Cartesian product and joins
Rename
Relation R
Relation S
σlength100(Movies)
title year length genre
Gone With the Wind 1939 231 Drama
Star Wars 1977 124 Scifi
title,year,length(Movies) genre(Movies)
title year length genre
Star Wars 1977 124 Scifi
Galaxy Quest 1999 104 Comedy
Wayne’s World 1992 95
Cartesian product R3 := R1 Χ R2
title,year
length>=100 studioName=‘Fox’
Movies Movies
Example
Section relation
29
Union Operation
30
Union Operation
section relation
result
31
Set-Intersection Operation
32
Set Difference Operation
33
Equivalent Queries
There is more than one way to write a query in relational
algebra.
Example: Find information about courses taught by instructors
in the Physics department with salary greater than 90,000
Query 1
dept_name=“Physics” salary > 90,000 (instructor)
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.
34
Equivalent Queries
35
Exercises
Example: Customer
Ex1: