Vous êtes sur la page 1sur 33

Relational Algebra

Lesson Outcomes
2

By the end of the lesson students should


be able to:
Relate the use of relational algebra in
database manipulation
Identify the basic symbols used in
relational algebra
Express database queries in the form of
relational algebra expressions

What is Relational Algebra (RA) ?


3

A query language (QL) is a language that allows users to


manipulate and retrieve data from a database.
The relational model supports simple, powerful QLs (having strong
formal foundation based on logics, allow for much optimization)
Two (mathematical) query languages form the basis for real"
languages (e.g., SQL) and for implementation. One of them is
relational algebra
Relational Algebra is useful for representing query execution plans,
and query optimization techniques.
Relational Algebra comprises a set of basic operations.
An operation is the application of an operator to one or more
source (or input) relations to produce a new relation as a result.
Relational algebra has the property of closure, that is the output
from each of the operations is another relation.

5 Basic RA Operations
Projection ( ) Retains only wanted columns from
relation (vertical).
Selection
(
) Selects a subset of rows from
relation (horizontal).
Cross-product (x) Allows us to combine two
relations.
Set-difference () Tuples in R1, but not in R2.
Union ( ) Tuples in R1 and/or in R2.
NOTE: R1 = relation/table 1
R2 = relation/table 2

Example Instances
Boats

bid
101
102
103
104

S2

bname
Interlake
Interlake
Clipper
Marine

color
blue
red
green
red

Rents

sid bid
day
22 101 10/10/96
58 103 11/12/96

sid
28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0

Projection

Retains only attributes that are in the projection


list.
Examples:
(S2)

age

sname,rating(S2)

The 1st query will produce a relation with only one


column which is age. While the 2nd query produces
a relation with two columns: sname and rating. The
number of rows depends on the number of unique
content. e.g. 1st query, 2 rows, 2nd query, 4 rows
Projection operator eliminates duplicates rows

Projection
age(S2)

age
35.0
55.5

sname
yuppy
sname,rating(S 2) lubber
guppy
rusty

Eliminates
redundant
35.0

rating
9
8
5
10

Selection ()

Selects rows that satisfy selection condition. Can also have


multiple conditions
Result is a relation with same number of columns BUT probably
lesser number of rows.
Example:

rating >8(S2)
sid
28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0

Union and Set-Difference

All of these operations take two input


relations, which must be union-compatible:
Same number of fields.
`Corresponding fields have the same
type.
R1 U R2

R1 - R2

R2 - R1

Union
S1

sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
S2

sid
28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0

S1 U S2

sid sname rating age


22
31
58
44
28

dustin
lubber
rusty
guppy
yuppy

7
8
10
5
9

Eliminates duplicates

45.0
55.5
35.0
35.0
35.0

sid
22
31
58

Set
Difference

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0

sid sname rating age


22 dustin 7
45.0

S1 S2

S1

sid
28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0
S2

sid sname rating age


28 yuppy
9
35.0
44 guppy
5
35.0

S2 S1

Cross-Product
R1 x R2: Each row of R1 (m rows) paired with each row of
R2 (n rows).
Q: How many rows in the result?
mxn
Result schema has one field per field of R1 and R2, with
field names `inherited if possible.
May have a naming conflict: Both R1 and R2 have a field
with the same name.
In this case, can use the renaming operator (OPTIONAL):

X (E)
- rename expression E as x
(A1, A2, , An) (E) = rename attributes in E as A1, A2,..,An

Cross Product Example


sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0

sid bid
day
22 101 10/10/96
58 103 11/12/96
R2

R1
(sid) sname rating

R1 X R2 =

age

(sid)

bid

day

22

dustin

45.0

22

101

10/ 10/96

22

dustin

45.0

58

103

11/ 12/96

31

lubber

55.5

22

101

10/ 10/96

31

lubber

55.5

58

103

11/ 12/96

58

rusty

10

35.0

22

101

10/ 10/96

58

rusty

10

35.0

58

103

11/ 12/96

Compound Operator:
Intersection

In addition to the 5 basic operators, there are


several additional Compound Operators
These add no computational power to the
language, but are useful shorthands.
Can be expressed solely with the basic
operations.

Intersection takes two input relations, which


must be union-compatible.
Q: How to express it using basic operators?

R S = R (R S)

Intersection
sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
S1

sid
28
31
44
58

sname rating age


yuppy
9
35.0
lubber
8
55.5
guppy
5
35.0
rusty
10 35.0
S2

sid sname rating age


31 lubber 8
55.5
58 rusty
10
35.0
S1 n S2

Compound Operator: Join

Joins are compound operators involving cross


product, selection, and (sometimes) projection.
Most common type of join is a natural join (often
just called join). R
S conceptually is:

Compute R X S
Select rows where attributes that appear in both relations have equal
values and domains
Project all unique atttributes and one copy of each of the common ones.

Note: Usually done much more efficiently than this.


Useful for putting normalized relations back
together.

