Introduction to Relational Algebra

by K. Yue

1. Introduction

2. Basic Operations

Cartesian Product

CartesianProduct.png

Example:

R(A,B,C) has three tuples. S(A,D) has four tuples.

The result of R x S always has 12 tupes with the schema (R.A, B, C, S.A, D).

Select

select

σcond(R) = {t | t ε R and cond}

Project

πc1, .., cm(R) = {s | there exists t ε R (t(ci) = s(ci), for 1 <= i <= m)}

project

Union

R U S = {t | t ε R or t ε S}

Example:

Suppose StaffID and FacultyID are uniion compatible.

 πStaffID(Staff) U πFacultyID(Faculty)

Difference (Minus)

R - S = {t | t ε R and not (t ε S)}

Rename

rename

3. Common Derived Operations

Theta-join

R1 |x|Θ R2

Equi-join

Natural Join

Let C1, C2, ... Cm be the common attributes of R1 and R2.

R |x| S = πA1, A2, .. Al(σR.C1=S.C1,.., R.Cm=S.Cm(RxS)

where A1, A2, ... Al is the list of components in RxS except S.C1, S.C2,.. S.Cm.

Example:

The schema of R(A,B) |x| S(A,C) is ABC. The schema of R(A,B) x S(A,C) is {R.A, B, S.A, C}.

Exercise

Let the cardinality of R(A,B) be 5 and the cardinality of S(A,C) be 6. What is the range of the cardinality of R(A,B) |x| S(A,C)?

Other Joins

Division

Exercise:

Work on some of the query questions listed in the SupplY database example.

4. Epilog

Some shortcomings of Relational Algebra: