You are on page 1of 49

Relational Algebra

Naranker Dulay

n.dulay@imperial.ac.uk
https://www.doc.ic.ac.uk/~nd/databases
The Relational Model

ATTRIBUTES (the columns)


name:type

HEADING title:string year:int length:int genre:string

Gone with
1939 231 Drama
the Wind

BODY TUPLES
Star Wars 1977 124 SF
(the rows)

Wayne’s
1992 95 Comedy
World

Movies RELATION
2
N. Dulay Databases : 02 : Relational Algebra
Relations

Relation = Relation Heading plus Relation Body

Heading = (Unordered) Set of Attributes


Attribute Types are also known as Domains
Attribute = Attribute Name plus Type (normally simple indivisible types).
Why not lists, arrays, graphs, relations?
Body = (Unordered) Set of Tuples

Tuple = Set of attribute values, one for each attribute belonging to attribute type.

Schema for Relation = Name of relation plus heading, for example:

movies(title:string, year:int, length:int, genre:string)

Database = One or more Relations

Schema for Database = Schemas for all relations.

N. Dulay Databases : 02 : Relational Algebra 3


Relations II

In mathematics:

Given sets T1, T2, ... Tn

A relation R is a set of tuples (V1, V2, ... Vn) where Vk ∈ Tk

R is a subset of T1 x T2 ... x Tn.

Tuples are said to be R-related or ‘in R’. e.g. Movies-related or in Movies

n is the degree of the relation, while the number of tuples is the cardinality

Extending to the relational model we have attributed rather than ordered tuples:

R is the set of tuples (A1:T1=V1, ... An:Tn=Vn) where Vk ∈ Tk

N. Dulay Databases : 02 : Relational Algebra 4


Drawing a relation

Important. In the relational model, the ordering of attributes and tuples is


unimportant matter. We can present relations using whatever permutation of
attributes and tuples we like, e.g.:

Movies

year:int genre:string title:string length:int

1977 SF Star Wars 124

Wayne’s
1992 Comedy 95
World

Gone with
1939 Drama 231
the Wind

N. Dulay Databases : 02 : Relational Algebra 5


Drawing a relation II

Relations are not 2-dimensional tables. Although this a convenient way of


presenting them on paper.

A better way to think of them is as a set of n-dimensional values.

For example:

(title:string=StarWars, year:int=1977, length:int=127, genre:string=SF)

is a 4-dimensional movie value.

N. Dulay Databases : 02 : Relational Algebra 6


Relational Algebra

Relational expressions are used construct new relations from other relations.
Operators include:

selection projection product

a x a x
× =
b y a y
c b x
b y
c x
c y
intersection union difference

N. Dulay Databases : 02 : Relational Algebra 7


Applying Set Operations on Relations

Operands must have schemas with an identical set of attributes (name and type).

Before applying union, intersection, or difference we need to ensure that the order of
attributes used for both relations is the same. We can use any ordering we like.

We can use the renaming operator (see later) to change the attribute names of a
relation. This is useful where relations are the same except for one or more attribute
names. For example, an attribute might be called Surname in one relation,
Lastname in another.

For conciseness in the following slides, we shall restrict ourselves to using integer
attributes named a, b, c etc. We’ll omit the type in headings.
Checkout textbooks for more “real-life” relations.

N. Dulay Databases : 02 : Relational Algebra 8


Union R ∪ S
R S R∪S

a b c a b c a b c

1 2 3 7 8 9

4 5 6 2 2 2

7 8 9 3 4 5

1 2 3

Set of tuples in that are in R or in S.

N. Dulay Databases : 02 : Relational Algebra 9


Union R ∪ S
R S R∪S

a b c a b c a b c

1 2 3 7 8 9 1 2 3

4 5 6 2 2 2 4 5 6

7 8 9 3 4 5 7 8 9

1 2 3 2 2 2

3 4 5

Set of tuples in that are in R or in S.

N. Dulay Databases : 02 : Relational Algebra X


Union R ∪ S
R S R∪S

a b c c a b b a c

N. Dulay Databases : 02 : Relational Algebra 10


Difference R − S
R S R−S

a b c a b c a b c

1 2 3 7 8 9

4 5 6 2 2 2

7 8 9 3 4 5

2 2 2 1 2 3

3 4 8
Set of tuples that are in R but not in S

N. Dulay Databases : 02 : Relational Algebra 11


Difference R − S
R S R−S

