T4 Relational Algebra
T4 Relational Algebra
Databases
Goals
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
5
Rename operation
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” (∨).
9
Projection Operation
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:
• Selection:
• Projection:
R := Professors[name, lastName]
12
Compatible 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}
• 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).
• To list the professors that are also staff members, we apply a union:
ProffStaff := Professors[DNI, name, lastName] ∩ PAS
17
S T
Difference Operation
18
Operation difference
• Consider again the relations corresponding professors and
administrative staff of a university:
Professors{DNI, name, lastName, subject}
Staff{DNI, name, lastName}
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
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.
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
30