Docsity

Relational Algebra in Databases: A Comprehensive Guide with Examples, Slides of Algebra

Toggle sidebar
bg1
1
Relational Algebra —1
CSC343 Introduction to Databases —University of Toronto
Week 3 – Relational Algebra
Querying and Updating a Database
The Relational Algebra
Union, Intersection, Difference
Renaming, Selection and Projection
Join, Cartesian Product
Relational Algebra —2
CSC343 Introduction to Databases —University of Toronto
Query Languages for
Relational Databases
Operations on databases:
Queries — read data from the database;
Updates — change the content of the database.
In this lecture unit we discuss the relational algebra,
a procedural language that defines database
operations in terms of algebraic expressions.
[The Relational Calculus is a declarative language
for database operations based on Predicate Logic;
we will not discuss it here.]
Relational Algebra —3
CSC343 Introduction to Databases —University of Toronto
Relational Algebra
A collection of algebraic operators that
Are defined on relations;
Produce relations as results,
and therefore can be combined to form
complex algebraic expressions.
Operators:
Union, intersection, difference;
Renaming;
Selection and Projection;
Join (natural join, Cartesian product, theta
join).
Relational Algebra —4
CSC343 Introduction to Databases —University of Toronto
Union, Intersection, Difference
Relations are sets, so we can apply set-theoretic
operators
However, we want the results to be relations
(that is, homogeneous sets of tuples)
It is therefore meaningful to only apply union,
intersection, difference to pairs of relations
defined over the same attributes.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Relational Algebra in Databases: A Comprehensive Guide with Examples and more Slides Algebra in PDF only on Docsity!

Relational Algebra

— 1

CSC343 Introduction to Databases

—

University of Toronto

Week 3 – Relational Algebra

Querying and Updating a Database

The Relational Algebra

Union, Intersection, Difference

Renaming, Selection and Projection

Join, Cartesian Product

Relational Algebra

— 2

CSC343 Introduction to Databases

—

University of Toronto

Query Languages forRelational Databases

Æ

Operations on databases:

Queries

— read data from the database;

Updates

— change the content of the database.

Æ

In this lecture unit we discuss the relational algebra,a procedural language that defines databaseoperations in terms of algebraic expressions.

Æ

[The Relational Calculus is a declarative languagefor database operations based on Predicate Logic;we will not discuss it here.]

Relational Algebra

— 3

CSC343 Introduction to Databases

—

University of Toronto

Relational Algebra

Æ

A collection of

algebraic operators

that

Are defined on relations;

Produce relations as results,

and

therefore

can

be

combined

to

form

complex algebraic expressions.

Operators

Union, intersection, difference;

Renaming;

Selection and Projection;

Join

(natural

join,

Cartesian

product,

theta

join).

Relational Algebra

— 4

CSC343 Introduction to Databases

—

University of Toronto

Union, Intersection, Difference

Æ

Relations are sets, so we can apply set-theoreticoperators

Æ

However, we want the results to be relations(that is, homogeneous sets of tuples)

Æ

It is therefore meaningful to only apply union,intersection,

difference

to

pairs

of

relations

defined over the same attributes.

Relational Algebra

— 5

CSC343 Introduction to Databases

—

University of Toronto

Union

Number

Surname

Age

7274

Robinson

37

7432

O'Malley

39

9824

Darkes

38

Number

Surname

Age

9297

O'Malley

56

7432

O'Malley

39

9824

Darkes

38

Graduates

Managers

Number

Surname

Age

7274

Robinson

37

7432

O'Malley

39

9824

Darkes

38

9297

O'Malley

56

Graduates

∪

Managers

Relational Algebra

— 6

CSC343 Introduction to Databases

—

University of Toronto

Intersection

Number

Surname

Age

7274

Robinson

37

7432

O'Malley

39

9824

Darkes

38

Number

Surname

Age

9297

O'Malley

56

7432

O'Malley

39

9824

Darkes

38

Graduates

Managers

Number

Surname

Age

7432

O'Malley

39

9824

Darkes

38

Graduates

∩

Managers

Relational Algebra

— 7

CSC343 Introduction to Databases

—

University of Toronto

Difference

Number

Surname

Age

7274

Robinson

37

7432

O'Malley

39

9824

Darkes

38

Number

Surname

Age

9297

O'Malley

56

7432

O'Malley

39

9824

Darkes

38

Graduates

Managers

Number

Surname

Age

7274

Robinson

37

Graduates - Managers

Relational Algebra

— 8

CSC343 Introduction to Databases

—

University of Toronto

Paternity

∪

Maternity ???

Father

Child

Adam

Cain

Adam

Abel

Abraham

Isaac

Abraham

Ishmael

Paternity

Mother

Child

Eve

Cain

Eve

Seth

Sarah

Isaac

Hagar

Ishmael

Maternity

A Meaningful but Impossible Union

Æ

The problem:

Father

and

Mother

are different

names, but both represent a parent.

Æ

The solution: rename attributes!

Relational Algebra

— 13

CSC343 Introduction to Databases

—

University of Toronto

Selection and Projection

Æ

These are unary operators, in a senseorthogonal:

selection for "horizontal" decompositions;

projection for "vertical" decompositions.

A B C A B C

Selection

A B C

Projection

A B

Relational Algebra

— 14

CSC343 Introduction to Databases

—

University of Toronto

Selection

Æ

This is a unary operation which returns a relation

with the same schema as the operand;

but, with a

subset of the tuples

of the operand,

i.e., only those that satisfy a condition.

Æ

Notation:

F

(r)

Æ

Semantics:

σ

F

(r) = { t | t

∈

r s.t. t satisfies F, I.e., F(t)}

Relational Algebra

— 15

CSC343 Introduction to Databases

—

University of Toronto

We can use

the or

symbol (

v),

Not symbol

(

¬

) &

and

symbol

(ʌ)

as we would

for

specifying any logical

condition

Selection Example

Surname

FirstName

Age

Salary

Smith

Mary

25

2000

Black

Lucy

40

3000

Verdi

Nico

36

4500

Smith

Mark

40

3900

Employees

Surname

FirstName

Age

Salary

Smith

Mary

25

2000

Verdi

Nico

36

4500

σ

Age<30 v Salary>

(Employees)

Relational Algebra

— 16

CSC343 Introduction to Databases

—

University of Toronto

Selection, Another Example

Surname

FirstName

PlaceOfBirth

Residence

Smith

Mary

Rome

Milan

Black

Lucy

Rome

Rome

Verdi

Nico

Florence

Florence

Smith

Mark

Naples

Florence

Citizens

σ

PlaceOfBirth=Residence

(Citizens)

Surname

FirstName

PlaceOfBirth

Residence

Black

Lucy

Rome

Rome

Verdi

Nico

Florence

Florence

Relational Algebra

— 17

CSC343 Introduction to Databases

—

University of Toronto

Projection

Æ

Projection returns a relation which includes a

subset

of the attributes

of the operand.

Æ

Notation: Given a relation r(X) and a subset Y of X:

π

Y

(r)

Æ

Semantics:

π

Y

(r) = { t[Y] | t

r }

Relational Algebra

— 18

CSC343 Introduction to Databases

—

University of Toronto

Example of Projection

Surname

FirstName

Department

Head

Smith

Mary

Sales

De Rossi

Black

Lucy

Sales

De Rossi

Verdi

Mary

Personnel

Fox

Smith

Mark

Personnel

Fox

Employees

Surname

FirstName

Smith

Mary

Black

Lucy

Verdi

Mary

Smith

Mark

π

Surname, FirstName

(Employees)

Relational Algebra

— 19

CSC343 Introduction to Databases

—

University of Toronto

Another Example

Surname

FirstName

Department

Head

Smith

Mary

Sales

De Rossi

Black

Lucy

Sales

De Rossi

Verdi

Mary

Personnel

Fox

Smith

Mark

Personnel

Fox

Employees

Department

Head

Sales

De Rossi

Personnel

Fox

π

Department, Head

(Employees)

Relational Algebra

— 20

CSC343 Introduction to Databases

—

University of Toronto

Cardinality of Projection Operations

Æ

Note that the result of a projection contains at most as many tuples as the operand relation.

Æ

However, it may contain fewer, if several tuples collapse, i.e., they are identical in all their values.

Æ

Theorem:

π

Y

(r) contains as many tuples as r if

and only if Y is a superkey for r.

Æ

This property holds even if Y is "by chance” a superkey, i.e., it is not defined as a superkey in theschema,

but

it

is

a

superkey

for

the

current

database, see the example.

Relational Algebra

— 25

CSC343 Introduction to Databases

—

University of Toronto

Definition of Natural Join

Æ

r

1

(X

1

), r

2

(X

2

Æ

r

1

r

2

(natural join of r

1

and r

2

) is a relation on

X

1

X

2

(the

union

of the two sets):

{ t on X

1

X

2

| t [X

1

]

r

1

and t [X

2

]

r

2

or, equivalently

{ t on X

1

X

2

| exist t

1

r

1

and t

2

r

2

with t [X

1

] = t

1

and t [X

2

] = t

2

Relational Algebra

— 26

CSC343 Introduction to Databases

—

University of Toronto

Natural Join: Comments

Æ

The tuples in the resulting relation are obtained bycombining

tuples

in

the

operands

with

equal

values on the common attributes

Æ

The common attributes

often form a key of one

of

the

operands

(remember:

references

are

realized by means of foreign keys, and we join inorder to follow references)

‘

Not always! Consider Person(Name,Addr,PostalC) and letus

define

Neighbour(Name,Addr,Name1,Addr1,PostalC)

by

joining

Person

with

ρ

Name,Addr

→

Name1,Addr

(Person);

What is criterion for neighbourhood here?

Relational Algebra

— 27

CSC343 Introduction to Databases

—

University of Toronto

Another Example

Registration

Dept

Owner

…

6544 XY

75

Cordon Edouard

…

7122 HT

75

Cordon Edouard

…

5694 FR

75

Latour Hortense

…

6544 XY

47

Mimault Bernard

…

Code

Date

Officer

Dept

Registration

Owner

…

143256

25/10/

567

75

5694 FR

Latour Hortense

…

987554

26/10/

456

75

5694 FR

Latour Hortense

…

987557

26/10/

456

75

6544 XY

Cordon Edouard

…

630876

15/10/

456

47

6544 XY

Cordon Edouard

…

539856

12/10/

567

47

6544 XY

Mimault Bernard

…

Offences

⋈

Cars

Offences

Code

Date

Officer

Dept

Registartion

143256

25/10/

567

75

5694 FR

987554

26/10/

456

75

5694 FR

987557

26/10/

456

75

6544 XY

630876

15/10/

456

47

6544 XY

539856

12/10/

567

47

6544 XY

Cars

Relational Algebra

— 28

CSC343 Introduction to Databases

—

University of Toronto

Yet Another Join

Æ

In this example, join gives very different resultsfrom union (see earlier example)

Father

Child

Adam

Cain

Adam

Abel

Abraham

Isaac

Abraham

Ishmael

Paternity

Mother

Child

Eve

Cain

Eve

Seth

Sarah

Isaac

Hagar

Ishmael

Maternity

Paternity

Maternity

Father

Child

Mother

Adam

Cain

Eve

Abraham

Isaac

Sarah

Abraham

Ishmael

Hagar

Relational Algebra

— 29

CSC343 Introduction to Databases

—

University of Toronto

Joins can be Incomplete

Æ

If a tuple does not have a "counterpart" in theother relation, then it does not contribute to thejoin ("dangling" tuple)

Employee

Department

Smith

sales

Black

production

White

production

Department

Head

production

Mori

purchasing

Brown

Employee

Department

Head

Black

production

Mori

White

production

Mori

r

1

r

2

r

1

r

2

Relational Algebra

— 30

CSC343 Introduction to Databases

—

University of Toronto

Joins can be Empty

Æ

As an extreme, we might have that no tuple hasa counterpart, and all tuples are dangling

Employee

Department

Smith

sales

Black

production

White

production

Department

Head

marketing

Mori

purchasing

Brown

Employee

Department

Head

r

1

r

2

r

1

r

2

Relational Algebra

— 31

CSC343 Introduction to Databases

—

University of Toronto

Another Extreme

Æ

If each tuple of each operand can be combinedwith all the tuples of the other, then the join has acardinality that is the product of the cardinalitiesof the operands

Employee

Project

Smith

A

Black

A

White

A

Project

Head

A

Mori

A

Brown

Employee

Project

Head

Smith

A

Mori

Black

A

Brown

White

A

Mori

Smith

A

Brown

Black

A

Mori

White

A

Brown

r

1

r

2

r

1

r

2

Relational Algebra

— 32

CSC343 Introduction to Databases

—

University of Toronto

How Many Tuples in a Join?

Æ

Given r

1

(X

1

), r

2

(X

2

) the join has cardinality

| r

1

⋈

r

2

| r

1

×

| r

2

where | r | is the cardinality of relation r.

Æ

Moreover:

if the join is complete, then its cardinality is atleast the maximum of | r

1

| and | r

2

if X

1

X

2

contains a key for r

2

then | r

1

⋈

r

2

| r

1

if X

1

X

2

is the primary key for r

2

, and there is a

referential constraint between X

1

X

2

in r

1

and

such a key, then | r

1

⋈

r

2

| = | r

1

Relational Algebra

— 37

CSC343 Introduction to Databases

—

University of Toronto

Join and Intersection

Æ

We have made no assumptions about the setsof attributes X

1

and X

2

on which the operands

of a join operation are defined; the two setscould even be equal or disjoint.

Æ

If X

1

= X

2

then r

1

r

2

= r

1

r

2

since, by

definition, the result is a relation which includestuples t such that t[X

1

]

r

1

and t[X

2

]

r

2

, and

X

1

= X

2

Relational Algebra

— 38

CSC343 Introduction to Databases

—

University of Toronto

Natural Join as Cartesian Product

Æ

The natural join is defined also when theoperands have no attributes in common.

Æ

In this case no condition is imposed on tuples,and therefore the result contains tuples obtainedby combining the tuples of the operands in allpossible ways.

Relational Algebra

— 39

CSC343 Introduction to Databases

—

University of Toronto

Cartesian Product: Example

Employee

Project

Smith

A

Black

A

Black

B

Code

Name

A

Venus

B

Mars

Employee

Project

Code

Name

Smith

A

A

Venus

Black

A

A

Venus

Black

B

A

Venus

Smith

A

B

Mars

Black

A

B

Mars

Black

B

B

Mars

Employees

Projects

Employes

Projects

Relational Algebra

— 40

CSC343 Introduction to Databases

—

University of Toronto

Theta-Join

Æ

In most cases, a Cartesian product is meaningfulonly if followed by a selection:

theta-join

: a derived operator

r

1

⋈

F

r

2

σ

F

(r

1

⋈

r

2

if F is a conjunction of equalities, then wehave an

equi-join

Relational Algebra

— 41

CSC343 Introduction to Databases

—

University of Toronto

Equi-join: example

Employee

Project

Smith

A

Black

A

Black

B

Code

Name

A

Venus

B

Mars

Employee

Project

Code

Name

Smith

A

A

Venus

Black

A

A

Venus

Black

B

B

Mars

Employees

Projects

Employes

Project=Code

Projects

Relational Algebra

— 42

CSC343 Introduction to Databases

—

University of Toronto

Division

Æ

Consider two relations A(x,y), B(y) and supposewe want to specify the query"Find all A's that are associated with all B's"

Æ

This can be expressed as

A/B =

π

x

(A) -

π

x

π

x

(A)

B) - A)

Æ

This

means

that

division

does

not

extend

the

expressiveness of Relational Algebra, but it is aconvenient operation to use in many situations.

Relational Algebra

— 43

CSC343 Introduction to Databases

—

University of Toronto

Example of Division

Æ

Assume

Take(x,y) - "student x has taken course y",

CS(y) - "y is a CS course"

Æ

We want "All students who have taken all CScourses"

x

(Take)

CS --?

Table of all students, CS

courses

x

(Take)

CS) - Take -- ??

Table of all

students and the CS courses they have nottaken

x

x

(Take)

CS) - Take) -- ???

