Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Relational Algebra

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

RELATIONAL ALGEBRA

Relational Algebra

• Relational algebra operations


– work on one or more relations to define another
relation without changing the original relations.

• Both operands and results are relations, so


output from one operation can become input
to another operation.

2
Relational Algebra

• Five basic operations in relational algebra:


– Selection
– Projection
– Cartesian product
– Union
– Set Difference.
– Intersection
• These perform most of the data retrieval
operations needed.

3
Relational Algebra Operations

4
Relational Algebra Operations

5
Instance of DreamHome rental
database

6
Selection

• predicate (R)
– Works on a single relation R and defines a
relation that contains only those tuples (rows) of
R that satisfy the specified condition (predicate).

7
Selection

• List all staff with a salary greater than £10,000.

salary > 10000 (Staff)

8
Example - Selection

• List all staff with a salary greater than


£10,000.
salary > 10000 (Staff)

9
Relational Algebra Operations

10
© Pearson Education Limited 1995, 2005
Projection

• col1, . . . , coln(R)
– Works on a single relation R and defines a
relation that contains a vertical subset of R,
extracting the values of specified attributes and
eliminating duplicates.

11
© Pearson Education Limited 1995, 2005
Projection

• Produce a list of salaries for all staff,


showing only staffNo, fName, lName, and
salary details.
staffNo, fName, lName, salary(Staff)

12
© Pearson Education Limited 1995, 2005
Example - Projection

• Produce a list of salaries for all staff,


showing only staffNo, fName, lName, and
salary details.
staffNo, fName, lName, salary(Staff)

13
Relational Algebra Operations

14
Union

• RS
– Union of two relations R and S defines a relation
that contains all the tuples of R, or S, or both R
and S, duplicate tuples being eliminated.
– R and S must be union-compatible.

• If R and S have I and J tuples, respectively,


union is obtained by concatenating them
into one relation with a maximum of (I + J)
tuples.

15
© Pearson Education Limited 1995, 2005
Union compatible

• both have same schema and the matching


attributes have the same domain.

 Two relations H1 and H2 are union-compatible


if H1 and H2 have the same degree and the
matching attributes in H1 and H2 have the
same domain. 

16
Example - Union

• List all cities where there is either a branch


office or a property for rent.
city(Branch)  city(PropertyForRent)

17
© Pearson Education Limited 1995, 2005
Example - Union

• List all cities where there is either a branch


office or a property for rent.
city(Branch)  city(PropertyForRent)

18
© Pearson Education Limited 1995, 2005
Relational Algebra Operations

19
© Pearson Education Limited 1995, 2005
Set Difference

• R–S
– Defines a relation consisting of the tuples that
are in relation R, but not in S.
– R and S must be union-compatible.

20
© Pearson Education Limited 1995, 2005
Example - Set Difference
• List all cities where there is a branch office
but no properties for rent.
city(Branch) – city(PropertyForRent)

21
© Pearson Education Limited 1995, 2005
Example - Set Difference

• List all cities where there is a branch office


but no properties for rent.
city(Branch) – city(PropertyForRent)

22
© Pearson Education Limited 1995, 2005
Relational Algebra Operations

23
© Pearson Education Limited 1995, 2005
Intersection

• RS
– Defines a relation consisting of the set of all
tuples that are in both R and S.
– R and S must be union-compatible.

24
© Pearson Education Limited 1995, 2005
Example - Intersection
• List all cities where there is both a branch
office and at least one property for rent.
city(Branch)  city(PropertyForRent)

25
© Pearson Education Limited 1995, 2005
Example - Intersection

• List all cities where there is both a branch


office and at least one property for rent.
city(Branch)  city(PropertyForRent)

26
© Pearson Education Limited 1995, 2005
Relational Algebra Operations

27
© Pearson Education Limited 1995, 2005
Cartesian product

• RXS
– Defines a relation that is the concatenation of
every tuple of relation R with every tuple of
relation S.

28
© Pearson Education Limited 1995, 2005
Example - Cartesian product
• List the names and comments of all clients who
have viewed a property for rent.
(clientNo, fName, lName(Client)) X (clientNo, propertyNo, comment
(Viewing))

29
Example - Cartesian product

30
End….

• These perform most of the data retrieval


operations needed.

31

You might also like