a b c a b c a b c

1 2 3 7 8 9 4 5 6

4 5 6 2 2 2 3 4 8

7 8 9 3 4 5

2 2 2 1 2 3

3 4 8
Set of tuples in that are in R but not in S

N. Dulay Databases : 02 : Relational Algebra X


Difference R − S
R S R-S

a b c a b c a b c

N. Dulay Databases : 02 : Relational Algebra 12


R−S≢S−R
R S R−S S−R

a b c a b c a b c a b c

1 2 3 7 8 9

4 5 6 2 2 2

7 8 9 3 4 5

2 2 2 1 2 3

3 4 8

R − S is not equivalent to S − R

N. Dulay Databases : 02 : Relational Algebra 13


R−S≢S−R
R S R−S S−R

a b c a b c a b c a b c

1 2 3 7 8 9 4 5 6 3 4 5

4 5 6 2 2 2 3 4 8

7 8 9 3 4 5

2 2 2 1 2 3

3 4 8

R − S is not equivalent to S − R

N. Dulay Databases : 02 : Relational Algebra X


Intersection R ∩ S
R S R∩S

a b c a b c a b c

1 2 3 7 8 9

4 5 6 2 2 2

7 8 9 3 4 5

1 2 3

Set of tuples in that are in R and in S.

Intersection can be formulated using difference: R ∩ S = R − (R − S)

N. Dulay Databases : 02 : Relational Algebra 14


Intersection R ∩ S
R S R∩S

a b c a b c a b c

1 2 3 7 8 9 1 2 3

4 5 6 2 2 2 7 8 9

7 8 9 3 4 5

1 2 3

Set of tuples in that are in R and in S.

Intersection can be formulated using difference: R ∩ S = R − (R − S)

N. Dulay Databases : 02 : Relational Algebra X


Intersection R ∩ S
R S R∩S

a b c a b c b a c

N. Dulay Databases : 02 : Relational Algebra 15


Projection πattributes(R)
R πa,c,e(R)
a b c d e f a c e

1 2 3 4 5 6

1 1 1 1 1 1

2 2 2 2 2 2

1 2 3 4 5 8

Projection returns relation with listed attributes only.

Note: no duplicate tuples in the result.

N. Dulay Databases : 02 : Relational Algebra 16


Projection πattributes(R)
R πa,c,e(R)
a b c d e f a c e

1 2 3 4 5 6 1 3 5

1 1 1 1 1 1 1 1 1

2 2 2 2 2 2
2 2 2

1 2 3 4 5 8

Projection returns relation with listed attributes only.

Note: no duplicate tuples in the result.

N. Dulay Databases : 02 : Relational Algebra X


Selection σcondition(R)
R σa>3 | b<5(R)
a b c a b c

1 2 3

4 5 6

7 8 9

1 5 3

2 9 1

5 8 9
Selection returns relation with all tuples that
2 6 2
satisfy condition.
3 4 5
σ is lowercase sigma (greek alphabet)
N. Dulay Databases : 02 : Relational Algebra 17
Selection σcondition(R)
R σa>3 | b<5(R)
a b c a b c

1 2 3 1 2 3

4 5 6 4 5 6

7 8 9 7 8 9

1 5 3 5 8 9

2 9 1 3 4 5

5 8 9 Selection returns relation with all tuples that


2 6 2 satisfy condition.

3 4 5 σ is lowercase sigma (greek alphabet)

N. Dulay Databases : 02 : Relational Algebra X


Selection σcondition(R)
R σcondition(R)
a b c a b c

N. Dulay Databases : 02 : Relational Algebra 18


Cartesian Product R × S
R S R×S

a b c d e a b c d e

1 2 1 2 3

3 4 4 5 6

7 8 9

Resulting relation has set of all tuples that


can be paired by including a tuple of R and a
tuple from S.

Schema for the result includes the schemas for


both relations. Also known as cross-product.
N. Dulay Databases : 02 : Relational Algebra 19
Cartesian Product R × S
R S R×S

a b c d e a b c d e

1 2 1 2 3 1 2 1 2 3

3 4 4 5 6 1 2 4 5 6

7 8 9 1 2 7 8 9

3 4 1 2 3
Resulting relation has set of all tuples that
can be paired by including a tuple of R and a
3 4 4 5 6
tuple from S.

Schema for the result includes the schemas for 3 4 7 8 9