All

students who have not taken a CS course

Relational Algebra

— 44

CSC343 Introduction to Databases

—

University of Toronto

Queries

Æ

A query is a function from database instances torelations.

Æ

Queries are formulated in relational algebra bymeans of expressions over relations.

Relational Algebra

— 49

CSC343 Introduction to Databases

—

University of Toronto

Example 4

Number

Name

Salary

NumberH

NameH

SalaryH

104

Luigi Neri

61

210

Marco Celli

60

252

Nico Bini

70

375

Mary Smith

65

π

Number,Name,Salary,NumberH,Nam3H, SalaryH

(

σ

Salary>SalaryH

(

ρ

Num,Name,Sal,Age

→

NumH,NameH,SalH,AgeH

Employees

⋈

NumberH=Head

(Supervision

⋈

Emp=Number

Employees)))

Æ

“Find the employees earning more than their respective

supervisors,

return

registration

numbers,

names

and

salaries

of

the

employees and their supervisors.”

Employees(Number,Name,Age,Salary)Supervision(Head,Emp)

Try it! Definitely challenging

☺

Relational Algebra

— 50

CSC343 Introduction to Databases

—

University of Toronto

Example 5

Æ

“Find

registration

numbers

and

names

of

supervisors,

all

of whose employees earn more

than 40M.”

