5/21/2018 DBMS - Ch2
1/96
Chapter 2: Relational Model
5/21/2018 DBMS - Ch2
2/96
Chapter 2: Relational Model Structure of Relational Databases
Fundamental Relational-Algebra-Operations
Additional Relational-Algebra-Operations
Etended Relational-Algebra-Operations
!ull "alues Modi#cation of the Database
5/21/2018 DBMS - Ch2
3/96
Eample of a Relation
5/21/2018 DBMS - Ch2
4/96
Attribute $%pes Each attribute of a relation has a name
$he set of allo&ed 'alues for each attribute is called the domainof the attribute
Attribute 'alues are (normall%) re*uired to be atomic+ that is,indi'isible
Eg the 'alue of an attribute can be an account number,but cannot be a set of account numbers
Domain is said to be atomic if all its members are atomic
$he special 'alue null is a member of e'er% domain
$he null 'alue causes complications in the de#nition of man%operations
.e shall ignore the e/ect of null 'alues in our mainpresentation and consider their e/ect later
5/21/2018 DBMS - Ch2
5/96
Relation Schema Formall%, gi'en domains D0, D2, 1 Dna relationris a subset of
D0 D2 1 Dn
$hus, a relation is a set of n-tuples (a0,a2, 1, an) &here each ai
Di
Schema of a relation consists of
attribute de#nitions
name
t%pedomain
integrit% constraints
5/21/2018 DBMS - Ch2
6/96
Relation 3nstance $he current 'alues (relation instance) of a relation are speci#ed
b% a table
An element tof ris a tuple, represented b% a row in a table
Order of tuples is irrele'ant (tuples ma% be stored in anarbitrar% order)
JonesSmithCurryLindsay
customer_name
MainNorthNorthPark
customer_street
HarrisonRyeRyePittsfield
customer_city
customer
attributes
(or columns)
tuples(or rows)
5/21/2018 DBMS - Ch2
7/96
Database A database consists of multiple relations
3nformation about an enterprise is bro4en up into parts, &itheach relation storing one part of the information
Eg
account : information about accounts
depositor : &hich customer o&ns &hich account
customer : information about customers
5/21/2018 DBMS - Ch2
8/96
$he customer Relation
5/21/2018 DBMS - Ch2
9/96
$he depositor Relation
5/21/2018 DBMS - Ch2
10/96
.h% Split 3nformation AcrossRelations5
Storing all information as a single relation such as
bank(account_number, balance, customer_name, )
results in
repetition of information
eg,if t&o customers o&n an account (.hat gets repeated5)
the need for null 'alues
eg, to represent a customer &ithout an account
!ormali6ation theor% (Chapter 7) deals &ith ho& to designrelational schemas
5/21/2018 DBMS - Ch2
11/96
8e%s 9et 8 R
K is a superkey of Rif 'alues for Kare sucient to identif% a uni*uetuple of each possible relation r(R)
b% ;possibler < &e mean a relation rthat could eist in theenterprise &e are modeling
Eample: =customer_name, customer_street> and
=customer_name>
are both super4e%s of Customer, if no t&o customers can possibl%
ha'e the same name
3n real life, an attribute such as customer_id&ould be usedinstead of customer_name to uni*uel% identif% customers, but
&e omit it to 4eep our eamples small, and instead assume
customer names are uni*ue
5/21/2018 DBMS - Ch2
12/96
8e%s (Cont)
Kis a candidate keyif Kis minimal
Eample: =customer_name> is a candidate 4e% for Customer,
since it is a super4e% and no subset of it is a super4e%
Primary key: a candidate 4e% chosen as the principal means ofidentif%ing tuples &ithin a relation
Should choose an attribute &hose 'alue ne'er, or 'er% rarel%,
changes
Eg email address is uni*ue, but ma% change
5/21/2018 DBMS - Ch2
13/96
Foreign 8e%s A relation schema ma% ha'e an attribute that corresponds to the
primar% 4e% of another relation $he attribute is called a foreignkey
Eg customer_nameand account_numberattributes of depositorare foreign 4e%s to customerand accountrespecti'el%
Onl% 'alues occurring in the primar% 4e% attribute of the
referenced relationma% occur in the foreign 4e% attribute ofthe referencing relation.
5/21/2018 DBMS - Ch2
14/96
Schema Diagram
5/21/2018 DBMS - Ch2
15/96
?uer% 9anguages 9anguage in &hich user re*uests information from the database
Categories of languages
@rocedural
!on-procedural, or declarati'e
;@ure< languages:
Relational algebra
$uple relational calculus
Domain relational calculus
@ure languages form underl%ing basis of *uer% languages thatpeople use
5/21/2018 DBMS - Ch2
16/96
Relational Algebra @rocedural language
Si basic operators
select:
proect:
union: set di/erence:
Cartesian product:
rename:
$he operators ta4e one or t&o relations as inputs and producea ne& relation as a result
5/21/2018 DBMS - Ch2
17/96
Select Operation B Eample Relation r
A B C D
1
5
12
23
7
7
3
10
A=B D > 5(r)A B C D
1
23
7
10
5/21/2018 DBMS - Ch2
18/96
@roect Operation B Eample Relationr:
A B C
10
20
30
40
1
1
1
2
A C
1
1
1
2
=
A C
1
1
2
A,C(r)
5/21/2018 DBMS - Ch2
19/96
nion Operation B Eample Relations r, s:
rs:
A B
1
2
1
A B
2
3
rs
A B
1
2
1
3
5/21/2018 DBMS - Ch2
20/96
BEample
Relations r, s:
r s:
A B
1
2
1
A B
2
3
r
s
A B
11
ar es an ro uc pera on
5/21/2018 DBMS - Ch2
21/96
ar es an- ro uc pera on BEample
Relationsr, s:
rxs:
A B
1
2
A B
111
12222
C D
1010
20
101010
2010
E
aab
baabb
C D
1010
2010
E
aabbr
s
5/21/2018 DBMS - Ch2
22/96
Rename Operation Allo&s us to name, and therefore to refer to, the results of
relational-algebra epressions
Allo&s us to refer to a relation b% more than one name
Eample:
x(E)
returns the epression Eunder the nameX
3f a relational-algebra epression Ehas arit% n, then
returns the result of epression Eunder the nameX, and &ith the
attributes renamed to! , " , #$, n
)(),...,,( 21E
nAAAx
5/21/2018 DBMS - Ch2
23/96
Composition of Operations Can build epressions using multiple operations
Eample: ADC(r x s)
r x s
ADC(r x s)
A B
111
12222
C D
1010
20
101010
2010
E
aab
baabb
A B C D E
122
1010
20
aab
5/21/2018 DBMS - Ch2
24/96
an4ing Eamplebranc% (branc%_name, branc%_cit&, assets)
customer (customer_name, customer_street,customer_cit&)
account (account_number, branc%_name, balance)
loan (loan_number, branc%_name, amount)
depositor (customer_name, account_number)
borrower(customer_name, loan_number)
5/21/2018 DBMS - Ch2
25/96
Eample ?ueries
Find all loans of o'er 02GG
Find the loan number for each loan of an amount greater than$1200
amount> 1200(loan)
loan_number(amount> 1200(loan))
Find the names of all customers who have a loan, an account, or both,from the bank
customer_name
(borrower)customer_name
(depositor)
5/21/2018 DBMS - Ch2
26/96
Eample ?ueries Find the names of all customers &ho ha'e a loan at the @err%ridge
branch
Find the names of all customers who have a loan at the
Perryridge branch but do not have an account at any branch ofthe bank.
customer_name(branch_name = Perryridge
(borrower.loan_number = loan.loan_number(borrower x loan)))
customer_name(depositor)
customer_name(branch_name=Perryridge
(borrower.loan_number = loan.loan_number(borrower x loan)))
5/21/2018 DBMS - Ch2
27/96
Eample ?ueries Find the names of all customers &ho ha'e a loan at the @err%ridge
branch
customer_name(loan.loan_number = borrower.loan_number(
(branch_name = Perryridge(loan)) x borrower))
customer_name(branch_name = Perryridge(
borrower.loan_number = loan.loan_number(borrower x loan)))
5/21/2018 DBMS - Ch2
28/96
Additional Operations
Additional Operations
Set intersection
!atural oin
Aggregation
Outer Hoin
Di'ision
All abo'e, other than aggregation, can be epressed using basic
operations &e ha'e seen earlier
5/21/2018 DBMS - Ch2
29/96
- BEample
Relation r, s:
rs
A B
121
A B
23
r s
A B
2
5/21/2018 DBMS - Ch2
30/96
!atural Hoin Operation BEample
Relations r, s:
A B
124
12
C D
aab
ab
B
131
23
D
aaa
bb
E
r
A B
1111
2
C D
aaaab
E
s
r s
5/21/2018 DBMS - Ch2
31/96
Notation: r s
!atural-Hoin Operation
9et rand sbe relations on schemas Rand 'respecti'el%$hen, r s is a relation on schema R 'obtained as follo&s:
Consider each pair of tuples trfrom rand tsfrom s
3f trand tsha'e the same 'alue on each of the attributes in R
', add a tuple t to the result, &here thas the same 'alue as tron r
thas the same 'alue as tson s
Eample:
R (, , C, D)
' (E, , D)
Result schema (, , C, D, E)
r sis de#ned as:
r$, r$(, r$C, r$D, s$E(r$( ) s$( r$D ) s$D(r s))
5/21/2018 DBMS - Ch2
32/96
an4 Eample ?ueries Find the largest account balance
Strateg%:
Find those balances that are not the largest
Rename account relation as d so that &e can compare eachaccount balance &ith all others
se set di/erence to #nd those account balances that &ere notfound in the earlier step
$he *uer% is:
balance(account)-account.balance
(account.balance < d.balance(accountxd(account)))
Aggregate Funct ons an
5/21/2018 DBMS - Ch2
33/96
Aggregate Funct ons anOperations
Aggregation functionta4es a collection of 'alues and returns a
single 'alue as a resultavg: a'erage 'aluemin: minimum 'aluemax: maimum 'aluesum: sum of 'aluescount: number of 'alues
Aggregate operationin relational algebra
Eis an% relational-algebra epression
*!, *"1, *nis a list of attributes on &hich to group (can be empt%) Each +iis an aggregate function
Eachiis an attribute name
)()(,,(),(,,, 221121E
nnn AFAFAFGGG
5/21/2018 DBMS - Ch2
34/96
Aggregate Operation BEample
Relation r:
A B
C
7
7
3
10
sum(c) (r) sum(c)
27
?uestion: .hich aggregate operations cannot beepressed using basic relational operations5
5/21/2018 DBMS - Ch2
35/96
Aggregate Operation BEample
Relation accountgrouped b% branc%-name:
branch_name sum(balance)(account)
branch_nameaccount_number balance
PerryridgePerryridgeBrighton
BrightonRedwood
A-102A-201A-217
A-215A-222
400900750
750700
branch_name sum(balance)
PerryridgeBrightonRedwood
13001500700
5/21/2018 DBMS - Ch2
36/96
Aggregate Functions (Cont) Result of aggregation does not ha'e a name
Can use rename operation to gi'e it a name
For con'enience, &e permit renaming as part of aggregateoperation
branch_name sum(balance)assum_balance(account)
5/21/2018 DBMS - Ch2
37/96
Outer Hoin An etension of the oin operation that a'oids loss of information
Computes the oin and then adds tuples form one relation thatdoes not match tuples in the other relation to the result of the
oin
ses null'alues:
null signi#es that the 'alue is un4no&n or does not eist All comparisons in'ol'ing nullare (roughl% spea4ing) falseb%
de#nition
.e shall stud% precise meaning of comparisons &ith nullslater
5/21/2018 DBMS - Ch2
38/96
Outer Hoin B Eample Relation loan
Relationborrower
customer_nameloan_number
Jones
SmithHayes
L-170
L-230L-155
300040001700
loan_number amount
L-170L-230L-260
branch_name
DowntownRedwoodPerryridge
5/21/2018 DBMS - Ch2
39/96
Outer Hoin B Eample Hoin
loan borrower
loan_number amount
L-170L-230
30004000
customer_name
JonesSmith
branch_name
DowntownRedwood
JonesSmithnull
loan_number amount
L-170L-230L-260
300040001700
customer_namebranch_name
DowntownRedwoodPerryridge
Left Outer Join
loan borrower
5/21/2018 DBMS - Ch2
40/96
Outer Hoin B Eample
loan_number amount
L-170L-230L-155
30004000null
customer_name
JonesSmithHayes
branch_name
DowntownRedwoodnull
loan_number amount
L-170L-230L-260L-155
300040001700null
customer_name
JonesSmithnullHayes
branch_name
DowntownRedwoodPerryridgenull
Full Outer Join
loan borrower
Right Outer Join
loan borrower
Question: can outerjoins be expressed using basic relational
algebra operations
5/21/2018 DBMS - Ch2
41/96
!ull "alues 3t is possible for tuples to ha'e a null 'alue, denoted b% null,
for some of their attributes
nullsigni#es an un4no&n 'alue or that a 'alue does not eist
$he result of an% arithmetic epression in'ol'ing nullis null$
Aggregate functions simpl% ignore null 'alues (as in S?9)
For duplicate elimination and grouping, null is treated li4e an%
other 'alue, and t&o nulls are assumed to be the same (as in
S?9)
5/21/2018 DBMS - Ch2
42/96
!ull "alues Comparisons &ith null 'alues return the special truth 'alue: unknown
3f .alse&as used instead of unknown, then not ( / 0)&ould not be e*ui'alent to 1 0
$hree-'alued logic using the truth 'alue unknown:
OR: (unknownortrue) true,(unknownor.alse) unknown
(unknown orunknown) unknown
A!D: (trueand unknown) unknown,(.alseand unknown) .alse,
(unknown andunknown) unknown
!O$: (notunknown) unknown
3n S?9 ;2is unknown
5/21/2018 DBMS - Ch2
43/96
Di'ision Operation
!otation:
Suited to *ueries that include the phrase ;for all
5/21/2018 DBMS - Ch2
44/96
Di'ision Operation B Eample Relationsr, s:
rs: A
B
1
2
A B
1231113461
2r
s
5/21/2018 DBMS - Ch2
45/96
Another Di'ision Eample
A B
aaaa
aaaa
C D
aaba
babb
E
1111
3111
Relationsr, s:
rs:
D
ab
E
11
A B
aa
C
r
s
5/21/2018 DBMS - Ch2
46/96
Di'ision Operation (Cont) @ropert%
9et 3 r s
$hen 3is the largest relation satisf%ing 3 s r
De#nition in terms of the basic algebra operation
9et r(R)and s(')be relations, and let ' R
rs R-'(r ) B R-'( ( R-'(r ) s ) B R-','(r ))
$o see &h%
R-',' (r) simpl% reorders attributes of r
R-' (R-'(r ) s ) B R-','(r) ) gi'es those tuples t in
R-'(r ) such that for some tuple u s, tu r
an4 Eample ?ueries
5/21/2018 DBMS - Ch2
47/96
an4 Eample ?ueries
Find the name of all customers &ho ha'e a loan at the ban4 andthe loan amount
Find the names of all customers who have a loan and an account atbank.
customer_name(borrower)customer_name(depositor)
customer_name, loan_number, amount(borrower loan)
an4 Eample ?ueries
5/21/2018 DBMS - Ch2
48/96
Query 1
customer_name(branch_name= Downtown(depositor account))
customer_name(branch_name= Uptown(depositor account))
Query 2
customer_name, branch_name(depositor account)
temp(branch_name)({(Downtown),(Uptown)})
Note that Query 2 uses a constant relation.
an4 Eample ?ueries
Find all customers &ho ha'e an account from at least the
;Do&nto&n< and the pto&n< branches
5/21/2018 DBMS - Ch2
49/96
an4 Eample ?ueries Find all customers &ho ha'e an account at all branches located
in roo4l%n cit%
customer_name, branch_name(depositor account)
branch_name(branch_city= Brooklyn(branch))
5/21/2018 DBMS - Ch2
50/96
End of Chapter 2
5/21/2018 DBMS - Ch2
51/96
Formal De#nition A basic epression in the relational algebra consists of either one of the
follo&ing:
A relation in the database
A constant relation
9et E!and E" be relational-algebra epressions+ the follo&ing are all
relational-algebra epressions:
E!E"
E!B E"
E! E"
p(E!), 2is a predicate on attributes in E!
s(E!), 'is a list consisting of some of the attributes in E!
x(E!), is the ne& name for the result of E!
5/21/2018 DBMS - Ch2
52/96
Select Operation !otation: p(r)
pis called the selection predicate
De#ned as:
p(r) =tI trandp(t)>
.herepis a formula in propositional calculus consisting of termsconnected b% : (and), (or), (not)Each termis one of:
JattributeK opJattributeK or JconstantK
&here opis one of: , , K, J
Eample of selection:
branc%_name42err&ride5(account)
5/21/2018 DBMS - Ch2
53/96
@roect Operation !otation:
&here!, "are attribute names and ris a relation name
$he result is de#ned as the relation of kcolumns obtained b%erasing the columns that are not listed
Duplicate ro&s remo'ed from result, since relations are sets
Eample: $o eliminate the branc%_nameattribute of account
account_number, balance(account)
)(,,, 21r
kAAA
5/21/2018 DBMS - Ch2
54/96
nion Operation !otation: rs
De#ned as:
r s =tI trorts>
For rsto be 'alid
0 r,smust ha'e the same arity(same number of attributes)
2 $he attribute domains must be compatible(eample: 2ndcolumn
of rdeals &ith the same t%pe of 'alues as does the 2nd
column of s)
Eample: to #nd all customers &ith either an account or a loan customer_name(depositor) customer_name(borrower)
5/21/2018 DBMS - Ch2
55/96
Set Di/erence Operation !otation r s
De#ned as:
r s =tI trandt s>
Set di/erences must be ta4en bet&een compatible
relations
rand smust ha'e the same arit%
attribute domains of r and s must be compatible
5/21/2018 DBMS - Ch2
56/96
Cartesian-@roduct Operation !otationr s
De#ned as:
r s =t 3 It r and 3 s>
Assume that attributes of r(R) and s(S) are disoint ($hat is, R
' ) 3f attributes of r and sare not disoint, then renaming must be
used
5/21/2018 DBMS - Ch2
57/96
Set-3ntersection Operation !otation: rs
De#ned as:
rs = t I trandts>
Assume:
r, sha'e the same arit&
attributes of rand sare compatible
!ote: rs rB (rB s)
5/21/2018 DBMS - Ch2
58/96
Assignment Operation $he assignment operation () pro'ides a con'enient &a% to epress
comple *ueries
.rite *uer% as a se*uential program consisting of
a series of assignments
follo&ed b% an epression &hose 'alue is displa%ed as a result ofthe *uer%
Assignment must al&a%s be made to a temporar% relation 'ariable
Eample: .rite rsas
temp!R-'(r )
temp"R-'((temp! s ) B R-',' (r ))
result temp!Btemp"
$he result to the right of the is assigned to the relation 'ariable on
the left of the
Ma% use 'ariable in subse*uent epressions
en e e a ona - ge ra-
5/21/2018 DBMS - Ch2
59/96
en e e a ona ge raOperations
Lenerali6ed @roection
Aggregate Functions
Outer Hoin
5/21/2018 DBMS - Ch2
60/96
Lenerali6ed @roection Etends the proection operation b% allo&ing arithmetic functions
to be used in the proection list
Eis an% relational-algebra epression
Each of +0, +2, 1, +n are are arithmetic epressions in'ol'ing
constants and attributes in the schema of E
Li'en relation credit_in.o(customer_name, limit, credit_balance),#nd ho& much more each person can spend:
customer_name, limit credit_balance(credit_in.o)
)(,...,,21
EnFFF
5/21/2018 DBMS - Ch2
61/96
Modi#cation of the Database $he content of the database ma% be modi#ed using the
follo&ing operations: Deletion
3nsertion
pdating
All these operations are epressed using the assignmentoperator
5/21/2018 DBMS - Ch2
62/96
Deletion A delete re*uest is epressed similarl% to a *uer%, ecept
instead of displa%ing tuples to the user, the selectedtuples are remo'ed from the database
Can delete onl% &hole tuples+ cannot delete 'alues ononl% particular attributes
A deletion is epressed in relational algebra b%:
rrB E
&here ris a relation and Eis a relational algebra *uer%
5/21/2018 DBMS - Ch2
63/96
Deletion Eamples Delete all account records in the @err%ridge branch
Delete all accounts at branches located in Needham.
r1branch_city = Needham(account branch)
r2account_number,branch_name, balance(r1)
r3customer_name, account_number(r2 depositor)
accountaccount r2
depositordepositor r3
Deleteall loan records with amount in the range of 0 to 50
loanloanamount0and amount50(loan)
accountaccount branch_name = Perryridge(account)
5/21/2018 DBMS - Ch2
64/96
3nsertion $o insert data into a relation, &e either:
specif% a tuple to be inserted
&rite a *uer% &hose result is a set of tuples to be inserted
in relational algebra, an insertion is epressed b%:
r r E
&here ris a relation and Eis a relational algebra epression
$he insertion of a single tuple is epressed b% letting E be aconstant relation containing one tuple
5/21/2018 DBMS - Ch2
65/96
3nsertion Eamples 3nsert information in the database specif%ing that Smith has
02GG in account A-7N at the @err%ridge branch
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.
accountaccount{(A-973,Perryridge, 1200)}
depositordepositor{(Smith, A-973)}
r1(branch_name = Perryridge(borrower loan))
accountaccountloan_number,branch_name,200(r1)
depositordepositorcustomer_name, loan_number(r1)
5/21/2018 DBMS - Ch2
66/96
pdating A mechanism to change a 'alue in a tuple &ithout charging all
'alues in the tuple
se the generali6ed proection operator to do this tas4
Each +iis either the 6 thattribute of r, if the 6 th attribute is not updated, or,
if the attribute is to be updated Fi is an epression, in'ol'ingonl% constants and the attributes of r, &hich gi'es the ne&'alue for the attribute
)(,,,, 21rr
lFFF
5/21/2018 DBMS - Ch2
67/96
pdate Eamples Ma4e interest pa%ments b% increasing all balances b% percent
Pay all accounts with balances over $10,000 6 percent interestand pay all others 5 percent
accountaccount_number,branch_name,balance* 1.06(BAL>10000(account))
account_number,branch_name,balance*1.05(BAL10000(account))
accountaccount_number,branch_name,balance* 1.05(account)
5/21/2018 DBMS - Ch2
68/96
Figure 2N $he branc% relation
5/21/2018 DBMS - Ch2
69/96
Figure 2P: $he loanrelation
Figure 2 7: $he borrower
5/21/2018 DBMS - Ch2
70/96
Figure 27: $he borrowerrelation
Figure 2
5/21/2018 DBMS - Ch2
71/96
gResult of branchQname ;@err%ridge
Top Related