both relations. Also known as cross-product.
N. Dulay Databases : 02 : Relational Algebra X
Cartesian Product R × S
R S R×S

a b b c d a R.b S.b c d

1 2 1 2 3

3 4 4 5 6

7 8 9

If a R and S have attribute names in


common, then we distinguish them by
prefixing the attribute name with the name
of the relation.

N. Dulay Databases : 02 : Relational Algebra 20


Cartesian Product R × S
R S R×S

a b b c d a R.b S.b c d

1 2 1 2 3 1 2 1 2 3

3 4 4 5 6 1 2 4 5 6

7 8 9 1 2 7 8 9

3 4 1 2 3
If a R and S have attribute names in
common, then we distinguish them by 3 4 4 5 6
prefixing the attribute name with the name
of the relation. 3 4 7 8 9

N. Dulay Databases : 02 : Relational Algebra X


Cartesian Product R × S = S × R
R×S S×R

a b c d e c d e a b

1 2 1 2 3 1 2 3 1 2

1 2 4 5 6 1 2 3 3 4

1 2 7 8 9 4 5 6 1 2

3 4 1 2 3 4 5 6 3 4

3 4 4 5 6 7 8 9 1 2

3 4 7 8 9 7 8 9 3 4

N. Dulay Databases : 02 : Relational Algebra 21


Cartesian Product R × S = S × R
R×S S×R

a b c d e c d e a b

1 2 1 2 3 1 2 3 1 2

1 2 4 5 6 1 2 3 3 4

1 2 7 8 9 4 5 6 1 2

3 4 1 2 3 4 5 6 3 4

3 4 4 5 6 7 8 9 1 2

3 4 7 8 9 7 8 9 3 4

N. Dulay Databases : 02 : Relational Algebra X


Natural Join R ⋈ S
R S R⋈S

a b b c d a b c d

1 2 1 2 3

1 3 2 5 6

3 4 4 8 9

4 9 9

Resulting relation has all tuples that can be “joined” using all matching attributes
of R and S.

Resulting schema is the set of both schemas.


N. Dulay Databases : 02 : Relational Algebra 22
Natural Join R ⋈ S
R S R⋈S

a b b c d a b c d

1 2 1 2 3 1 2 5 6

1 3 2 5 6 3 4 8 9

3 4 4 8 9 3 4 9 9

4 9 9

Resulting relation has all tuples that can be “joined” using all matching attributes
of R and S.

Resulting schema is the set of both schemas.


N. Dulay Databases : 02 : Relational Algebra X
Natural Join R ⋈ S
R S R⋈S

a b c b c d a b c d

1 2 3 2 3 3

6 7 8 2 3 6

9 7 8 4 8 9

4 2 7 7 8 3

1 7 8

Must only pair tuples if values of each common attribute are equal.

N. Dulay Databases : 02 : Relational Algebra 23


Natural Join R ⋈ S
R S R⋈S

a b c b c d a b c d

1 2 3 2 3 3 1 2 3 3

6 7 8 2 3 6 1 2 3 6

9 7 8 4 8 9 6 7 8 3

4 2 7 7 8 3 9 7 8 3

1 7 8 1 7 8 3

Must only pair tuples if values of each common attribute are equal.

N. Dulay Databases : 02 : Relational Algebra X


Natural Join R ⋈ S
R S R⋈S

a b c a c d e a b c d e

N. Dulay Databases : 02 : Relational Algebra 24


Natural Join

Natural join can be expressed in terms of product, selection and projection:

R⋈S= πL (σC(R × S))


where L is union of all attributes of R and S i.e. attr(R) ∪ attr(S)

C is R.a1=S.a1 & R.a2 = S.a2 & ... & R.an = S.an


where ak are the attributes common to R and S, ak ∈attr(R) ∩ attr(S)

Natural join is associative: (R ⋈ S) ⋈ T = R ⋈ (S ⋈ T)

If there is no common attribute then: R ⋈ S = R × S i.e. we don’t apply selection


and projection.

N. Dulay Databases : 02 : Relational Algebra 25


Renaming ρnew /old , new /old , ..., new /old (R)
1 1 2 2 n n

R ρx/a, y/d(R)
a b c d

1 2 5 6 1 2 5 6

3 4 8 9 3 4 8 9

3 4 9 9 3 4 9 9

Changes the names of one or more attributes. ρ is lowercase rho