Natural Join Example


sid bid
day
22 101 10/10/96
58 103 11/12/96
R1

S1

R1 =
sid
22
58

sid
22
31
58

sname rating age


dustin
7
45.0
lubber
8
55.5
rusty
10 35.0
S1

sname rating age bid day


dustin 7
45.0 101 10/10/96
rusty
10
35.0 103 11/12/96

Theta Join Example


sid bid
22
58

sid sname rating age

day

22
31
58

101 10/10/96
103 11/12/96

dustin
lubber
rusty

R1

7
8
10

45.0
55.5
35.0

S1

S1><

R1 =
S1.sid < R1.sid

(sid)

sname rating age

22
31

dustin
lubber

7
8

(sid)

45.0 58
55.5 58

bid

day

103 11/12/96
103 11/12/96

Outer Join
An extension of the join operation that avoids
loss of information.
Computes the join and then adds tuples form
one relation that does not match tuples in the
other relation to the result of the join.
Uses null values:
null signifies that the value is unknown or does
not exist

Outer Join Example


Relation loan
loan-number

branch-name

amount

L-170
L-230
L-260

Downtown
Redwood
Perryridge

3000
4000
1700

Relation borrower
customer-name loan-number
Jones
Smith
Hayes

L-170
L-230
L-155

Left Outer Join


Join
loan

Borrower

loan-number branch-name amount customer-name


L-170
L-230

Downtown
Redwood

3000
4000

Jones
Smith

Left Outer Join

loan

Borrower

loan-number branch-name amount customer-name


L-170
L-230
L-260

Downtown
Redwood
Perryridge

3000
4000
1700

Jones
Smith
null

Right Outer Join, Full Outer Join


Right Outer Join

loan

borrower

loan-number branch-name amount customer-name


L-170
L-230
L-155

Downtown
Redwood
null

3000
4000
null

Jones
Smith
Hayes

Full Outer Join


loan

borrower

loan-number branch-name amount customer-name


L-170
L-230
L-260
L-155

Downtown
Redwood
Perryridge
null

3000
4000
1700
null

Jones
Smith
null
Hayes

Compound Operator:
Division

Division is useful for expressing for all


queries like:
Find sids of
sailors who have reserved all boats.
For A/B attributes of B are subset of attributes
of A.
May need to project to make this happen.
E.g., let A have 2 fields, x and y; B have only
field y:
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


(AB)
sno

pno

s1

p1

s1

p2

s1

p3

s1

p4

s2

p1

s2

p2

s3

p2

s4

p2

s4

p4

pno
p2

B1

pno
p2
p4

B2

pno
p1
p2
p4

B3

sno
s1
s2
s3
s4

sno
s1
s4

sno
s1

A/B1

A/B2

A/B3

Another Division Example

Relations r, s:

B
a
a
a
a
a
a
a
a

D
a
a
b
a
b
a
b
b

E
1
1
1
1
3
1
1
1

A B C
r s:

a
a

D E
a 1
b 1
s

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
G1, G2, , Gn F1( A1), F2( A2),, Fn( An) (E)
E is any relational-algebra expression
G1, G2 , Gn is a list of attributes on which to group (can be
empty)
Each Fi is an aggregate function
Each Ai is an attribute name

Aggregate Operation Example

A B C

Relation r:

7
7
3
10

sum(c) (r)

sum-C
27

Aggregate Operation Example


Relation account grouped by branch-name:

branch-name account-number
Perryridge
Perryridge
Brighton
Brighton
Redwood
branch-name

balance

A-102
A-201
A-217
A-215
A-222

400
900
750
750
700

sum(balance) (account)

branch-name

balance

Perryridge
Brighton
Redwood

1300
1500
700

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.
Example: Write r s as
temp1 R-S (r)
temp2 R-S ((temp1 x s) R-S,S (r))
result = temp1 temp2

References
32

www.cs.kent.edu/~yuri/cs43005.s2008/lectures/lect
ure2.ppt
inst.eecs.berkeley.edu/~cs186/sp06/lecs/lecture8Al
g.ppt
and others

Exercises
33

Given the following relational schema:


STUDENT (sid, sname)
COURSE (ccode,cname, ccredithr, lno)
LECTURER (lno, lname)
ENROLMENT(sid,ccode)
Write the relational algebra expression to extract
the following:
The name of all courses offered
The name of 3 credit hours courses

Exercises
34

The name of courses along with the


name of the lecturers who teach them
The name of courses and the name of
lecturers
The name of lecturers who teach 3 cr
hrs courses as well as 4 cr hrs courses
The name of lecturers who teach 3 cr
hrs courses but not 4 cr hrs courses

Exercises
35

The name of students who take SCB1033 and


SCB1043.
The name of students who take all courses
taught by Dr Samuel.
The maximum number of credit hours offered
The number of students enrolled in each
course
The number of credit hrs taken by a student
with id 1121.

Vous aimerez peut-être aussi