0% found this document useful (0 votes)
27 views30 pages

T4 Relational Algebra

The document discusses various relational algebra operations that can be performed on relations in a database, including: - Rename operations to change relation or attribute names - Selection operations to retrieve tuples matching a condition - Projection operations to select a subset of attributes - Set operations like union, intersection, and difference on compatible relations - Cartesian products to combine all tuples between two relations - Theta joins to combine tuples where an attribute comparison evaluates to true The goal is to learn how to manipulate relations using these operations and how to combine them to perform complex queries.

Uploaded by

Leo Barcino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

T4 Relational Algebra

The document discusses various relational algebra operations that can be performed on relations in a database, including: - Rename operations to change relation or attribute names - Selection operations to retrieve tuples matching a condition - Projection operations to select a subset of attributes - Set operations like union, intersection, and difference on compatible relations - Cartesian products to combine all tuples between two relations - Theta joins to combine tuples where an attribute comparison evaluates to true The goal is to learn how to manipulate relations using these operations and how to combine them to perform complex queries.

Uploaded by

Leo Barcino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Relational Algebra

Databases
Goals

• Learning the different operations that are facilitated


relational algebra
• Learning how to apply these operations to access concrete
information.
• Learning how to combine operations to perform complex
queries

2
Relational Algebra
• So far we have seen how to structure data in a relational model.
• Relational algebra enables us to operate and manipulate
relations
• There are two general types of operations:
• Update: modification, insertion, and elimination
• Query.
• These operations are defined in what we call relational algebra.
• It will be the base on which the structured query languages are
defined, such as SQL.
3
Types of Operations
• Query operations affect one or more relations.
• They can include calculations based on values from outside the
database (e.g., current date).
• Update operations have to comply with the integrity rules:
• Uniqueness of the primary key
• Integrity of the primary key
• Referential integrity
• Domain integrity
4
Operations in Relational Algebra

•The operands and the results are relations


•They can be unary or binary (operating on one
or two operands, respectively)
•Operations can be combined if they need to
operate on more than two relations

5
Rename operation

Rename (ρ) operation allows us to change the name of a


relation or its attributes. It can also do the same for a relation
resulting from an operation.

• Allows changing the name of an attribute to simplify its access


• Allows assigning a descriptive name to an operation on two relations
• The resulting schema can be different from that of the input relation.
• The degree of the output relation will always be the same as the input
relation
6
Rename Operation: Example
Consider a relation called Professors, representing the
professors that work at a university.
• We can change the name of the relation itself as well as the name of
some of its attributes as follows:
ProfUni{NIF, name, lastName} := Professors{DNI, name, lastName}

7
Selection Operation
The selection (σ) operation enables selecting a subset of the tuples of
a relation that comply with a condition. The condition is formed by
clauses connected using Boolean operators “AND” (∧) and “OR” (∨).

• Outputs subsets of the data according to some condition


• The output will be a relation with the same degree and schema
• It performs a horizontal cut of the extension of the relation (selects a
subset of rows of the table)
• It also allows access to a single tuple
8
Selection Operation: Example
• To select all the professors that were born between 1974 and 1980,
we apply the following operation:
R := Professors(birthDate >= 1974 ∧ birthDate <= 1980)

• To select the professor with the NIF 00000000T, we apply the


following operation:
R := Professors(NIF = 00000000T)

9
Projection Operation

The projection (π) operation allows us to select a


subset of the attributes of a relation.

• The attributes are selected by their names.


• The result will be a new relation with a different degree and schema.
• It performs a vertical cut on the extensions of the relation (selects a
subset of columns of the table).

10
Projection Operation
• To select the names and last names of all professors, we apply
the following operation:

R := Professors[name, lastName]

• RA notation:
πname, lastName(Professor)

11
Notation:

• Rename:

ProfUni{NIF, name, lastName} := Professors{DNI, name, lastName}

• Selection:

R := Professors(birthDate >= 1974 ∧ birthDate <= 1980)

• Projection:

R := Professors[name, lastName]
12
Compatible Relations

Two relations are compatible (sometimes referred to as


union‐compatible) if they have the same degree and their
attributes match (they have the same domain and order).

• Certain binary operations (union, intersection, set


difference) are only possible on compatible relations.
• In some cases we can accept compatibility between
attributes of similar type and domain.
13
Union Operation
The union (∪) of two relations results in a new relation formed by the
union of all the tuples of the two relations.

