DATABASE DESIGN I - 1DL300 Spring 2011 An introductory course on database systems 2011-02-03 1 Manivasakan Sabesan- UDBL - IT - UU http://www.it.uu.se/edu/course/homepage/dbastekn/vt11/ Manivasakan Sabesan Uppsala Database Laboratory Department of Information Technology, Uppsala University, Uppsala, Sweden
32
Embed
DATABASE DESIGN I · DATABASE DESIGN I -1DL300 Spring 2011 An introductory course on database systems ... Department of Information Technology, Uppsala University, Uppsala, Sweden.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
– Formal languages form underlying basis of query languages that people use.
Relational algebra
• Relational algebra is a procedural language
• Operations in relational algebra takes two or more relations as arguments and return a new relation.
• Relational algebraic operations:– Operations from set theory:
2011-02-03 4Manivasakan Sabesan- UDBL - IT - UU
– Operations from set theory: • Union, Intersection, Difference, Cartesian product
– Operations specifically introduced for the relational data model:• Select, Project, Join
• It have been shown that the select, project, union, difference, andCartesian product operations form a complete set. That is any other relational algebra operation can be expressed in these.
Operations from set theory
• Relations are required to be union compatible to be able to take part in the union, intersection and difference operations.
• Two relations R1 and R2 is said to be union-compatible if:
i.e. if they have the same degree and the same domains.
Union operation
• The union of two union-compatible relations R and S is the set of all tuples that either occur in R, S, or in both.
• Notation: R ∪ S
• Defined as: R ∪ S = {t | t ∈ R or t ∈ S}
• For example:
2011-02-03 6Manivasakan Sabesan- UDBL - IT - UU
• For example:
R S
A B
aab
121
U A B
ab
23
= A B
aabb
1213
Difference operation
• The difference between two union-compatible sets R and S is the set of all tuples that occur in R but not in S.
• Notation: R − S
• Defined as: R − S = {t | t ∈ R and t ∉ S}
• For example:
2011-02-03 7Manivasakan Sabesan- UDBL - IT - UU
• For example:
R S
A B
aab
121
− A B
ab
23
= A B
ab
11
Intersection
• The intersection of two union-compatible sets R and S, is the set of all tuples that occur in both R and S.
• Notation: R∩S
• Defined as: R ∩ S = {t | t ∈ R and t ∈ S}
• For example:
2011-02-03 8Manivasakan Sabesan- UDBL - IT - UU
• For example:
R S
A B
aab
121
∩ A B
ab
23
= A B
a 2
Cartesian product
• Let R and S be relations with k1 and k2 arities resp. The Cartesian product of R and S is the set of all possible k1+k2 tuples where the first k1 components constitute a tuple in R and the last k2 components a tuple in S.
• Notation: R × S
• Defined as: R × S = {t q | t ∈ R and q ∈ S}• Assume that attributes of r(R) and s(S) are disjoint. (i.e. R ) S = �). If attributes
of r(R) and s(S) are not disjoint, then renaming must be used.
2011-02-03 9Manivasakan Sabesan- UDBL - IT - UU
of r(R) and s(S) are not disjoint, then renaming must be used.
X =
Cartesian product example
A B
ab
12
× C D
abbc
5565
= A B
aaaa
1111
C D
abbc
5565
2011-02-03 10Manivasakan Sabesan- UDBL - IT - UU
c 5 abbbb
12222
cabbc
55565
Selection operation
• The selection operator, σ, selects a specific set of tuples from a relation according to a selection condition (or selection predicate) P.
• Notation: σ P(R)
• Defined as: σ P(R) = {t | t ∈ R and P(t) } (i.e. the set of tuples t in R that fulfills the condition P)
• Where P is a logical expression(*) consisting of terms connected by:
2011-02-03 11Manivasakan Sabesan- UDBL - IT - UU
• Where P is a logical expression(*) consisting of terms connected by: ∧ (and), ∨ (or), ¬ (not) and each term is one of: <attribute> op <attribute> or <constant>, where op is one of: =,≠, >, ≥, <, ≤
Example:σSALARY>30000(EMPLOYEE)
(*) a formula in propositional calculus
Selection example
= A B
aabb
abbb
C D
1524
7739
R
2011-02-03 12Manivasakan Sabesan- UDBL - IT - UU
b b 4 9
σA=B ∧ D > 5 (R) = A B
ab
ab
C D
14
79
Projection operation
• The projection operator, Π, picks out (or projects) listed columns from a relation and creates a new relation consisting of these columns.
• Notation: Π A1,A2,...,Ak(R), where A1, A2 are attribute names and
R is a relation name.• The result is a new relation of k columns.
2011-02-03 13Manivasakan Sabesan- UDBL - IT - UU
• The result is a new relation of k columns.
• Duplicate rows removed from result, since relations are sets.
Example: Π LNAME,FNAME,SALARY(EMPLOYEE)
Projection example
= A B
aabb
1234
C
1112
R
2011-02-03 14Manivasakan Sabesan- UDBL - IT - UU
b 4 2
Π A,C (R) = A C
aabb
1112
= A C
abb
112
Join operator
• The join operator, ⊗ (almost, correct ), creates a new relation by joining related tuples from two relations.
• Notation: R ⊗ C SC is the join condition which has the form Ar θ As , where θ is one of {=, <, >, ≤, ≥, ≠}. Several terms can be connected as C1
2011-02-03 15Manivasakan Sabesan- UDBL - IT - UU
one of {=, <, >, ≤, ≥, ≠}. Several terms can be connected as C1∧C2 ∧...Ck.
• A join operation with this kind of general join condition is called “Theta join”.
Example Theta join
θA≤F = A B
11
22
C D
33
27
A B
16
27
C
38
D E
27
33
F
45
R θA≤F S
E
33
F
45
R S
2011-02-03 16Manivasakan Sabesan- UDBL - IT - UU
1169
2277
3388
7777
69
77
88
77
38
59
3888
5999
Equijoin
• The same as join but it is required that attribute Ar and attribute Asshould have the same value.
• Notation: R ⊗C SC is the join condition which has the form Ar = As. Several terms can be connected as C ∧C ∧...C .
2011-02-03 17Manivasakan Sabesan- UDBL - IT - UU
be connected as C1 ∧C2 ∧...Ck.
Example Equijoin
⊗B=C = A B
aa
24
C D
24
dd
A B
aa
24
C D
24
dd
E
ee
R ⊗B=C S
E
ee
R S
2011-02-03 18Manivasakan Sabesan- UDBL - IT - UU
a 4 4 da 4 49
dd
ee
e
Natural join
• Natural join is equivalent with the application of join to R and S with the equality condition Ar = As (i.e. an equijoin) and then removing the redundant column As in the result.
• Notation: R *Ar,As SA ,A are attribute pairs that should fulfill the join condition which
2011-02-03 19Manivasakan Sabesan- UDBL - IT - UU
Ar,As are attribute pairs that should fulfill the join condition which has the form Ar = As. Several terms can be connected as C1 ∧C2∧...Ck.
Example Natural join
⊗B=C = A B
aa
24
D
dd
A B
aa
24
C D
24
dd
E
ee
R ∗B=C S
E
ee
R S
2011-02-03 20Manivasakan Sabesan- UDBL - IT - UU
a 4 da 4 49
dd
ee
e
Composition of operations
• Expressions can be built by composing multiple operations
• Example: σA=C (R × S)
A B
ab
12
× C D
ab
55
= A B
aa
11
C D
ab
55
R × S =
2011-02-03 21Manivasakan Sabesan- UDBL - IT - UU
σA=C (R × S) =
b 2 bbc
565
aaabbbb
1112222
bbcabbc
5655565
A B
abb
122
C D
abb
556
Assignment operation
• The assignment operation (← ) makes it possible to assign the result of an expression to a temporary relation variable.
• Example:
temp ← σdno = 5 (EMPLOYEE)
2011-02-03 22Manivasakan Sabesan- UDBL - IT - UU
dno = 5
result ← ∏fname,lname,salary (temp)
• The result to the right of the ← is assigned to the relation variable on the left of the ←.
• The variable may be used in subsequent expressions.
Renaming relations and attribute
• The assignment operation can also be used to rename relations and attributes.