Sei sulla pagina 1di 17

1

Algebra Relacional

Bases de Datos
Lic. Carlos Buckle
Utiliza contenido del libro Fundamentos de Bases de Datos (Silberschatz, Korth ,Sudarshan)
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
2
Aspectos del modelo Relacional
Aspecto estructural:
Los datos son percibidos por los usuarios como tablas.

Aspecto de integridad:
Dichas tablas satisfacen cietos requisitos de integridad. Por
ej. Integridad referencial, requisitos de dominio y unicidad
de claves.

Aspecto de manejo de datos:
Los operadores disponibles para manejar tablas generan
nuevas tablas como resultado.
2
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
3
Introduccin al Algebra Relacional
El principal componente para explotar el modelo
relacional es el lgebra relacional.

Un conjunto de Operaciones que toman tablas como
operadores/operandos y arrojan tablas como resultado

Propiedad de clausura - de relaciones, en relaciones
(Algebra)
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
4
Algebra Relacional
Seis operadores bsicos
Union:
Diferencia:
Producto cartesiano: x
Seleccin: o
Proyeccin: [
Renombrar:
Los operadores son binarios (toman dos relaciones como
entrada) o unarios (toman solo una relacin como
entrada).
3
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Operadores Bsicos
Union () Diferencia () Producto Cartesiano ()

Seleccin (o) Proyeccin (t)
5
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
6
Cuatro operadores especiales
Seleccin (o) Proyeccin (t)






Join Natural () Divisin()
a1
a2
a3
b1
b1
b2
b1
b2
b3
c1
c2
c3
a1
a2
a3
b1
b1
b2
c1
c1
c2
=>

a
b
c
a
a
a
b
c
x
y
z
x
y
x
z
a
4
Union Operation Example
Relations r, s:
r s:
7
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Union Operation
Notation: r s
Defined as:
r s = {t | t e r or t e s}
For r s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2
nd
column
of r deals with the same type of values as does the 2
nd

column of s)
Example: to find all courses taught in the Fall 2009 semester, or in the
Spring 2010 semester, or in both
[
course_id
(o
semester=Fall year=2009
(section))
[
course_id
(o
semester=Spring year=2010
(section))


8
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
5
Set difference of two relations
Relations r, s:
r s:
9
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Set Difference Operation
Notation r s
Defined as:
r s = {t | t e r and t e s}

Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Example: to find all courses taught in the Fall 2009 semester, but not in
the Spring 2010 semester
[
course_id
(o
semester=Fall year=2009
(section))
[
course_id
(o
semester=Spring year=2010
(section))




10
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
6
Cartesian-Product Operation Example
Relations r, s:
r x s:
11
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t e r and q e s}

Assume that attributes of r(R) and s(S) are
disjoint. (That is, R S = C).
If attributes of r(R) and s(S) are not disjoint,
then renaming must be used.
12
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
7
Select Operation Example
Relation
r
o
A=B ^ D > 5
(r)
13
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Select Operation
Notation: o
p
(r)
p is called the selection predicate
Defined as:

o
p
(r) = {t | t e r and p(t)}

Where p is a formula in propositional calculus consisting of terms connected
by : . (and), v (or), (not)

Each term is one of:
<attribute> op <attribute> or <constant>
where op is one of: =, =, >, >. <. s

Example of selection:

o
dept_name=Physics
(instructor)
14
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
8
Project Operation Example
Relation r:

[
A,C
(r)
15
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Project Operation
Notation:

where A
1
, A
2
are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained
by erasing the columns that are not listed
Duplicate rows removed from result, since relations are sets
Example: To eliminate the dept_name attribute of
instructor

[
ID, name, salary
(instructor)

) (
, ,
2
,
1
r
k
A A A
[
16
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
9
Rename Operation
Allows us to name, and therefore to refer to, the results of relational-
algebra expressions.
Allows us to refer to a relation by more than one name.
Example:

x
(E)

returns the expression E under the name X
If a relational-algebra expression E has arity n, then


returns the result of expression E under the name X, and with the
attributes renamed to A
1
, A
2
, ., A
n
.

) (
) ,...,
2
,
1
(
E
n
A A A x

17
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Example Query
Find the largest salary in the university
Step 1: find instructor salaries that are less than some other
instructor salary (i.e. not maximum)
using a copy of instructor under a new name d
[
instructor.salary
(o
instructor.salary < d,salary

(instructor x
d
(instructor)))
Step 2: Find the largest salary
[
salary
(instructor)
[
instructor.salary
(o
instructor.salary < d,salary

(instructor x
d
(instructor)))
18
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
10
Composition of Operations
Can build expressions using multiple operations
Example: o
A=C
(r x s)

r x s






o
A=C
(r x s)
19
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
20
Operadores extendidos
Interseccin ()
Theta Join (

)
Join Natural()
Divisin ()
11
Intersection
Notation: r s
Defined as:
r s = { t | t e r and t e s }
Assume:
r, s have the same arity
attributes of r and s are compatible
Note: r s = r (r s)
21
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Intersection Example
Relation r, s:






r s
22
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
12
Theta-Join
Notation: r

s
Equivalent to: r

s = o

(r x s)
Donde es una frmula
23
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Theta-Join - Example
Relations r, s:
r x s:
r

s donde es A=C

24
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
13
Notation: r s
Natural-Join Operation
Let r and s be relations on schemas R and S respectively.
Then, r s is a relation on schema R S obtained as follows:
Consider each pair of tuples t
r
from r and t
s
from s.
If t
r
and t
s
have the same value on each of the attributes in R S, add a
tuple t to the result, where
t has the same value as t
r
on r
t has the same value as t
s
on s
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as:
[
r.A, r.B, r.C, r.D, s.E
(o
r.B = s.B . r.D = s.D
(r x s))
25
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Natural Join Example
Relations r, s:
r s
26
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
14
Natural Join and Theta Join
Find the names of all instructors in the Comp. Sci. department together
with the course titles of all the courses that the instructors teach
[
name, title
(o
dept_name=Comp. Sci.
(instructor teaches course))
Natural join is associative
(instructor teaches) course is equivalent to
instructor (teaches course)
Natural join is commutative
instruct teaches is equivalent to
teaches instructor
The theta join operation r
u
s is defined as
r
u
s = o
u
(r x s)
27
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Assignment Operation
The assignment operation () provides a
convenient way to express complex queries.
Write query as a sequential program consisting
of
a series of assignments
followed by an expression whose value is
displayed as a result of the query.
Assignment must always be made to a
temporary relation variable.
28
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
15
Division
Given relations r(R) and s(S), such that S c R, r s is the largest
relation t(R-S) such that
t x s _ r
E.g. let r(ID, course_id) = [
ID, course_id
(takes ) and
s(course_id) = [
course_id
(o
dept_name=Biology
(course )
then r s gives us students who have taken all courses in the Biology
department
Can write r s as
temp1

[
R-S
(r )
temp2 [
R-S
((temp1 x s ) [
R-S,S
(r ))
result = temp1 temp2
The result to the right of the is assigned to the relation variable on
the left of the .
May use variable in subsequent expressions.
29
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
Algebra Relacional - Lic. Carlos Buckle - UNPSJB
30
Division Example
S#
S1
S2
S2
S1
S3
S1
P#
P1
P2
P1
P4
P1
P6
P#
P1
S#
S1
S2
r s rs
16
Aggregate Functions and Operations
Aggregation function takes a collection of values and returns a single
value as a result.
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in relational algebra


E is any relational-algebra expression
G
1
, G
2
, G
n
is a list of attributes on which to group (can be
empty)
Each F
i
is an aggregate function
Each A
i
is an attribute name
Note: Some books/articles use instead of (Calligraphic G)
) (
) ( , , ( ), ( , , ,
2 2 1 1 2 1
E
n n n
A F A F A F G G G
Aggregate Operation Example
Relation r:
A B
o
o
|
|
o
|
|
|
C
7
7
3
10

sum(c)
(r) sum(c )
27
17
Aggregate Operation Example
Find the average salary in each department

dept_name

avg(salary)
(instructor)

avg_salary
Aggregate Functions (Cont.)
Result of aggregation does not have a name
Can use rename operation to give it a name
For convenience, we permit renaming as part of
aggregate operation


dept_name

avg(salary) as avg_sal
(instructor)

Potrebbero piacerti anche