Vous êtes sur la page 1sur 19

Chapter 3: Relational Algebra

INFT 2301: Fundamentals of Databases, School of

But whats been done so far?


Chapter 1: Introduction to Databases
Databases, data storage, overview of DBMS and its components
How data is accessed in a database, overview of SQL

Chapter 2: Relational Model


Relational concepts, keys and constraints
Relational database schema and relational database

1/3

INFT 2301: Fundamentals of Databases, School of

Relational Algebra Operations


Introduction to Relational Algebra
Relational Algebra Operations

2/3

INFT 2301: Fundamentals of Databases, School of

Why is there a need for query languages at all?


In order for a database to be useful, it should be possible to query and store information in

it. This is achieved via data manipulation languages.


Relational model comes with well-defined data manipulation languages which are
Relational Algebra specifies operations and the order in which these operations must be done in order to

retrieve the request results.


Relational Calculus specifies what the query must return without specifying the way of doing it.

3/3

INFT 2301: Fundamentals of Databases, School of

Relational Algebra Operations


Unary operations
SELECT operation: (sigma)
PROJECT operation: (pi)
RENAME operation: (rho)

Binary operations
UNION operation:
INTERSECTION operation:
DIFFERENCE operation:
CARTESIAN PRODUCT: X

JOIN operations:

4/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: SELECT


The SELECT operation selects a subset of tuples from a relation based on a

selection condition

<selection condition>

(R)

The selection condition plays role of a filter.


The selection condition is a Boolean expression on the attributes of relation R.
Only those tuples are retained which satisfy the condition.
Tuples that dont satisfy the condition are discarded.
As a result of the SELECT operation a relation with the same attributes as R is

produced.
The resultant relation has less or equal number of tuples of the original relation.
Select operation is commutative

<condition1>

< condition2>

(R)) =

<condition2>

5/3

< condition1>

(R))

INFT 2301: Fundamentals of Databases, School of

Relational Operations: SELECT (Cont.)


Find all people collecting stamps.

Hobby = stamps(Person)

Hobby=stamps(Person)

Person
Id
1123
1123
5556
9876

Name
Address
John
123 Main
John
123 Main
Mary 7 Lake Dr
Bart
5 Pine St

Hobby Id
stamps 1123
coins
9876
hiking
stamps

6/3

Name
John
Bart

Address
123 Main
5 Pine St

Hobby
stamps
stamps

INFT 2301: Fundamentals of Databases, School of

Relational Operations: PROJECT


The PROJECT keeps certain attributes (columns) from a relation and discards the

other attributes.

<attribute list> (R)


<attribute list> is the desired list of attributes from relation R.
Project operation vertically partitions a relation:
The list of specified attributes is kept in each tuple
The rest of the attributes are discarded

Project operation removes duplicate rows.


Project operation is not commutative

<list1> ( <list2> (R) ) =


attributes in <list1>

<list1>

(R) if and only if <list2> contains the

7/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: PROJECT (Cont.)


Example of Projection
Find names and hobby of persons

Name, Hobby(Person)

Id

Person
(Person)
Name
Address
Hobby

1123
1123
5556
9876

John
John
Mary
Bart

Name,
Hobby

123 Main stamps


123 Main coins
7 Lake Dr hiking
5 Pine St stamps

8/3

Name

John
John
Mary
Bart

Hobby

stamps
coins
hiking
stamps

INFT 2301: Fundamentals of Databases, School of

Relational Operations: RENAME


In some cases we may want to rename the attributes of a relation or the relation name or

both.
This can be achieved by the RENAME operation

S (B1, B2, , Bn )(R)


The above rename operation
Sets R relations name to S
Changes attribute names of relation R to B1, B2,, Bn.

9/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: UNION


Result of UNION operation of two relations R and S is a relation R S that contains all

tuples that are either in R or S or in both R and S.


Duplicate tuples are removed.
Relations R and S must be union compatible:
Number of attributes of R and S must be same
Pairs of corresponding types must have same domains

sid
S1
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0

S1 S2

S2 sid

28
31
44
58

sid

sname rating

age

