Upload
ngohuong
View
214
Download
1
Embed Size (px)
Citation preview
© Prof. Dr.-Ing. Wolfgang Lehner |
INTELLIGENT DATABASE GROUP
Relational Data Model 1
Foundations of Information Systems
| 2
>
© A. Behrend Foundations of Information Systems |
What is in the Lecture?
1. Database Usage Query Programming Design
| 4
>
© A. Behrend Foundations of Information Systems |
The Relational Model
The Relation Model Developed by Codd (IBM) in 1970 Considered ingenious but impractical in
1970 Conceptually simple Computers lacked power to implement
the relational model Today, microcomputers can run
sophisticated relational database software
[Codd, E.F. (1970). "A Relational Model of Data for Large Shared Data Banks". Communications of the ACM 13 (6): 377–387.]
| 5
>
© A. Behrend Foundations of Information Systems |
Relational Model – Foundations
Foundations Domains: Integer, String[20], Date, … Relation R is defined on a relational schema RS Relational Schema RS: Set of attributes {A1,…,Ak} Attribute Aj: Domain Dj = dom(Aj) Relation: Subset of the cartesian product of the domains
R ⊆ D1 × D2 × ... × Dk, k ≥ 1
Example Domain D1 = {a,b,c}, D2 = {0,1} Cartesian product D1 × D2 = { (a,0), (a,1), (b,0), (b,1), (c,0), (c,1) } Possible relations R1= { (a,0), (b,0), (c,0), (c,1) } or R2 = ∅
| 6
>
© A. Behrend Foundations of Information Systems |
Relational Model – Foundations (2)
Foundations (cont‘d) Tuple: Element of a relation Cardinality of a relation: number of tuples in a relation Degree of a relation R ⊆ D1 × D2 × ... × Dk: k ≥ 1
Representation of a relation as table Attribut names A and B are column names Order of rows (tuples) and columns (attributes) within a table is important
R1 A B
a 0
b 0
c 0
c 1
| 7
>
© A. Behrend Foundations of Information Systems |
Relational Model – Foundations (3)
Example Relational schema {Name, Inhabitants, State} with dom(Name) = String[40],
dom(Inhabitants) = INTEGER and dom(Land) = String[40]}
Instances t1[Name] = Munich, t1[Inhabitants] = 101617 and t1[State] = Bavaria t2[Name] = Dresden, t2[Inhabitants] = 476668 and t2[Germany] = Saxony
In general Database schema = Set of relation schemas Database = Set of relations
Cities Name Inhabitants State Munich 1.353.186 Bavaria Dresden 523.058 Saxony
| 8
>
© A. Behrend Foundations of Information Systems |
Primary Key Concept
R PNr Salary
1 1700
2 2172
4 3189
| 11
>
© A. Behrend Foundations of Information Systems |
Algebra
Mathematical system consisting of Operands: variables or values from which new values can be constructed Operators: symbols denoting procedures that construct new values from given
values Relational Algebra An algebra whose operands are relations or variables that represent relations Operators are designed to do the most common things that we need to do with
relations in a database The result is an algebra that can be used as a query language for relations
Core Selection: picking certain rows Projection: picking certain columns Renaming of relations and attributes Products and joins: compositions of relations Union, intersection, and difference Usual set operations, but both operands must have the same relation schema
| 12
>
© A. Behrend Foundations of Information Systems |
Selection
R1 := σC (R2) C is a condition (as in “if”) that refers to attributes of R2 R1 is all those tuples of R2 that satisfy C
Example (relation Sells)
JoeMenu := σbar=“Joe’s”(Sells) bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
| 13
>
© A. Behrend Foundations of Information Systems |
Projection
R1 := πL (R2) L is a list of attributes from the schema of R2 R1 is constructed by looking at each tuple of R2, extracting the attributes on list L, in
the order specified, and creating from those components a tuple for R1 Eliminate duplicate tuples, if any
Example (relation Sells)
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
Prices := πbeer,price(Sells)
beer price
Bud 2.50
Miller 2.75
Miller 3.00
| 14
>
© A. Behrend Foundations of Information Systems |
Renaming
R1 := ρR1(A1,…,An)(R2) The ρ operator gives a new schema to a relation Makes R1 be a relation with attributes A1,…,An and the same tuples as R2 Simplified notation: R1(A1,…,An) := R2
R(bar, addr) := Bars name addr
Joe’s Maple St.
Sue’s River Rd
bar addr
Joe’s Maple St.
Sue’s River Rd
Bars R
| 15
>
© A. Behrend Foundations of Information Systems |
Extended Projection
For operator πL, we allow L to contain arbitrary expressions Arithmetic on attributes, e.g. A+B C Duplicate occurrences of the same attribute
Example
R2:=πA+BC,AA1, AA2 (R1) A B
1 2
3 4
C A1 A2
3 1 1
7 3 3
R1 R2
| 16
>
© A. Behrend Foundations of Information Systems |
Cartesian Product
R3 := R1 × R2
Pair each tuple t1 of R1 with each tuple t2 of R2 Concatenation t1t2 is a tuple of R3 Schema of R3 are the attributes of R1 and R2 (in order) To differentiate attribute A of the same name in R1 and R2 R1.A and R2.A
Example
A B
1 2
3 4
B C
2 6
4 8
9 19
A R1.B R2.B C
1 2 2 6
1 2 4 8
1 2 9 19
3 4 2 6
3 4 4 8
3 4 9 19
R1
R2
R3
R3 := R1 × R2
| 17
>
© A. Behrend Foundations of Information Systems |
Natural Join
R3 := R1 ⋈ R2
Connects two relations Equating attributes of the same name, and Projecting out one copy of each pair of equated attributes
Example
name beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
name addr
Joe’s Maple St.
Sue’s River Rd.
name beer price addr
Joe’s Bud 2.50 Maple St.
Joe’s Miller 2.75 Maple St.
Sue’s Bud 2.50 River Rd.
Sue’s Miller 3.00 River Rd.
BarInfo := Sells ⋈ Bars
| 18
>
© A. Behrend Foundations of Information Systems |
Theta-Join
R3 := R1 ⋈C R2
Equivalent to Take the product R1 × R2 Then apply σC to the result
C can be any boolean-valued condition, A θ B, where θ is =, <, >, … Example
bar beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
name addr
Joe’s Maple St.
Sue’s River Rd.
bar beer price name addr
Joe’s Bud 2.50 Joe’s Maple St.
Joe’s Miller 2.75 Joe’s Maple St.
Sue’s Bud 2.50 Sue’s River Rd.
Sue’s Miller 3.00 Sue’s River Rd.
BarInfo := Sells ⋈Sells.bar = Bars.name Bars
| 19
>
© A. Behrend Foundations of Information Systems |
Set Operations
Union R∪ S R and S have the same relational schema (except renaming) Attribute domains have to be compatible
Difference R/S (or R–S) R and S have the same relational schema (except renaming) Attribute domains have to be compatible
Derived Operations Intersection R∩S R∩S = R – (R – S)
Division R÷S The result of R÷S consists of the attributes that are in R but not S and for which it
holds that all combinations with tuples in S are present in R R÷S := πR-S(R) – πR-S((πR-S(R)×S) – R) or: πR(S×(R÷S)) is subset of R
| 20
>
© A. Behrend Foundations of Information Systems |
Example: Division Bars÷S
name beer price
Joe’s Bud 2.50
Joe’s Miller 2.75
Sue’s Bud 2.50
Sue’s Miller 3.00
Bars
beer price
Bud 2.50
Miller 2.75
S
name
Joe’s
Bars÷S
| 21
>
© A. Behrend Foundations of Information Systems |
Building Complex Expressions
Combine operators with parentheses and precedence rules Three notations: Sequences of assignment statements, expressions with several
operators or expression trees
Precedence of relational operators [σ, π, ρ] (highest) [×, ⋈] ∩ [∪, —]
Example R3 := R1 ⋈C R2 can be written as R3 := σC (R1×R2)
| 22
>
© A. Behrend Foundations of Information Systems |
Expression Trees
Expression Trees Leaves are operands Interior nodes are operators, applied to their child or children
Example Using the relations Bars(name, addr) and Sells(bar, beer, price) Find the names of all the bars that are either on Maple St. or sell Bud for less than $3
Bars
Sells
σaddr = “Maple St.”
σprice<3 AND beer=“Bud”
πname ρR(name)
πbar
∪
| 23
>
© A. Behrend Foundations of Information Systems |
Expression Trees (2)
Example (Self-Join) Using Sells(bar, beer, price), find the bars that sell two different beers at the same
price Strategy By renaming, define a copy of Sells, called S(bar, beer1, price1) The natural join of Sells and S consists of tuple (bar, beer, beer1, price, price1)
such that the bar sells both beers at this price
Sells
Sells ρS(bar, beer1, price1)
⋈
πbar
σbeer != beer1 and price=price1
| 24
>
© A. Behrend Foundations of Information Systems |
Result Schemas
Union, intersection, and difference: the schemas of the two operands must be the same, so use that schema for the result
Selection: schema of the result is the same as the schema of the operand
Projection: list of attributes tells us the schema
Product: schema is the attributes of both relations Use R.A, etc., to distinguish two attributes named A
Theta-join: same as product
Natural join: union of the attributes of the two relations
Renaming: the operator tells the schema