Академический Документы
Профессиональный Документы
Культура Документы
36
ER
schema
Relational
Database
Relational Algebra
Modification of the Database
Overview
ECS-165A WQ11
37
ECS-165A WQ11
38
StudI
d
412
307
412
StudNam
e
Smith
Jones
Smith
Major
CS
CSE
CSE
relation schema
tuple
relation
has
ECS-165A WQ11
39
ECS-165A WQ11
40
ECS-165A WQ11
41
ECS-165A WQ11
42
Example translation:
DocId
Title
Publisher
Year
BOOKS
Date
borrows
describes
DESCRIPTIONS
Keyword
StId
has
written
AUTHORS
STUDENTS
AName Address
According to step 1:
ECS-165A WQ11
43
relation
for
entity
type
ECS-165A WQ11
44
Procedural language
Queries in relational algebra are applied to relation
instances, result of a query is again a relation
instance
Six basic operators in relational algebra:
select
selects a subset of tuples
ECS-165A WQ11
45
from reln
deletes unwanted columns
project
from reln
Cartesian
allows to combine two
Product
relations
tuples in reln. 1, but not in
Set-difference
reln. 2
tuples in reln 1 plus tuples in
Union
reln 2
renames attribute(s) and
Rename
relation
The operators take one or two relations as input and
give a new relation as a result (relational algebra is
closed).
Select Operation
Notation: P(r)
Defined as
ECS-165A WQ11
46
1
2
3
7
1
0
Project Operation
ECS-165A WQ11
47
rA,C(r)
A
B
1
0
2
0
3
0
4
0
2
2
4
ECS-165A WQ11
48
Notation: r s
Defined as r s := {tq | t r and q s}
applied first.
0
2A
1
Example: relations r,s:
0
1
+ rs
0
1 C +D
E
0
2 1
0
0
1 1
0
0
ECS-165A WQ11
49
rs
A
1
2
1
3
Union Operator
Notation: r s
Defined as r s := {t | t r or t s}
For r s to be applicable,
1. r,s must have the same number of attributes
2. Attribute domains must be compatible (e.g., 3rd
column of r has a data type matching the data
type of the 3rd column of s)
ECS-165A WQ11
50
2
3
rs
A
Notation: r s
Defined as r s := {t | t r and t 6
s}
For r s to be applicable,
1. r and s must have the same arity
2. Attribute domains must be compatible
ECS-165A WQ11
51
2
3
rs
A
1
1
ECS-165A WQ11
52
Rename Operation
x(A1,A2,...,Ak)(E)
returns the expression E under the name x, and
with the attribute names A1,A2,...,Ak.
Composition of Operations
ECS-165A WQ11
53
Example: A=C(r s)
rs
A
B
1
1
1
1
2
2
2
2
D
10
10
20
10
10
10
20
10
E
+
+
+
+
1
0
1
0
2
0
A=C(r s)
Example Queries
Assume the following relations:
BOOKS(DocId, Title, Publisher, Year)
STUDENTS(StId, StName, Major, Age)
AUTHORS(AName, Address)
ECS-165A WQ11
54
ECS-165A WQ11
55
ECS-165A WQ11
56
ECS-165A WQ11
57
StName(STUDENTS.StId=borrows.StId
(Major=CS(STUDENTS) borrows))
2. List the title of books written by the author
Silberschatz.
Title(AName=Silberschatz
(has-written.DocId=BOOKS.DocID(has-writtenBOOKS)))
or
Title(has-written.DocId=BOOKS.DocID
(AName=Silberschatz(has-written) BOOKS))
3. As 2., but not books that have the keyword database.
. . . as for 2. . . .
Title(describes.DocId=BOOKS.DocId
(Keyword=database(describes) BOOKS))
4. Find the name of the youngest student.
StName(STUDENTS)
S1.StName(S1.Age>S2.Age(S1(STUDENTS)S2(STUDENT
S)))
ECS-165A WQ11
58
Title(BOOKS)
B1.Title(B1.Year>B2.Year(B1(BOOKS)
B2(BOOKS)))
Additional Operators
These
operators
do
not
add
any
power
(expressiveness) to the relational algebra but simplify
common (often complex and lengthy) queries.
Set-Intersection
Natural Join
Condition
Division
Assignment
Set-Intersection
Notation: r s
Defined as r s := {t | t r and t s}
For r s to be applicable,
1. r and s must have the same arity
2. Attribute domains must be compatible
Derivation: r s = r (r s)
Example: given the relations r and s
Dept. of Computer ScienceUC Davis
ECS-165A WQ11
59
rsr s
A
Natural Join
1
2
1
Notation: r 1 s
Let r,s be relations on schemas R and S,
respectively. The result is a relation on schema R
S. The result tuples are obtained by considering
each pair of tuples tr r and ts s.
If tr and ts have the same value for each of the
attributes in R S (same name attributes), a
tuple t is added to the result such that
t has the same value as tr on r
t has the same value as ts on s
ECS-165A WQ11
60
r.A,r.B,r.C,r.D,s.E(r.B=s.Br.D=s.D(r s))
Example: given the relations r and s
r
s
A
B
1
2
4
1
2
D
a
a
b
a
b
B
1
1
1
1
2
D
a
a
a
a
b
B
1
3
1
2
3
D
a
a
a
b
b
r1s
A
Condition Join
Notation: r 1C s
C is a condition on attributes in R S, result
schema is the same as that of Cartesian Product. If
R S 6= and condition C refers to these
attributes, some of these attributes must be
renamed.
Sometimes also called Theta Join (r 1 s).
ECS-165A WQ11
61
Derivation: r 1C s = C(r s)
Note that C is a condition on attributes from both r
and s
Example: given two relations r,s
rs
A
1
4
7
r 1B<D s
2
5
8
3
6
1
2
1 2 3 3
1
1 2 3 6
2
4 5 6 6
2
If C involves only the comparison operator =, the
condition join is also called Equi-Join.
Example 2:
rs
ECS-165A WQ11
62
4 5
6
7 8
10
r 1C=SC (S(SC,D)(s))
8
12
A
S
C
Division
Notation: r s
Precondition: attributes in S must be a subset of
attributes in R, i.e., S R. Let r,s be relations on
schemas R and
S, respectively, where
R(A1,...,Am,B1,...,Bn)
S(B1,...,Bn)
The result of r s is a relation on schema
R S = (A1,...,Am)
ECS-165A WQ11
63
r s := {t | t RS(r) u s: tu
r}
Example: given the relations r,s:
rs
A
a
a
a
a
a
a
a
a
a
a
C
s
Assignment
a
b
1
1
ECS-165A WQ11
64
Example Queries
1. List each book with its keywords.
BOOKS 1 Descriptions
Note that books having no keyword are not in the
result.
AName=Ullman(has-written))
ECS-165A WQ11
65
AName(StName=Smith(has-written
STUDENTS))
both
keywords
(borrows
database
BOOKS 1 (DocId(Keyword=database(Descriptions ))
DocId(Keyword=programming(Descriptions)))
or
BOOKS
1
(programming)})
(Descriptions{(database),
ECS-165A WQ11
66
STUDENTS{(1024,
Clark,
Delete
ECS-165A WQ11
67