Vous êtes sur la page 1sur 37

Relational Algebra

Operations and Set operations


Relational Algebra

π
By relieving the brain of all unnecessary
work, a good notation sets it free to
concentrate on more advanced
problems, and, in effect,
increases the mental power of the
race.

-- Alfred North Whitehead (1861 -


1947)
What is Relational Algebra
• It defines basic set of operations for
manipulating relational data.
• It enables users for basic retrieval
operations
• Result of any retrieval always gives
another relation to manipulate
• It specifies relations to be performed on
relations to derive result relations.
Relational algebra : Features
• It is a procedural query language.

• It consists of a collection of operators, such as


join, union, and intersect,that take relations as
their operands and return relations as their result.
• It is the formal description of how a relational
database operates.

• Relational algebra is the mathematics which


underpin SQL operations.
• Operators in relational algebra are not necessarily
the same as SQL operators, even if they have the
same name.
The BIG picture

Its place in the big picture:

Declarative
Declarative
query
query Algebra
Algebra Implementation
Implementation
language
language

SQL, Relational algebra


relational calculus
RELATIONAL ALGEBRA
OPERATORS

Set Specific
Operators Operators
Set Operators: Introduction
Relational algebra received little attention until
the publication of E.F. Codd’s relational model
of data in 1970. Codd proposed such an algebra
as a basis for database query languages.
E.F. Codd’s Operators
Set operators from mathematical set theory

Union
Intersection
Difference
Cartesian Product
Set Operators
Relation is a set of tuples, so set operations
should apply: ∩, ∪, − (set difference)
Result of combining two relations with a set
operator is a relation
All its elements must be tuples having same
structure
Hence, scope of set operations limited to union
compatible relations.
Union Compatible Relations
Two relations are union compatible if
Both have same degree (number of columns)
Names of attributes are the same in both
Attributes with the same name in both
relations have the same domain (i.e.the data
type of the attributes should be same like
character,integer etc. )
Union compatible relations can be combined
using union,
union intersection,
intersection and set difference.
Union
• Returns a relation consisting of all tuples
appearing in either or both of two specified
relations.
• Duplicates are eliminated
• Both relations must be union compatible
• It is denoted by symbol ∪.
Example
A B
S# SNAME STATUS CITY S# SNAME STATUS CITY
Smith 20 London S1 Smith 20 London
S1
S4 Clark 20 London S2 Jones 20 Paris

AUB
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Clark 20 London
S2 Jones 20 Paris
Intersection
• Returns a relation consisting of all tuples
appearing in both of two specified relations.
• It is denoted by symbol ∩ .
• Both relations must be union compatible.
Duplicates are eliminated
Example
A
S# SNAME STATUS CITY

S1 Smith 20 London

S4 Clark 20 London

B
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris

A INTERSECTION B (A ∩ B)
S# SNAME STATUS CITY
S1 Smith 20 London
Difference
Returns a relation consisting of all tuples
appearing in the first and not the second of two
specified relations.It is denoted by symbol − .
Example
A
S# SNAME STATUS CITY

S1 Smith 20 London

S4 Clark 20 London

B
S# SNAME STATUS CITY
S1 Smith 20 London
S4 Jones 20 Paris