• The extension of the new relation will be the union of the extension of the
two input relations.
• The input relations have to be compatible (have the same attributes).
• In the case of incompatible relations, we need to first apply a projection
and/or rename operation to obtain a compatible subset of attributes.
• If tuples with the same value exist in both input relations, they will only
appear once in the resulting relation.
14
Union Operation: Example
• Consider the relations corresponding professors and administrative staff of
a university:
Professors{DNI, name, lastName, subject}
Staff{DNI, name, lastName}

• To get a relation containing


Employees := Professors[DNI, name, lastName] ∪ Staff

• If a professor is also a member of staff, her data appears only once in the
result of the union.

15
Intersection Operation
The intersection (∩) of two relations results in a new relation formed
by all the tuples that have exactly the same values in both relations (for
all attributes).

• Obtains a subset of the tuples that is common between two relations.


• The input relations have to be compatible.
• In the case of incompatible relations, we have to first apply the
appropriate operations to make them compatible (rename or
projection).
• There won’t be any repeated tuples in the result.
16
Intersection Operation: Example

• Consider again the relations corresponding professors and


administrative staff of a university:
Professors{DNI, name, lastName, subject}
Staff{DNI, name, lastName}

• To list the professors that are also staff members, we apply a union:
ProffStaff := Professors[DNI, name, lastName] ∩ PAS

17
S T
Difference Operation

The diference ( ‐ ) of two relations T and S, results in a new relation


containing all the tuples in T that do not exist in S.

• The relations have to be compatible or be made so by applying the


necessary operations first.
• It is possible to obtain an empty relation
• Note T – S = T – (T ∩ S)

18
Operation difference
• Consider again the relations corresponding professors and
administrative staff of a university:
Professors{DNI, name, lastName, subject}
Staff{DNI, name, lastName}

• To get a relation containing teachers who are ONLY teachers (not


staff), we use:
ProfNotStaff:= Professors[DNI, name, lastName] - Staff

19
Cartesian Product Operation
The Cartesian Producte (x) of two relations T and S results in a new relation
with a schema that is the concatenation of the schemas of T and S.
The extension of the resulting relation will contain all possible
concatenations of every tuple in T with every tuple in S.
• The degree of the resulting relation will be the sum of the degrees of the original
relations.
• The cardinality of the resulting relation will be the product of the cardinalities of
the original relations
• Relations can be non compatible
• The relations cannot have common attribute names
• It does not provide significant information
20
Cartesian Product: Example
Hotel Hiker
ID Name Address DNI Name LastName
1 Hotel Solineu Avinguda Supermolina, 7 00000000T Joan Martí Roure
2 Alberg Abrigall Masella Avinguda Peu Pista P‐3, 2 11111111V Xavier Potera Serés

Hotel’{ID, HotelName, Address} := Hotel{ID, Name, Address};


CartesianProd := Hotel’ × Hiker;
ID HotelName Address DNI Name LastName
1 Hotel Solineu Carrer de Lepant, 416 00000000T Joan Martí Roure
1 Hotel Solineu Carrer de Lepant, 416 11111111V Xavier Potera Serés
2 Alberg Abrigall Masella Carrer de los Castillejos, 388 00000000T Joan Martí Roure
2 Alberg Abrigall Masella Carrer de los Castillejos, 388 11111111V Xavier Potera Serés

21
Theta‐Join Operation
The theta‐join (θ) of two relations results in a new relation has a schema
that is the concatenation of the schemas of the two relations, and an
extension that contains those combinations of the tuples of the original
relations that meet a specified condition.

• It basically selects the subset of tuples from the Cartesian product


that meet a specified condition
• The relations can be incompatible
• The relations cannot have attributes with the same names.
• The condition can be any inequality condition (=, ≠, <, ≤, >, ≥) on the
attributes of the two relations.
22
Theta‐Join Operation Sintax: R = R1 [condicions] R2
HOTEL OWNER
ID Name Owner Opening DNI Name LastName
1 Hotel Solineu 00000000T 2005 00000000T Joan Martí Roure
2 Alberg Abrigall Masella 11111111V 2003 11111111V Xavier Potera Serés
3 Hotel Super Molina 11111111V 2001 33333333M Pere Serafí Cases