22
31
58
44
28

dustin
lubber
rusty
guppy
yuppy

45.0
55.5
35.0
35.0
35.0

10/3

7
8
10
5
9

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10
35.0

INFT 2301: Fundamentals of Databases, School of

Relational Operations: INTERSECTION


Result of INTERSECTION operation of two relations R and S is a relation R S that

contains tuples that are both in R and S.


Relations R and S must be type compatible:
Pairs of corresponding attributes must have same domains

S1

sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0

S2 sid

28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0

sid sname rating age


31 lubber 8
55.5
58 rusty
10
35.0

S1 S2
11/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: DIFFERENCE


Result of DIFFERENCE operation of two relations R and S is a relation R S that contains

all tuples that are in R and not in S.


Relations R and S must be type compatible:
Pairs of corresponding attributes must have same domains

S1

sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0

S2 sid

28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0

sid sname rating age


22 dustin 7
45.0

S1 S2
13/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: CARTESIAN PRODUCT


Result of CARTESIAN PRODUCT (or CROSS PRODUCT) operation of two relations R and S

is a relation R X S that has one tuple for each combination of tuples one from R and one
from S.
Relations R and S dont have to be type compatible.

a b
x1 x2
x3 x4

c d
y1 y2
y3 y4

15/3

a
x1
x1
x3
x3

b c
x2 y1
x2 y3
x4 y1
x4 y3
R S

d
y2
y4
y2
y4

INFT 2301: Fundamentals of Databases, School of

Relational Operations: JOIN


Normally, CARTESIAN PRODUCT will provide unnecessary information.
In order to get rid of this information the SELECT operation must be applied to retrieve the

tuples one needs.


Combination of CARTESIAN PRODUCT operation followed by the SELECT operation is
called JOIN, normally denoted as follows:
R
<join condition>S = P
where R and S can be any relations.
The resultant relation of join of two relations is a relation P with degree of n + m, where n
and m are degrees of relations R and S, correspondingly.
This operation is important for any relational database with more than one relation, because
it allows to combine related tuples from various relations.

17/3

INFT 2301: Fundamentals of Databases, School of

Relational Operations: EQUIJOIN and NATURAL JOIN


Mostly used form of join is a join with join conditions involving only equality conditions;

attribute names of relations can be different.


Such a join is called an EQUIJOIN.
EUQIJOIN results in one or more attributes that have the same values in every tuple.
Because there can be two attributes with identical values then one can be removed.
And if such a join is done via same attribute names from both relations then the result is a
NATURAL JOIN.

S
B
A B
1 2 R(B)=S(B) 2
4
3 4
9

EQUIJOIN
C
5
7
10

D
6
8
11

A B B C D
1 2 2 5 6
3 4 4 7 8

18/3

NATURLAJOIN
A B C D
1 2 5 6
3 4 7 8

INFT 2301: Fundamentals of Databases, School of

Relational Operations: OUTER JOIN


When we want to keep all tuples in either of the relations R and S or in both of them

we use OUTER JOIN.


Variations of OUTER JOIN are:

LEFT OUTER JOIN keeps all the tuples in the first relation R and if no matching tuples are found

in S, then the attributes of S in the join result are filled with nulls.
RIGHT OUTER JOIN is the same as left outer join but all tuples from S are kept and Rs values
are filled with nulls.
FULL OUTER JOIN keeps values from both relations and when no values are found pads them
with nulls.

19/3

INFT 2301: Fundamentals of Databases, School of

Relational Algebra and SQL


Relational Algebra forms a foundation for SQL

SELECT C.CrsName
FROM Course C, Teaching T
WHERE C.CrsCode=T.CrsCode AND T.Sem=S2000
Also equivalent to:

CrsName C_CrsCode=T_CrsCode AND Sem=S2000


(Course [C_CrsCode, DeptId, CrsName, Desc]
Teaching [ProfId, T_CrsCode, Sem])

20/3

INFT 2301: Fundamentals of Databases, School of

End of Chapter 3

21/3

INFT 2301: Fundamentals of Databases, School of

Vous aimerez peut-être aussi