Note: the values and types of the relation are not affected.
N. Dulay Databases : 02 : Relational Algebra 26
Renaming ρnew /old , new /old , ..., new /old (R)
1 1 2 2 n n

R ρx/a, y/d(R)
a b c d x b c y

1 2 5 6 1 2 5 6

3 4 8 9 3 4 8 9

3 4 9 9 3 4 9 9

Changes the names of one or more attributes.

Note: the values and types of the relation are not affected.
N. Dulay Databases : 02 : Relational Algebra X
Relational Expressions

Relational expressions are used to construct new relations from other relations in:

Retrieval - to define data to be retrieved.

Updates - to define data to be inserted, changed, or deleted.

Constraints - to define constraints that the database must satisfy.

Derived relations (views) - to define one relation in terms of others, without


constructing it.

Concurrency - to define the data to use for concurrent actions.

Access control - to define the data over which a permission should be granted.

A relational database language is said to be relationally complete if it is at least as


expressive as Relational Algebra.

N. Dulay Databases : 02 : Relational Algebra 27


Exercise 1

Consider the database schema (types omitted):

employee(personname, street, city)


worksfor(personname, companyname, salary)
company(companyname, city)
Give relational expressions for each of the following:

1. Names of all employees who work for Barclays.


2. Names of Barclays employees and the city where they live.
3. Names, street and city of all Barclays employees who earn more than £1M.

4. What will πpersonname(employee ⋈ worksfor ⋈


company) return?
N. Dulay Databases : 02 : Relational Algebra 28
Exercise 1

employee(personname, street, city)


worksfor(personname, companyname, salary)
company(companyname, city)
1. Names of all employees who work for Barclays.

πpersonname(σcompanyname=”Barclays”(worksfor))

N. Dulay Databases : 02 : Relational Algebra X


Exercise 1

employee(personname, street, city)


worksfor(personname, companyname, salary)
company(companyname, city)
2. Names of Barclays employees and city where they live.

πpersonname, city(
(σcompanyname=”Barclays”(workfor) ⋈ employee))

N. Dulay Databases : 02 : Relational Algebra X


Exercise 1

employee(personname, street, city)


worksfor(personname, companyname, salary)
company(companyname, city)
3. Names, street and city of all Barclays employees who earn more than £1M.

πpersonname, street, city(


σcompanyname=”Barclays” & salary>1000000(worksfor)
⋈ employee)

N. Dulay Databases : 02 : Relational Algebra X


Exercise 1

employee(personname, street, city)


worksfor(personname, companyname, salary)
company(companyname, city)
4. What will

πpersonname (employee ⋈ worksfor ⋈ company)


return?

Names of all employees who live in the same city as the company they work for.

N. Dulay Databases : 02 : Relational Algebra X


Exercise 2

Consider the database schema (types omitted):

supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
Give relational expressions for each of the following:

1. Names of all suppliers who supply a red part.


2. Names of all suppliers who supply a red part or are at 18 Queensgate
3. Names of parts supplied by at least two different suppliers.
Assume: C1 and C2 are temporary copies of the catalog relation.
4. Pairs of suppliers such that the first suppliers charges more for some part than the
second supplier. Assume: C1 and C2 are temporary copies of the catalog relation.

N. Dulay Databases : 02 : Relational Algebra 29


Exercise 2

supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
1. Names of all suppliers who supply a red part

πsname(σcolour=”red”(part) ⋈ catalog)

N. Dulay Databases : 02 : Relational Algebra X


Exercise 2

supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
2. Names of all suppliers who supply a red part or are at 180 Queensgate

πsname(σcolour=”red”(part) ⋈ catalog) ∪
πsname(σaddress=”180 Queensgate”(supplier))

N. Dulay Databases : 02 : Relational Algebra X


Exercise 2

supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
3. Names of parts supplied by at least two different suppliers.
Assume: C1 and C2 are copies of the catalog relation.

πC1.pname(
σC1.pname=C2.pname & C1.sname≠C2.sname(C1×C2))

N. Dulay Databases : 02 : Relational Algebra X


Exercise 2

supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
4. Pairs of suppliers such that the first supplier charges more for some part than the
second supplier. Assume: C1 and C2 are temporary copies of the catalog relation.

πC1.sname, C2.sname(
σC1.pname=C2.pname & C1.sname≠C2.sname &
C1.cost>C2.cost(C1 × C2))

N. Dulay Databases : 02 : Relational Algebra X

You might also like