Employees(Number,Name,Age,Salary)Supervision(Head,Emp)

Number

Name

301

Steve Smith

375

Mary Smith

π

Number,Name

(Employees

⋈

Number=Head

(

π

Head

(Supervision) -

π

Head

(Supervision

⋈

Number=Emp

(

σ

Salary

≤

40

Employees))))

Try it!

Relational Algebra

— 51

CSC343 Introduction to Databases

—

University of Toronto

Another Series of Examples:

Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,

Nationality)

Roles(Film#,Actor#,Character)

Æ

Find “The titles of films starring Henry Fonda

π

Title

(Films

σ

(FirstName=“Henry”)

∧

(Surname=“Fonda”)

(Artists

Roles)))

Try it!

Relational Algebra

— 52

CSC343 Introduction to Databases

—

University of Toronto

Example 2

Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,

Nationality)

Roles(Film#,Actor#,Character)

Æ

Find “The titles of all films in which the director isalso an actor”

π

Title

σ

(Director=Actor#)

(Films

Roles))

Try it!

Relational Algebra

— 53

CSC343 Introduction to Databases

—

University of Toronto

Example 3

Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,

Nationality)

Roles(Film#,Actor#,Character)

Æ

Find “The actors who have played two characters inthe same film; show the title of each such film, firstname

and

surname

of

the

actor

and

the

two

characters”

π

Title,FirstName,Surname,Character1,Character

ρ

Film#,Actor#,Character

→

Film#1,Actor#1,Character

(Roles)

(Film#1=Film#)

∧

(Actor#1=Actor#)

∧

(Character

≠

Character)

Roles

Artists

Films)

Try it!

Relational Algebra

— 54

CSC343 Introduction to Databases

—

University of Toronto

Example 4

Films(Film#,Title,Director,Year,ProdCost)Artists(Actor#,Surname,FirsName,Sex,Birthday,

Nationality)

Roles(Film#,Actor#,Character)

Æ

“The titles of the films in which the actors are all ofthe same sex” π

Title

(Films) –

π

Title

(Films)

σ

sex

≠

sex

((Artists

Roles)

ρ

Ac#,Char,Sur,Fir,Sex,BD,N

→

Ac#1,Char1,Sur1,Fir1,Sex1,BD1,N

(Artists

Roles))

Try it!

Relational Algebra

— 55

CSC343 Introduction to Databases

—

University of Toronto

Relational Algebra and Null Values

Æ

Consider

σ

Age>

(People)

Æ

Which tuples belong to the result?

Æ

The first yes, the second no, but the third??

Name

Age

Salary

Aldo

35

15

Andrea

27

21

Maria

NULL

42

People

Relational Algebra

— 56

CSC343 Introduction to Databases

—

University of Toronto

Æ

Emp(Num,Name,Age,Sal)

Æ

"Find all ages for which there are at least twoemployees earning more than 40M" σ

sal>

(Emp)

σ

sal1>

ρ

Num,Name,Sal

→

Num1,Name1,Sal

Emp)

Æ

This results in a relation R with schema

??(Num,Name,Age,Sal,Num1,Name1,Sal1)

Æ

Now we select tuples where Num

Num1 and project

on Age

π

Age

σ

Num

≠

Num

R)

Æ

In a similar fashion, you can find ages for which thereare

3 employees earning >40M, etc.

Lecture Example (for blackboard)