We want to obtain the hotels owned by Xavier Potera Serés which opened after 2002
HOTEL’{ID, HotelName, Owner, Opening} := HOTEL{ID, Name, Owner, Opening}; Rename
R’ := Hotel’[Owner=DNI]OWNER; Theta‐Join
R:= R’(Opening≥ 2003 Selection
∧ Name= ‘Xavier’
∧ LastName= ‘Potera Serés’);
R
ID HotelName Owner Opening DNI Name LastName
2 Alberg Abrigall Masella 11111111V 2003 11111111V Xavier Potera Serés
Natural Join Operation
• Let equi‐join be a theta‐join in which all conditions are in the form of
an equality “=”.
• An equi‐join results in relations with differently named but identical
attributes, which are superfluous and have no real utility.
• A Natural Join (*) is an equi‐join with its superfluous attributes
removed.
• Natural joins compare attributes of the same name and when their
values agree, concatenate the tuples.
• We should rename the attributes that should be compared (so that
they have the same name) before applying a natural join

24
Natural join Sintax: R = R1 *R2
HOTEL OWNER
ID Name Owner Opening DNI Name LastName
1 Hotel Solineu 00000000T 2005 00000000T Joan Martí Roure
2 Alberg Abrigall Masella 11111111V 2003 11111111V Xavier Potera Serés
3 Hotel Super Molina 11111111V 2001 33333333M Pere Serafí Cases

We want to obtain the hotels owned by Xavier Potera Serés which opened after 2002

HOTEL’{ID, HotelName, DNI, Opening} := HOTEL{ID, Name, Owner, Opening}; Rename


R’ := HOTEL’ * OWNER; Natural join
R := R’(Opening≥ 2003 ∧ Name= ‘Xavier’ ∧ LastName= ‘Potera Serés’); Selection
R
ID HotelName Opening DNI Name LastName
2 Alberg Abrigall Masella 2003 11111111V Xavier Potera Serés
Outer Join Operations
• Their goal is to join two relations while preserving all the tuples from
one or both relations.
• A left outer join, T*LS, between two relations T and S preserves all
tuples from T.
• The attributes of S which do not coincide with T will have NULL values.
• A right outer join T*RS between T and S preserves all tuples from S.
• The attributes of T which do not coincide with S will have NULL values.
• A full outer join T*FS between T and S preserves all tuples of T and S.
• The attributes of T which do not coincide with S will have NULL values.
• The attributes of S which do not coincide with T will have NULL values.
26
Outer Joins: Exemples
Likes Dislikes
Student Likes Student Dislikes

Harry unicorns Harry Snape

Ron Quidditch Harry dementors


Hermione exams Ron spiders
Hermione cats Luna Daily prophet
Likes *F Dislikes
Likes *L Dislikes Student Like Dislike
Student Like Dislike Likes *R Dislikes Harry unicorns Snape
Harry unicorns Snape Student Like Dislike Harry unicorns dementors
Harry unicorns dementors Harry unicorns Snape Ron Quidditch spiders
Ron Quidditch spiders Harry unicorns dementors Hermione exams NULL
Hermione exams NULL Ron Quidditch spiders Hermione cats NULL
Hermione cats NULL Luna NULL Daily prophet Luna NULL Daily prophet
Sequence of Operations
• It is often necessary to perform various operations to get the desired
result.
• These operations have to follow an established order
• We have two options:
• Grouping all the operations in a single expression, using the necessary
parenthesis to assure the order
• Decomposing the expression into various steps and obtaining an intermediate
relation per step
• In the second case, the DBMS has to maintain the information on the
state of the system to store the intermediate values.
28
Sequence of Operations: Example
To obtain only the names and last names of all the employees in the
university (professors and administrative staff), we should first do a union of
the professors and the staff relations and then a projection on the name and
last name attributes
• Using a single expression:
R := (PROFESSORS ∪ STAFF)[name, lastName]
• In two steps:
EMPLOYEES:= PROFESSORS ∪ PAS;
R := EMPLOYEES [name, lastName]
29
Summary
• Relational algebra offers a set of operations for manipulating
relations.
• They can be classified as:
• Set operations: union, intersection, difference, and cartesian product
• Specifically relational operations: selection, projection, and joins
• The operations in relational algebra can form a sequence
that enable us to perform complex queries.

30

You might also like