Professional Documents
Culture Documents
Naranker Dulay
n.dulay@imperial.ac.uk
https://www.doc.ic.ac.uk/~nd/databases
The Relational Model
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
Tuple = Set of attribute values, one for each attribute belonging to attribute type.
In mathematics:
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:
Movies
Wayne’s
1992 Comedy 95
World
Gone with
1939 Drama 231
the Wind
For example:
Relational expressions are used construct new relations from other relations.
Operators include:
a x a x
× =
b y a y
c b x
b y
c x
c y
intersection union difference
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.
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
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
a b c c a b b a 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
Set of tuples that are in R but not in 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
a b c a b c a b c
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
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
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
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
a b c a b c b a c
1 2 3 4 5 6
1 1 1 1 1 1
2 2 2 2 2 2
1 2 3 4 5 8
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
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
a b c d e a b c d e
1 2 1 2 3
3 4 4 5 6
7 8 9
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.
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
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
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
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
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.
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.
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.
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.
a b c a c d e a b c d e
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
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
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:
Access control - to define the data over which a permission should be granted.
πpersonname(σcompanyname=”Barclays”(worksfor))
πpersonname, city(
(σcompanyname=”Barclays”(workfor) ⋈ employee))
Names of all employees who live in the same city as the company they work for.
supplier(sname, address)
part(pname, colour)
catalog(sname, pname, cost)
Give relational expressions for each of the following:
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)
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))
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))
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))