A DIFFERENCE B (A - B)
S# SNAME STATUS CITY
S4 Clark 20 London
Cartesian Product
Cross product or cross join
Result relation will have degree= sum of degrees of
both source relations
Result relation will have tuples= product of tuples of
both source relations
Returns a relation consisting of all possible tuples
that are a combination of two tuples, one from each
of two specified relations.It is denoted by the
symbol X.
R and S need not be union compatible
Example
a x ax
b * y = ay
c bx
(A)(B) b y
cx
cy
(A X B)
Cross-Product : detailed example
• Each row of R is paired with each row of S.
• Result schema has one field per field of S and R, with
field names `inherited’ if possible.

R R C R O S S S
A 1 A 1 A 1 F 4 A 1
B 2 A 1 C 2 F 4 C 2
D 3 A 1 D 3 F 4 D 3
F 4 A 1 E 4 F 4 E 4
E 5 B 2 A 1 E 5 A 1
S B 2 C 2 E 5 C 2
B 2 D 3 E 5 D 3
A 1 B 2 E 4 E 5 E 4
C 2 D 3 A 1
D 3 D 3 C 2
E 4 D 3 D 3
D 3 E 4
• Union and Intersection are cumulative
and associative operations therefore
AUB=BUA
A∩ B=B∩ A
AU(BUC)=(AUB)UC
A ∩ ( B ∩ C) = ( A ∩ B ) ∩ C

– But difference operation is not


therefore
A–B ≠B-A
Operators Developed Specifically
For Relational Databases

Project

Select

Join
Project
Retains only wanted columns from relation.
Projection operator has to eliminate duplicates
Denoted by symbol π.
Produces table containing subset of columns of
argument table
π (relation)
attribute list
Example
sid sname rating age
28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S
2

πsname , rating (S2)


=
Select
Selects rows that satisfy specific conditions.
No duplicates in result
Result relation can be the input for another
relational algebra operation (Operator
composition.)
Denoted by symbol σ
Produce table containing subset of rows of
argument table satisfying condition
σ condition (relation)
Operators: <, ≤ , ≥ , >, =, ≠

For multiple conditions use : and ( ), or (V)


^
Example

sid sname rating age


28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
S2

σ rating >8(S2) sid sname rating age


28 yuppy 9 35.0
58 rusty 10 35.0
Rename
• Used as alias name for result relation
• Often used to assign simple name to the result
relation or attributes when it is to be used again in
some other expression.
• Symbol used is ←
• example:
(B400)←π (bookid,bname)σ (price>400)(books)
• Now B400 can be used again
• Example : π (bookid)(B400)
Join
Joins are compound operators involving cross
product, selection, and (sometimes) projection.
Most common type of join is a “natural join” R
S conceptually is:
Compute R X S
Project all unique attributes and one copy of
each of the common ones.
Example

sid sname rating age


sid bid day
22 dustin 7 45.0
22 101 10/10/96
58 103 11/12/96 31 lubber 8 55.5
58 rusty 10 35.0
R1
S1
R1 S1 =
sid sname rating age bid day
22 dustin 7 45.0 101 10/ 10/ 96
58 rusty 10 35.0 103 11/ 12/ 96
Division

• Not supported as a primitive operator, but


useful for certain

• Let A have 2 fields, x and y; B have only field


y:
– A/B =
– i.e., A/B contains all x tuples such that for every y
tuple in B, there is an xy tuple in A.
Examples of Division A/B
sno pno pno pno pno
s1 p1 p2 p2 p1
s1 p2 p4 p2
s1 p3 B1 p4
s1 p4
B2
s2 p1 sno B3
s2 p2 s1
s3 p2 s2 sno
s4 p2 s3 s1 sno
s4 p4 s4 s4 s1

A A/B1 A/B2 A/B3


Practical Examples
Example R1 sid bid day
Instances 22 101 10/10/96
58 103 11/12/96
• “Sailors” and “Reserves”
S1 sid sname rating age
relations for our
22 dustin 7 45.0
examples.
31 lubber 8 55.5
58 rusty 10 35.0

S2 sid sname rating age


28 yuppy 9 35.0
31 lubber 8 55.5
44 guppy 5 35.0
58 rusty 10 35.0
Find names of sailors who’ve
reserved boat #103
• Solution 1: π sname((σ bid = 103 Reserves)  Sailors)

❖ Solution 2: ρ (Temp1, σ Re serves)


bid = 103

ρ ( Temp2, Temp1 
Sailors)
π sname (Temp2)

❖ Solution 3: π sname (σ bid =103 (Re serves 


Sailors))
Find names of sailors who’ve
reserved a red boat
• Information about boat color only available
in Boats; so need an extra join:
π sname ((σ Boats ) 
Re serves 
Sailors )
color =' red '

❖ A more efficient solution:


π sname (π ((π σ Boats ) 
Re s) 
Sailors )
sid bid color =' red '

☛ A query optimizer can find this given the first solution!


Find sailors who’ve reserved a
red or a green boat
• Can identify all red or green boats, then
find sailors who’ve reserved one of
these boats: , (σ
ρ (Tempboats Boats ))
color =' red ' ∨ color =' green '
π sname(Tempboats 
Re serves 
Sailors )

❖ Can also define Tempboats using union! (How?)


What happens if ∨ is replaced by∧ in this query
Find sailors who’ve reserved a
red and a green boat
• Previous approach won’t work! Must
identify sailors who’ve reserved red boats,
sailors who’ve reserved green boats, then
find the intersection (note that sid is a key
ρ (Tempred ,π ((σ
for Sailors): Boats) 
Re serves))
sid color =' red '
ρ (Tempgreen, π ((σ Boats) Re serves))
sid color =' green'

π sname((Tempred ∩ Tempgreen ) 
Sailors )
Find the names of sailors who’ve
reserved all boats
• Uses division; schemas of the input
relations to / must be carefully chosen:
ρ (Tempsids, (π Re serves) / (π Boats))
sid, bid bid
π sname (Tempsids 
Sailors )

To find sailors who’ve reserved all ‘Interlake’ boats:


..... /π (σ Boats)
bid bname =' Interlake'

Vous aimerez peut-être aussi