Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
CHAPTER 2CHAPTER 2RELATIONAL MODELRELATIONAL MODEL
Chapter 2: Relational Modelp
Structure of Relational DatabasesFundamental Relational Algebra OperationsAdditional Relational Algebra OperationsAdditional Relational Algebra OperationsExtended Relational-Algebra-OperationsNull ValuesModification of the Database
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 2
Basic Structure
Formally,given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn
R ⊆ D1, X ….. X D (n: degree of R)R ⊆ D1, X ….. X Dn (n: degree of R)or
R = { <d1, ..., dn> | d1∈ D1, ..., dn∈ Dn } (set of tuples)
Example: customer-name = {Jones, Smith, Curry, Lindsay}
customer-street = {Main, North, Park}
customer-city = {Harrison, Rye, Pittsfield}y { , y , }
Then r = { (Jones, Main, Harrison), (Smith, North, Rye),
(Curry North Rye) (Lindsay Park Pittsfield)}(Curry, North, Rye), (Lindsay, Park, Pittsfield)}
is a relation over customer-name x customer-street x customer-city
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 3
Attribute Typesyp
Each attribute of a relation has a nameThe set of allowed values for each attribute is called the domainof the attributeof the attributeAttribute values are (normally) required to be atomic, that is, indivisibleindivisible
E.g. multivalued attribute values are not atomicE g composite attribute values are not atomicE.g. composite attribute values are not atomic
The special value null is a member of every domainTh ll l li i i h d fi i i fThe null value causes complications in the definition of many operations
dwe shall ignore the effect of null values in our main presentation and consider their effect later
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 4
Relation Schema
A1, A2, …, An are attributes1 2 n
R = (A1, A2, …, An ) is a relation schemaE g Customer schema = (customer name customer street customer city)E.g. Customer-schema = (customer-name, customer-street, customer-city)
r(R) is a relation (variable) on the relation schema RE.g. customer(Customer-schema)
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 5
Relational Algebrag
Algebra : operators and operandsRelational algebra
operands : relationsb i (+ ddi i l i )operators : basic operators (+ additional operations)
take two or more relations as inputs and give a new relation as a result.
Procedural languageProcedural language6 Fundamental Operators
selectselectprojectunionset differenceCartesian productrename
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 6
Example Queriesp Q
Find all loans of over $1200σamount >1200 (loan)
Find the loan number for each loan of an amount greater than $1200$1200
∏loan-number (σamount>1200 (loan))
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 7
Example Queriesp Q
Find the names of all customers who have a loan, an account, or both, from the bank
∏customer-name (borrower) ∪ ∏customer-name (depositor)
Find the names of all customers who have a loan and an account at bank.
∏customer-name (borrower) ∩ ∏customer-name (depositor)customer name customer name
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 8
Example Queriesp Q
Find the names of all customers who have a loan at the Perryridge branch.
∏customer-name (σbranch-name=“Perryridge”
(σb l b = l l b (borrower x loan)))(σborrower.loan-number = loan.loan-number(borrower x loan)))
Find the names of all customers who have a loan at the Perryridge branch but do notFind the names of all customers who have a loan at the Perryridge branch but do not have an account at any branch of the bank.
∏ t (σb h “P id ”∏customer-name (σbranch-name = “Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))
– ∏customer-name(depositor)
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 9
Example Queriesp Q
Find the names of all customers who have a loan at the Perryridge branch.
Query 1∏ (∏customer-name(σbranch-name = “Perryridge”
(σborrower.loan-number = loan.loan-number(borrower x loan)))
Query 2∏ (∏customer-name(
(σloan.loan-number = borrower.loan-number(( σbranch name = “Perryridge”(loan) ) x borrower( branch-name = Perryridge ( ) ) x w
) ))
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 10
Example Queriesp Q
Find the largest account balancegRename account relation as dThe query is:
∏balance(account) –∏balance( )∏account.balance (
σ ( t x ρ ( t) )σaccount.balance < d.balance (account x ρd (account) ))
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 11
Formal Definition
A basic expression in the relational algebra consists of either one of the following:
A relation in the databaseA lA constant relation
Let E1 and E2 be relational-algebra expressions; the following are ll l ti l l b p iall relational-algebra expressions:
E1 ∪ E2E1 – E2E1 x E2σp (E1), P is a predicate on attributes in E1∏s(E1), S is a list consisting of some of the attributes in E1ρN (E1), N is the new name for the result of E1
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 12
Additional Operationsp
We define additional operations that do not add any power to thep y prelational algebra, but that simplify common queries.
Set intersection
Natural joinNatural joinDivisionA iAssignment
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 13
Division Operationp
r ÷ s
Suited to queries that include the phrase “for all”Suited to queries that include the phrase for all .Let r and s be relations on schemas R and S respectively where
R = (A1, …, Am, B1, …, Bn)S = (B1, …, Bn)
Th l f i l i hThe result of r ÷ s is a relation on schemaR – S = (A1, …, Am)
r ÷ s = { t | t ∈ ∏ R-S(r) ∧ ∀ u ∈ s ( tu ∈ r ) }
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 14
Division Operation – Examplep p
Relations r s: r ÷ s:
ABA B
Relations r, s: r ÷ s:
AB
α1
2
αα
12
β2αβγ
311
sγδδδ
1134δ
∈∈β
4612β 2
r
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 15
Division Operation – Examplep p
Relations r s: r ÷ s:
A B C D E D E A B C
Relations r, s: r ÷ s:
A B
α a
C D
α a
E
11
D
ab
E
11
A B
α a
C
γααβ
aaa
γγγ
aba
111
b 1 γ a γs
βγγ
aaa
γγγ
bab
311γ
γ aγβ b 1
r
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 16
Division Operationp
Property Let q = r ÷ sThen q is the largest relation satisfying q × s ⊆ r
Definition in terms of the basic algebra operationLet r(R) and s(S) be relations, and let S ⊆ R
r ÷ s = ∏R-S(r) –∏R-S ( (∏R-S (r) × s) – ∏R-S,S(r))
To see why∏R-S,S(r) simply reorders attributes of r
∏ (∏ (r) × s) ∏ (r)) gives those tuples t in ∏ (r)∏R-S(∏R-S (r) × s) – ∏R-S,S(r)) gives those tuples t in ∏R-S (r)such that for some tuple u∈s, tu ∉ r.
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 17
Example Queries
Find all customers who have an account from at least the
p Q
“Downtown” and the “Uptown” branches.Query 1
∏customer-name(σbranch-name=“Downtown”(depositor account)) ∩
∏ (σ (depositor account))∏customer-name(σbranch-name=“Uptown”(depositor account))
Query 2
∏customer-name, branch-name (depositor account)÷ ρtemp(branch-name) ({(“Downtown”), (“Uptown”)})
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 18
Example Queriesp Q
Find all customers who have an account at all branches located inFind all customers who have an account at all branches located in Brooklyn city.
∏customer-name, branch-name (depositor account)customer name, branch name ( p )÷ ∏branch-name (σbranch-city = “Brooklyn” (branch))
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 19
Extended Relational-Algebra Operationsg p
adds power and convenience to the relational algebrap g
Generalized ProjectionGeneralized ProjectionAggregate FunctionsOuter Join
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 20
Generalized Projectionj
Extends the projection operation by allowing arithmetic p j p y gfunctions to be used in the projection list.
∏F1, F2, …, Fn(E), , ,
E is any relational-algebra expressionEach of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E.
Find how much more each person can spendcredit-info(customer-name, limit, credit-balance)f ( )
∏customer-name, limit – credit-balance (credit-info)
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 21
Aggregate Functions and Operations
Aggregation function
gg g p
gg gtakes a collection of values and returns a single value as a result.avg, min, max, sum, count
Aggregate operation in relational algebra
g (E)G1, G2, …, Gn g F1(A1), F2(A2), …, Fm(Am) (E)
E is any relational-algebra expressionG1, G2…, Gn is a list of attributes on which to group (can be empty)F i f i dFi is an aggregate function, and Ai is an attribute name, for i=1, …, m
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 22
Aggregate Operation – Examplegg g p p
Relation r:A B
α α
C
7ααβ
αββ
7
7
3ββ
ββ3
10
sum Cg sum(c) (r)sum-C
27
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 23
Aggregate Operation – Example
Relation account
gg g p p
branch-name account-number balancen h n o n n n
PerryridgePerryridge
A-102A-201
400900
BrightonBrightonRedwood
A-217A-215A-222
750750700
g (account) branch-name balancebranch-name g sum(balance) (account)
PerryridgeBrightonRedwood
13001500700
Result of aggregation does not have a nameC i i i ( i SQL)
Redwood 700
Can use rename operation to give it a name (as in SQL)
branch-name g sum(balance) as sum-balance (account)
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 24
branch-name g sum(balance) as sum-balance ( )
Outer JoinJ
An extension of the join operation that avoids loss of j pinformation
Compute the join add tuples that do not have matching tuples in the other relation
fill in undefined attribute values with null
Three typesleft outer joinright outer joinfull outer join
Conventional join is called inner join
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 25
Outer Join – ExampleJ p
loan-number amountbranch-nameRelation loan o n n o n
L-170L-230L 260
300040001700
DowntownRedwoodP idL-260 1700Perryridge
Relation borrowercustomer-name loan-number
JonesS i h
L-170L 230Smith
HayesL-230L-155
Inner join: loan BorrowerInner join: loan Borrower
loan-number amount customer-namebranch-name
L-170L-230
30004000
JonesSmith
DowntownRedwood
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 26
Outer Join – Example
Left outer join:
J p
loan-number amount customer-namebranch-nameloan borrower
o n n o n
L-170L-230L 260
300040001700
o n
JonesSmithll
n h n
DowntownRedwoodP idL-260 1700 nullPerryridge
Right outer join :loan borrower
loan-number amount
L-170L 230
30004000
customer-name
JonesSmith
branch-name
DowntownRedwoodL-230
L-1554000null
SmithHayes
Redwoodnull
Full outer join :loan borrower
loan-number amount
L-170 3000
customer-name
Jones
branch-name
Downtownloan borrower L 170L-230L-260L 155
300040001700null
JonesSmithnullHayes
DowntownRedwoodPerryridgenull
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 27
L-155 null Hayesnull
Null Values
null: an unknown or non-existing valuegThe result of any arithmetic expression involving null is nullAggregate functionsAggregate functions
simply ignore null values (SQL semantics)Is an arbitrary decision Could have returned null as result insteadIs an arbitrary decision. Could have returned null as result instead.
For duplicate elimination and groupingll i d lik h l d ll d b hnull is treated like any other value, and two nulls are assumed to be the
same (SQL semantics)Alternative: assume each null is different from each otherAlternative: assume each null is different from each otherBoth are arbitrary decisions
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 28
Null Values
Comparisons with null values return the special truth value unknownWhy do we need unknown?If false was used instead of unknown, thennot (A < 5) would not be equivalent to A >= 5not (A < 5) would not be equivalent to A > 5
Three-valued logic using the truth value unknown:OR: (unknown or true) = true,
(unknown or false) = unknown(unknown or unknown) = unknown
AND: (true and unknown) = unknown, ( )(false and unknown) = false,(unknown and unknown) = unknown
NOT: (not unknown) = unknown( )In SQL, “P is unknown” evaluates to true if predicate P evaluates to unknown
Result of select predicate is treated as false if it evaluates to unknown
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 29
Modification of the Database
The content of the database may be modified using the y gfollowing operations:
DeletionInsertionUpdating
All these operations are expressed using the assignment operatorAll these operations are expressed using the assignment operator.
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 30
Deletion
A delete request is expressed similarly to a query, except instead q p y q y pof displaying tuples to the user, the selected tuples are removed from the database.Deletion operation in relational algebra :
r ← r Er ← r – Ewhere r is a relation and E is a relational algebra query.
You can only delete whole tuples;cannot delete values on only particular attributes=> should use update operation
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 31
Deletion Examples
Delete all account records in the Perryridge branch.
p
account ← account – σ branch-name = “Perryridge” (account)Delete all loan records with amount in the range of 0 to 50
loan ← loan – σ amount ≥ 0 and amount ≤ 50 (loan)Delete all accounts at branches located in Needham.
r1 ← σ branch-city = “Needham” (account branch)
r ← ∏b h b b l (r )r2 ← ∏branch-name, account-number, balance (r1)
r3 ← ∏ customer-name, account-number (r2 depositor)
t ← taccount ← account – r2
depositor ← depositor – r3
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 32
Insertion
To insert data into a relation, we either:specify a tuple to be insertedwrite a query whose result is a set of tuples to be inserted
Insertion operation in relational algebra:r ← r ∪ Er ← r ∪ E
where r is a relation and E is a relational algebra expression.
The insertion of a single tuple is expressed by letting E be a constant relation containing one tuple.
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 33
Insertion Examplesp
Insert information in the database specifying that Smith has p y g$1200 in account A-973 at the Perryridge branch.
account ← account ∪ {(“Perryridge”, A-973, 1200)}account ← account ∪ {( Perryridge , A 973, 1200)}depositor ← depositor ∪ {(“Smith”, A-973)}
Provide as a gift for all loan customers in the Perryridge branch, a $200 savings account. Let the loan number serve as the account number for the new savings account.
r1 ← (σbranch-name = “Perryridge” (borrower loan))account ← account ∪ ∏branch-name account-number 200 (r1)branch-name, account-number,200 ( 1)depositor ← depositor ∪ ∏customer-name, loan-number,(r1)
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 34
Updatingp g
A mechanism to change a value in a tuple without changing allg p g gvalues in the tupleUse the generalized projection operator to do this taskUse the generalized projection operator to do this task
r ← ∏ F1, F2, …, FI, (r)
Each Fi, is eitherthe ith attribute of r: the attribute is not updated, or, an expression, involving only constants and the attributes of r, which gives h l f h ib h ib i d dthe new value for the attribute: the attribute is updated
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 35
Update Examplesp p
Make interest payments by increasing all balances by 5 percent.p y y g y paccount ← ∏ account#, branch-name, balance*1.05 (account)
Pay all accounts with balances over $10,000 6 percent interest d ll h 5and pay all others 5 percent
account ←∏ account#, branch-name, balance*1.06 (σ balance > 10000 (account) ) ∪, ,
∏account#, branch-name, balance*1.05 (σbalance ≤ 10000 (account) )
Original Slides:© Silberschatz, Korth and Sudarshan
Advanced DB (2008-1)Copyright © 2006 - 2008 by S.-g. Lee Silberschatz Chap 2 - 36
END OF CHAPTER 2END OF CHAPTER 2