Relational Algebra

COMP3311 20T3 ♢ Relational Algebra ♢ [0/16]
❖ Relational Algebra

Relational algebra (RA) can be viewed as ...

Relational algebra consists of: Why is it important?
COMP3311 20T3 ♢ Relational Algebra ♢ [1/16]
❖ Relational Algebra (cont)


Core relational algebra operations:

Common extensions include:
COMP3311 20T3 ♢ Relational Algebra ♢ [2/16]
❖ Relational Algebra (cont)

Select, project, join provide a powerful set of operations for building relations and extracting interesting data from them.

[Diagram:Pics/relalg/spj.png]

Adding set operations and renaming makes RA complete.

COMP3311 20T3 ♢ Relational Algebra ♢ [3/16]
❖ Notation

Standard treatments of relational algebra use Greek symbols.

We use the following notation (because it is easier to reproduce):

Operation Standard
Notation
Our
Notation
Selection σexpr(Rel) Sel[expr](Rel)
Projection πA,B,C(Rel) Proj[A,B,C](Rel)
Join Rel1 expr Rel2 Rel1  Join[expr]  Rel2
Rename ρschemaRel Rename[schema](Rel)

For other operations (e.g. set operations) we adopt the standard notation.
Except when typing in a text file, where * = intersection, + = union

COMP3311 20T3 ♢ Relational Algebra ♢ [4/16]
❖ Describing RA Operations

We define the semantics of RA operations using

For each operation, we also describe it operationally:
COMP3311 20T3 ♢ Relational Algebra ♢ [5/16]
❖ Describing RA Operations (cont)

All RA operators return a result of type relation.

For convenience, we can name a result and use it later.

E.g.

Temp = R op1 S op2 T
Res  = Temp op3 Z
-- which is equivalent to
Res  = (R op1 S op2 T) op3 Z

Each "intermediate result" has a well-defined schema.

COMP3311 20T3 ♢ Relational Algebra ♢ [6/16]
❖ Example Database #1


[Diagram:Pics/relalg/db1.png]

COMP3311 20T3 ♢ Relational Algebra ♢ [7/16]
❖ Example Database #2


[Diagram:Pics/relalg/db2.png]

COMP3311 20T3 ♢ Relational Algebra ♢ [8/16]
❖ Rename

Rename provides "schema mapping".

If expression E returns a relation R(A1, A2, ... An), then

Rename[S(B1, B2, ... Bn)](E)

gives a relation called S

Rename is like the identity function on the contents of a relation

The only thing that Rename changes is the schema.

COMP3311 20T3 ♢ Relational Algebra ♢ [9/16]
❖ Rename (cont)

Rename can be viewed as a "technical" apparatus of RA.

We can also use implicit rename/project in sequences of RA operations, e.g.

--  R(a,b,c),  S(c,d)
Res = Rename[Res(b,c,d)](Project[b,c](Sel[a>5](R)) Join S)
-- vs
Tmp1 = Select[a>5](R)
Tmp2 = Project[b,c](Tmp1)
Tmp3 = Rename[Tmp3(cc,d)](S)
Tmp4 = Tmp2 Join[c=cc] Tmp3
Res  = Rename[Res(b,c,d)](Tmp4)
-- vs
Tmp1(b,c)  = Select[a>5](R)
Tmp2(cc,d) = S
Res(b,c,d) = Tmp1 Join[c=cc] Tmp2

In SQL, we achieve a similar effect by defining a set of views

COMP3311 20T3 ♢ Relational Algebra ♢ [10/16]
❖ Selection

Selection returns a subset of the tuples in a relation r(R) that satisfy a specified condition C.

σC(r)   =   Sel[C](r)   =   { t  |  t ∈ r ∧ C(t) }

C is a boolean expression on attributes in R.

Result size:   |σC(r)| |r|

Result schema:   same as the schema of r   (i.e. R)

Algorithmic view:

result = {}
for each tuple t in relation r
    if (C(t)) { result = result ∪ {t} }

COMP3311 20T3 ♢ Relational Algebra ♢ [11/16]
❖ Selection (cont)

Examples of selection:

[Diagram:Pics/relalg/selection2.png]

COMP3311 20T3 ♢ Relational Algebra ♢ [12/16]
❖ Selection (cont)

Querying with relational algebra (selection) ...

COMP3311 20T3 ♢ Relational Algebra ♢ [13/16]
❖ Projection

Projection returns a set of tuples containing a subset of the attributes in the original relation.

πX(r)   =   Proj[X](r)   =   { t[X]  |  t ∈ r },     where r(R)

X specifies a subset of the attributes of R.

Note that removing key attributes can produce duplicates.

In RA, duplicates are removed from the result set.
(In RDBMS's, duplicates are retained   (i.e. they use bags, not sets))

Result size:   |πX(r)| |r|     Result schema:   R'(X)

Algorithmic view:

result = {}
for each tuple t in relation r
    result = result ∪ {t[X]}

COMP3311 20T3 ♢ Relational Algebra ♢ [14/16]
❖ Projection (cont)

Examples of projection:

[Diagram:Pics/relalg/projection2.png]

COMP3311 20T3 ♢ Relational Algebra ♢ [15/16]
❖ Projection (cont)

Querying with relational algebra (projection)...

COMP3311 20T3 ♢ Relational Algebra ♢ [16/16]


Produced: 6 Nov 2020