You are on page 1of 26

Database systems

5th Lecture
Relational Algebra
• Relational algebra is a procedural query
language.
• It consist a set of operations that take one two
relations as input and produce a new relation
as their result.
• Relational Algebra is a basis for database
query language.
Operations in Relational Algebra
Operations in Relational Algebra
Relational Algebra in a DBMS
Projection 
• A projection operator is used to produce from
a relation R a new relation that has only some
of R’s columns.

The value of expression  A1, A2, …,An(R) is a


relation that has only the columns for attributes
A1, A2, A3, …., An of R.
• C (R)
Projection 
R2 = Projection L (R1)
L is a list of attributes from the schema of R1.
R2 is constructed by looking at each tuple of R1,
extracting the attributes on list L, in the order
specified, and creating from those components a
tuple for R2.
Eliminate duplicate tuples, if any.
Projection

Title Year Length Genre Studio Name Producer C#

Star Wars 1977 124 SciFi Fox 1234

Galaxy Quest 1999 104 Comedy DreamWorks 1240

Wayne’s World 1992 95 Comedy Paramount 999


Projection Result
 title, year, length (Movies)

Title Year Length

Star Wars 1977 124

Galaxy Quest 1999 104

Wayne’s World 1992 95

 Genre (Movies) ????


Selection 
• The selection operator, applied to a relation R,
produces a new relation with a subset of R’s
tuples.
• The tuples in the resulting relation are those
that satisfy some condition C that involves the
attributes of R.
•  C(predicate) (R)
Selection 

R2 = SELECT c (R1)
• C is a condition (as “if” statements) that refers
to attributes of R1.
• R2 is all those tuples of R1 that satisfy C.
Selection
•  length  100 (Movies)

Title Year Length Genre Studio Producer


Name C#
Star Wars 1977 124 SciFi Fox 1234

Galaxy Quest 1999 104 Comedy DreamWork 1240


s
Wayne’s World 1992 95 Comedy Paramount 999
Selection Result

Title Year Length Genre Studio Producer C#


Name

Star Wars 1977 124 SciFi Fox 1234

Galaxy 1999 104 Comedy DreamWork 1240


Quest s
Selection Result
•  length  100 AND studioName = ‘Fox’ (Movies)

Title Year Length Genre Studio Producer C#


Name

Star Wars 1977 124 SciFi Fox 1234


Cartesian Product
• The Cartesian product (or cross product , or
just product) of two sets R and S is the set of
pairs that can be formed by choosing the first
element of the pair to be any element of R
and the second any element of S. This product
is denoted R * S.
Cartesian Product
• R* S B C D
A B 5 6 7
1 2 8 9 10
3 4 11 12 13

A R.B S.B C D
1 2 5 6 7
1 2 5 6 7
1 2 8 9 10
3 4 8 9 10
3 4 11 12 13
3 4 11 12 13
Natural Joins
• Some time we need to join only those tuples
that match in some way, of two relations.
Denoted R ∞ S.

• A tuple that fails to pair with any


tuple of the other relation in a join is
said to be dangling tuple.
Natural Join
A B B C D
1 2 2 5 6

3 4 4 7 8
9 10 11

A R.B S.B C D
1 2 2 5 6 A B C D
1 2 2 5 6 1 2 5 6
1 2 4 7 8 3 4 7 8
3 4 4 7 8
3 4 9 10 11
3 4 9 10 11
Theta - Join
• The natural join forces us to pair tuples using one specific
condition.

• Theta join refers to an arbitrary condition, which we shall


represent by C rather than O.
The result of this operation is constructed as follows:
1. Take the product of R and S.
2. Select from the product only those tuples that satisfy the
condition C.
R∞cS
A B C B C D
1 2 3 2 3 4
6 7 8 4 5 6
9 7 8 5 7 10

A U.B U.C V.B V.C D


1 2 3 2 3 4
1 2 3 2 3 4
1 2 3 2 3 4
6 7 8 4 5 6
6 7 8 4 5 6 U∞ V
6 7 8 4 5 6
9 7 8 5 7 10
9 7 8 5 7 10
9 7 8 5 7 10
U∞ A<D V
A U.B U.C V.B V.C D
1 2 3 2 3 4
1 2 3 2 3 4
1 2 3 2 3 4
9 7 8 5 7 10
9 7 8 5 7 10
9 7 8 5 7 10

U∞ V A<D AND U.B ≠ V.B V

A U.B U.C V.B V.C D

9 7 8 5 7 10
Combining operations to form Queries
• If we write single operations on one or two
relations as queries, then relational algebra
would not be nearly as useful as it is.

• Relational algebra allows us to form


expressions of arbitrary complexity by
applying operations to the result of other
operations.
Movies Table
Title Year Length Genre Studio Name Producer C#

Star Wars 1977 124 SciFi Fox 1234

Galaxy Quest 1999 104 Comedy DreamWorks 1240

Wayne’s 1992 95 Comedy Paramount 999


World
Example
Suppose we want to know that, from our running
movies relation, “what are the titles and years of
movies made by fox that are at least 100 minutes
long?”
One way:
1. Select those movies tuples that have length
100.
2. Select those movies tuples that have studio
Name = ‘Fox’.
Conventional, linear Algebra
 title, year (  length 100 (Movies)   studioName = ‘Fox’
(Movies))
Equivalent form of the Query
 title, year ( length 100 AND studioName = ‘Fox’ (Movies))

You might also like