unit 6 rdb design
TRANSCRIPT
-
7/25/2019 Unit 6 RDB Design
1/103
Unit VI:Unit VI:
Relational Database DesignRelational Database Design
Pitfalls of Relational Model:
Redundancy and Anomalies Functional Dependency
Normalization its need andob!ecti"es Form
#NF $NF %NF &'NF and (NF
-
7/25/2019 Unit 6 RDB Design
2/103
First Normal Form
R is in frst normal ormi the domainso all attributes o R are atomic
In object relational/object orienteddatabases, attributes can be compositeor multi-valued
But in relational databases, composite
attributes will need to be atten outand multi-valued attributes need to berepresented b another relation
-
7/25/2019 Unit 6 RDB Design
3/103
First Normal FormFirst Normal Form
Domain is atomicif its elements are consideredto be indi"isible units
)*amples of non+atomic domains: ,et of names composite attributes
Identi-cation numbers li.e ',#/# t0at can be
bro.en up into parts
A relational sc0ema R is in -rst normal formift0e domains of all attributes of R are atomic
Non+atomic "alues complicate storage and
encourage redundant 1repeated2 storage of data )*ample: ,et of accounts stored 3it0 eac0
customer and set of o3ners stored 3it0 eac0account
4e assume all relations are in -rst normal
-
7/25/2019 Unit 6 RDB Design
4/103
First Normal Form !"ont#d$
Atomicity is actually a property of 0o3
t0e elements of t0e domain are used5 )*ample: ,trings 3ould normally be
considered indi"isible
,uppose t0at students are gi"en rollnumbers 30ic0 are strings of t0e formCS0012 or EE1127
If t0e -rst t3o c0aracters are e*tracted to
-nd t0e department t0e domain of rollnumbers is not atomic5
Doing so is a bad idea: leads to encodingof information in application program
rat0er t0an in t0e database5
-
7/25/2019 Unit 6 RDB Design
5/103
%italls in Relational &atabase&esi'n
(e can create tables to represent an )Rdesi'n in man di*erent was
"ombine attributes di*erentl to create tables (h do we choose some was over the
others+
Redundanc
Inabilit to represent certain inormation
)' relationships amon' attributes
-
7/25/2019 Unit 6 RDB Design
6/103
)R or Banin' )nterprise
-
7/25/2019 Unit 6 RDB Design
7/103
.chema &ia'ram or the Banin')nterprise
-
7/25/2019 Unit 6 RDB Design
8/103
)ample
"onsider the relation schema0Lending-schema 1 !branch-name, branch-city,assets, customer-name, loan-number, amount)
Redundanc, wh+ Inabilit to represent certain inormation, wh+
"annot store inormation about a branch i no loans eist
"an use null values, but the are di2cult to handle
-
7/25/2019 Unit 6 RDB Design
9/103
(h Redundanc Is Bad+
(astes space
"omplicates updatin', introducin'possibilit o inconsistenc o
assets
value
(e now wh inabilit to representcertain inormation is bad
-
7/25/2019 Unit 6 RDB Design
10/103
&ecomposition
&ecompose the relation schema
Lending-schema into0
Branch-schema = (branch-name, branch-
city,assets)Loan-info-schema = (customer-name,
loan-number, branch-name, amount)
-
7/25/2019 Unit 6 RDB Design
11/103
3ossless-join decomposition
4ll attributes o an ori'inal schema!R) must appear in thedecomposition !R5, R6):
R = R5R6
For all possible relations ron schema
Rr = R5 !r$ R6 !r$
-
7/25/2019 Unit 6 RDB Design
12/103
Non 3ossless-7oin &ecomposition
&ecomposition o R = (A, B) R1= (A) R6= (B)
A B
121
A
B
12
r
A(r) B(r)
A
(r) B
(r) A B
1212
We do not loss any tuple but we lose therelationship between A and B
-
7/25/2019 Unit 6 RDB Design
13/103
60e &an.ing ,c0ema
branch7 1branch_name branch_city assets2
customer7 1customer_id customer_name customer_streetcustomer_city2
loan7 1loan_number amount2
account7 1account_number balance2
employee7 1employee_id5 employee_name telephone_number
start_date2 dependent_name7 1employee_id dname2
account_branch7 1account_number branch_name2
loan_branch7 1loan_number branch_name2
borrower7 1customer_id loan_number2
depositor7 1customer_id account_number2
cust_banker7 1customer_id employee_id type2
works_for7 1worker_employee_id manager_employee_id2
payment7 1loan_numberpayment_numberpayment_datepayment_amount2
savings_account7 1account_number interest_rate2
checking_account7 1account_number overdraft_amount2
-
7/25/2019 Unit 6 RDB Design
14/103
-
7/25/2019 Unit 6 RDB Design
15/103
4 "ombined .chema (ithoutRepetition
"onsider combinin' loanbranchand loan
loanamtbr1 !loannumber, amount,branchname$
No repetition !as su''ested b eamplebelow$
-
7/25/2019 Unit 6 RDB Design
16/103
40at About ,maller,c0emas8
,uppose 3e 0ad started 3it0 bor_loan 9o3 3ould 3e .no3 tosplit up 1decompose2 it into borrower and loan8
4rite a rule if t0ere 3ere a sc0ema 1loan_number! amount2 t0enloan_number 3ould be a candidate .ey;
Denote as a functional dependency:
loan_numberamount In bor_loan because loan_numberis not a candidate .ey t0e
amount of a loan may 0a"e to be repeated5 60is indicates t0eneed to decompose bor_loan5
Not all decompositions are good5 ,uppose 3e decomposeemployeeinto
employee17 1employee_id employee_name2employee27 1employee_name telephone_number start_date2
60e ne*t slide s0o3s 0o3 3e lose information ++ 3e cannotreconstruct t0e original employeerelation ++ and so t0is is alossy decomposition5
-
7/25/2019 Unit 6 RDB Design
17/103
4 3oss &ecomposition
-
7/25/2019 Unit 6 RDB Design
18/103
"#! "$! ! "n? suc0 t0at
eac0 relation is in good form
t0e decomposition is a lossless+!oindecomposition
@ur t0eory is based on:
functional dependencies
multi"alued dependencies
-
7/25/2019 Unit 6 RDB Design
19/103
Functional Dependencies
Functional dependencies (FDs) are used tospecifyformal measures of the "goodness"of relational designs
FDs and keys are used to define normalformsfor relations
FDs are constraintsthat are derived fromthe meaning and interrelationships of thedata attributes
-
7/25/2019 Unit 6 RDB Design
20/103
Functional Dependencies (2)
A set of attributes Xfunctionally determines a set of
attributes Y if the value of X determines a uniue value for
Y
XY holds if !henever t!o tuples have the same value for
X they must have the same value for Y
If t#$X%&t'$X% then t#$Y%&t'$Y% in any relation instance r()
XY in specifies a constrainton all relation instancesr()
FDs are derived from the real!orld constraints on the
attributes
-
7/25/2019 Unit 6 RDB Design
21/103
*+amples of FD constraints
,ocial ,ecurity -umber determines employee name
,,-*-A.*
/ro0ect -umber determines pro0ect name andlocation
/-1.B* 2/-A.* /345A674-8
*mployee ,,- and pro0ect number determines thehours per !eek that the employee !orks on the
pro0ect
2,,- /-1.B*8 941,
-
7/25/2019 Unit 6 RDB Design
22/103
Functional Dependencies (3)
An FD is a property of the attributes in the
schema
6he constraint must hold on every relation
instance r()
7f : is a key of then : functionallydetermines all attributes in (since !e never
have t!o distinct tuples !ith t#$:%&t'$:%)
-
7/25/2019 Unit 6 RDB Design
23/103
Inference Rules for FDs
;iven a set of FDs F !e can inferadditional FDsthat hold !henever the FDs in F hold
Armstrong
-
7/25/2019 Unit 6 RDB Design
24/103
Additional 1seful 7nference
ules Decomposition
7f X Y> then X Y and X >
1nion 7f X Y and X > then X Y>
/suedotransitivity
7f X Y and Y> then X >
Closureof a set F of FDs is the set F of all FDs
that can be inferred from F
-
7/25/2019 Unit 6 RDB Design
25/103
Introduction to
Normalization Normalization? /rocess of decomposing
unsatisfactory "bad" relations by breaking up their
attributes into smaller relations Normal form? 5ondition using keys and FDs of a
relation to certify !hether a relation schema is in aparticular normal form '-F @-F B5-F based on keys and FDs of a relation
schema C-F based on keys multivalued dependencies
-
7/25/2019 Unit 6 RDB Design
26/103
First Normal Form
Disallo!s composite attributes multivalued
attributes and nested relations attributes
!hose valuesfor an individual tuple are
nonatomic
5onsidered to be part of the definition of
relation
-
7/25/2019 Unit 6 RDB Design
27/103
-
7/25/2019 Unit 6 RDB Design
28/103
-
7/25/2019 Unit 6 RDB Design
29/103
Second Normal Form
1ses the concepts of FDs primary key
Definitions? Prime attribute attribute that is member of the
primary key :
Full functional dependency a FD Y>!here removal of any attribute from Y means the
FD does not hold any more
-
7/25/2019 Unit 6 RDB Design
30/103
!amples
Second Normal Form 2,,- /-1.B*8 941, is a full FD since neither
,,-941, nor /-1.B* 941, hold
2,,- /-1.B*8 *-A.* is not a full FD (it iscalled apartial dependency) since ,,- *-A.* alsoholds
A relation schema is in second normal form (2NF) ifevery nonprime attribute A in is fully functionallydependent on the primary key
can be decomposed into '-F relations via the processof '-F normaliEation
-
7/25/2019 Unit 6 RDB Design
31/103
-
7/25/2019 Unit 6 RDB Design
32/103
-
7/25/2019 Unit 6 RDB Design
33/103
"#ird Normal Form
Definition "ransiti$e functional dependency if there a set of
atribute > that are neither a primary or candidate key andboth X > and Y:holds=
*+amples? ,,- D.;,,- is a transitive FD since
,,- D-1.B* and D-1.B* D.;,,- hold ,,- *-A.* is non-transitive since there is no set of
attributes X !here ,,- X and X *-A.*
-
7/25/2019 Unit 6 RDB Design
34/103
@rd-ormal Form
A relation schema is in t#ird normal form(3NF) if it is in '-F and no nonprime
attribute A in is transitively dependent on
the primary key
-
7/25/2019 Unit 6 RDB Design
35/103
%CNF (%oyce&Codd Normal
Form) A relation schema is in %oyce&Codd Normal
Form (%CNF) if !henever an FD X A holds in
then X is a superkey of *ach normal form is strictly stronger than the previous
one? *very '-F relation is in #-F *very @-F relation is in '-F *very B5-F relation is in @-F
6here e+ist relations that are in @-F but not in B5-F 6he goal is to have each relation in B5-F (or @-F)
-
7/25/2019 Unit 6 RDB Design
36/103
-
7/25/2019 Unit 6 RDB Design
37/103
-
7/25/2019 Unit 6 RDB Design
38/103
B5-F
2,tudentcourse8 7nstructor
7nstructor
5ourse
Decomposing into ' schemas 2,tudent7nstructor8 2,tudent5ourse8
25ourse7nstructor8 2,tudent5ourse8 25ourse7nstructor8 27nstructor,tudent8
FunctionalFunctional
-
7/25/2019 Unit 6 RDB Design
39/103
FunctionalFunctional
DependenciesDependencies
'onstraints on t0e set of legalrelations5
Reuire t0at t0e "alue for a certainset of attributes determinesuniuely t0e "alue for anot0er set ofattributes5
A functional dependency is ageneralization of t0e notation of akey
i l d i
-
7/25/2019 Unit 6 RDB Design
40/103
Functional &ependencies!"ont$
Bet "be a relation sc0ema " and "
60e functional dependency
0olds on"if and only if for any legal relations r1R2
30ene"er any t3o tuples t#and t$of ragree on t0eattributes t0ey also agree on t0e attributes 60at is
t#C 7 t$ C t#C 7 t$ C
)*ample: 'onsider r1A!# 2 3it0 t0e follo3ing instance of r
@n t0is instance$#does N@6 0old but #$does0old5
1 41 53 7
F i l & d i
-
7/25/2019 Unit 6 RDB Design
41/103
Functional &ependencies!"ont$
%is a super.ey for relation sc0ema "if andonly if % "
%is a candidate .ey for "if and only if %
" and for no
%! "
Functional dependencies allo3 us to e*pressconstraints t0at cannot be e*pressed usingsuper.eys5 'onsider t0e sc0ema:bor_loan 7 1customer_id! loan_number!amount 2
4e e*pect t0is functional dependency to 0old:loan_numberamountbut 3ould not e*pect t0e follo3ing to 0old:amount customer_name
9 F i l
-
7/25/2019 Unit 6 RDB Design
42/103
9se o Functional&ependencies
4e use functional dependencies to: test relations to see if t0ey are legal under a
gi"en set of functional dependencies5 If a relation ris legal under a set &of functional
dependencies 3e say t0at rsatis-es &
specify constraints on t0e set of legal relations 4e say t0at &0olds on"if all legal relations on "
satisfy t0e set of functional dependencies &
Note: A speci-c instance of a relationsc0ema may satisfy a functional dependencye"en if t0e functional dependency does not0old on all legal instances5 For e*ample a speci-c instance of loanmay by
c0ance satisfyamount customer_name
F ti l & d i
-
7/25/2019 Unit 6 RDB Design
43/103
Functional &ependencies!"ont$
$ functional dependency istri"ialif it is satis-ed by allinstances of a relation
)*ample' customer_name! loan_number customer_name
customer_name customer_nameIn general is tri"ial if
'losure of a ,et of Functional'losure of a ,et of Functional
-
7/25/2019 Unit 6 RDB Design
44/103
'losure of a ,et of Functional'losure of a ,et of Functional
DependenciesDependencies
-
7/25/2019 Unit 6 RDB Design
45/103
&oyce+'odd Normal Form&oyce+'odd Normal Form
is tri"ial 1i5e5 2
is a super.ey for "
A relation schemaRis in BCNF with respect to a setFof
functional dependencies if for all functional dependenciesinF+of the form
where Rand R,at least one of the following holds:
Example schemanotin BCNF:
bor_loan= (customer_id, loan_number, amount)
becauseloan_number
amount
holds onbor_loan
butloan_number
isnot a superkey
& i . h i t
-
7/25/2019 Unit 6 RDB Design
46/103
&ecomposin' a .chema intoB"NF ,uppose 3e 0a"e a sc0ema " and a
non+tri"ial dependency causes a"iolation of &'NF5
4e decompose "into:
1 U 21 "+ 1 ) 2 2
In our e*ample 7 loan_number
7amountand bor_loanis replaced by
1 U 2 7 1 loan_number! amount2 1 "+ 1 ) 2 2 7 1 customer_id! loan_number2
d d&'NF d D d
-
7/25/2019 Unit 6 RDB Design
47/103
&'NF and Dependency&'NF and Dependency
Preser"ationPreser"ation 'onstraints including functional
dependencies are costly to c0ec. in practiceunless t0ey pertain to only one relation
If it is sucient to test only t0ose
dependencies on eac0 indi"idual relation of adecomposition in order to ensure t0at allfunctional dependencies 0old t0en t0atdecomposition is dependency preserving
&ecause it is not al3ays possible to ac0ie"ebot0 &'NF and dependency preser"ation 3econsider a 3ea.er normal form .no3n asthird normal form
-
7/25/2019 Unit 6 RDB Design
48/103
;hird Normal Form A relation sc0ema "is in t0ird normal form
1%NF2 if for all:
in &E
at least one of t0e follo3ing 0olds:
is tri"ial 1i5e5 2
is a super.ey for "
)ac0 attribute$in G is contained in a candidate.ey for "
1N@6)' eac0 attribute may be in a diHerent candidate.ey2
If a relation is in &'NF it is in %NF 1since in &'NFone of t0e -rst t3o conditions abo"e must 0old25
60ird condition is a minimal rela*ation of &'NFto ensure dependency preser"ation 5
-
7/25/2019 Unit 6 RDB Design
49/103
NormalizationNormalization
Bet "be a relation sc0eme 3it0 a set&offunctional dependencies5
Decide 30et0er a relation sc0eme "is ingood; form5
In t0e case t0at a relation sc0eme "is notin good; form decompose it into a set ofrelation sc0eme >"#! "$! ! "n? suc0 t0at
eac0 relation sc0eme is in good form
t0e decomposition is a lossless+!oindecomposition
Preferably t0e decomposition s0ould bedependency preser"ing5
-
7/25/2019 Unit 6 RDB Design
50/103
-
7/25/2019 Unit 6 RDB Design
51/103
-
7/25/2019 Unit 6 RDB Design
52/103
-
7/25/2019 Unit 6 RDB Design
53/103
Functional-&ependenc;heor
4e no3 consider t0e formal t0eoryt0at tells us 30ic0 functionaldependencies are implied logically by a
gi"en set of functional dependencies5 4e t0en de"elop algorit0ms to
generate lossless decompositions into&'NF and %NF
4e t0en de"elop algorit0ms to test if adecomposition is dependency+preser"ing
"losure o a .et o Functional
-
7/25/2019 Unit 6 RDB Design
54/103
"losure o a .et o Functional&ependencies
=iven a set !set o unctional dependencies, there arecertain other unctional dependencies that are lo'icallimplied b ! For eample0 I ABand B", then we can iner thatA"
;he set o allunctional dependencies lo'icall implied b!is the closureo !
(e denote the closure o !b !#$ (e can fnd all oF#b applin' 4rmstron'#s 4ioms0
i , then 1reJe*i"ity2
i , then 1augmentation2 i , and , then 1transiti"ity2
;hese rules are sound!'enerate onl unctional dependencies that actuall
hold$ and
complete!'enerate all unctional dependencies that hold$
-
7/25/2019 Unit 6 RDB Design
55/103
)ample R = (A, B, ", %, &, ')
! = > A B A ""% &"% ' B &?
some members o !@A &
b transitivit romA B and B &
A% ' b au'mentin'A " with =, to 'etA% "%
and then transitivit with "% '
"% &' b au'mentin' "% ' to iner "% "=',
and au'mentin' o "% & to iner"%' &',
and then transitivit
-
7/25/2019 Unit 6 RDB Design
56/103
%rocedure or "omputin' F@
6o compute t0e closure of a set of functionaldependencies F:
& E7 &repeat
for eac0 functional dependency fin &E apply reJe*i"ity and augmentation rules on f add t0e resulting functional dependencies to& Efor eac0 pair of functional dependencies f#and f$in & E
if f#and f$can be combined using transiti"ityt0en add t0e resulting functional dependency to& Euntil & Edoes not c0ange any furt0er
"losure o Functional
-
7/25/2019 Unit 6 RDB Design
57/103
"losure o Functional&ependencies !"ont$
4e can furt0er simplify manualcomputation of &Eby using t0efollo3ing additional rules5 If 0oldsand 0olds t0en
0olds 1union2
If
0olds t0en
0olds and
0olds 1decomposition2
If 0oldsand 0olds t0en
0olds 1pseudotransiti"ity2
60e abo"e rules can be inferred fromArmstrongs a*ioms5
'los re of Attrib te ,ets'losure of Attribute ,ets
-
7/25/2019 Unit 6 RDB Design
58/103
'losure of Attribute ,ets'losure of Attribute ,ets =iven a set o attributes ,defne the closureo
under!!denoted b @$ as the set oattributes that are unctionall determined b under !
4l'orithm to compute @, the closure o under!
result 01 A
30ile!chan'es to result$ dofor eac0 in!dobeginif resultt0en result 01 result
end
)ample o 4ttribute .et
-
7/25/2019 Unit 6 RDB Design
59/103
)ample o 4ttribute .et"losure
" * +$! #! C! ,! -! ./ & * >$ #$ C
C, -C, .# -?
1$,/E
#5result * $,$5result * $#C, +$ C and$ #/%5result * $#C,- +C, -and C,
$,#C/(5result * $#C,-. +C, .and C,
$,#C-/
Is$,a candidate .ey8#5Is A< a super .ey8
#5Does$, " ** Is 1A
-
7/25/2019 Unit 6 RDB Design
60/103
9ses o 4ttribute "losure
60ere are se"eral uses of t0e attribute closurealgorit0m:
6esting for super.ey: 6o test if is a super.ey 3e compute Eand
c0ec. if Econtains all attributes of "5
6esting functional dependencies 6o c0ec. if a functional dependency 0olds
1or in ot0er 3ords is in &E2 !ust c0ec. if E5
60at is 3e compute Eby using attribute closureand t0en c0ec. if it contains 5
Is a simple and c0eap test and "ery useful
'omputing closure of F For eac0 "! 3e -nd t0e closure E and for eac0
SE 3e output a functional dependency S
-
7/25/2019 Unit 6 RDB Design
61/103
"anonical "over
,ets of functional dependencies may 0a"eredundant dependencies t0at can be inferred fromt0e ot0ers For e*ample: $ Cis redundant in: >$# #C?
Parts of a functional dependency may be redundant
)5g5: on R9,: >$# #C $C? can be simpli-ed to>$
# #
C $
?
)5g5: on B9,: >A
# #
C $C
? can be simpli-ed to>A
# #
C $
?
Intuiti"ely a canonical co"er of F is a minimal; setof functional dependencies eui"alent to F 0a"ing
no redundant dependencies or redundant parts ofdependencies
)traneous 4ttributes
-
7/25/2019 Unit 6 RDB Design
62/103
)traneous 4ttributes
'onsider a set &of functional dependencies andt0e functional dependency in &5 Attribute A is e*traneous in if$
and &logically implies 1&G > ?2 >1 G$2 ?5 Attribute$is e*traneousin if$
and t0e set of functional dependencies1& G > ?2 > + G$2? logically implies &
ote' implication in t0e opposite direction is tri"ialin eac0 of t0e cases abo"e since a stronger;functional dependency al3ays implies a 3ea.erone
)*ample: $C$#C3 Cis e*traneous in$#Csince $& Ccan be
inferred e"en after deleting C
)
-
7/25/2019 Unit 6 RDB Design
63/103
)traneous 'onsider a set &of functional dependencies
and t0e functional dependency in &5 6o test if attribute A
is e*traneousin
#5compute 1>? G A2Eusing t0e dependencies in &
$5 c0ec. t0at 1>
? G A2Econtains K if it does$is
e*traneous in
6o test if attribute$ is e*traneous in
#5compute E using only t0e dependencies inF 7 1& G > ?2 > + G$2?
$5 c0ec. t0at E contains$4 if it does! $ ise*traneous in
"anonical "over
-
7/25/2019 Unit 6 RDB Design
64/103
"anonical "over
A canonical coverfor &is a set of dependencies &c suc0t0at &logically implies all dependencies in &c!and &clogically implies all dependencies in &!and No functional dependency in &ccontains an e*traneous
attribute and )ac0 left side of functional dependency in &cis uniue5
6o compute a canonical co"er for &:repeat
Use t0e union rule to replace any dependencies in & # #and # $3it0 # # $Find a functional dependency 3it0 ane*traneous attribute eit0er in or inIf an e*traneous attribute is found delete it from
until &does not c0ange Note: Union rule may become applicable after some
e*traneous attributes 0a"e been deleted so it 0as tobe re+applied
"omputin' a "anonical "over
-
7/25/2019 Unit 6 RDB Design
65/103
"omputin' a "anonical "over " 7 1$! #! C/
& * 5$ #C # C $ #$#C?
'ombine$ #C and$ # into$ #C
,et is no3 5$ #C! # C! $#C?
$is e*traneous in$#C
'0ec. if t0e result of deleting A from $#C is implied byt0e ot0er dependencies
Les: in fact #C is already present
,et is no3 5$ #C! # C?
Cis e*traneous in$#C
'0ec. if$ Cis logically implied by$ # and t0e ot0erdependencies
Les' using transiti"ity on$ # and # '5
'an use attribute closure of$in more comple* cases
60e canonical co"er is: $ #
# C
3ossless join &ecomposition
-
7/25/2019 Unit 6 RDB Design
66/103
3ossless-join &ecomposition
For t0e case of"7 1"#! "$2!3e
reuire t0at for all possiblerelations ron sc0ema "
r * "11r 2 "21r 2 A decomposition of "into "#and "$
is lossless !oin if and only if at least
one of t0e follo3ing dependenciesis in &E:
"#"$"#
"#"$"$
-
7/25/2019 Unit 6 RDB Design
67/103
)ample R = (A, B, ")
! = A B, B ") "an be decomposed in two di*erent was
R5= (A, B), R6= (B, ") 3ossless-join decomposition0
R5R
6= >B?and B B"
&ependenc preservin'
R1 = (A, B), R6= (A, ") 3ossless-join decomposition0
R5 R6=>A?andA 4B Not dependenc preservin'
!cannot chec B " without computin' R1R6$
&ependenc %reservation
-
7/25/2019 Unit 6 RDB Design
68/103
p
3et !ibe the set o dependencies
! #that include onl attributes inRi$
4 decomposition is dependenc
preservin', i !!5!6!n $@1 ! #
I it is not, then checin' updates orviolation o unctional dependencies
ma reuire computin' joins, which isepensive
% ti
-
7/25/2019 Unit 6 RDB Design
69/103
%reservation
;o chec i a dependenc is preserved in a
decomposition o Rinto R5, R6, C, Rnwe appl theollowin' test !with attribute closure done withrespect to !$ result 1
30ile!chan'es to result$ dofor eac0Riin the decomposition
t1 !result Ri$@Riresult = result t
I resultcontains all attributes in , then the unctionaldependencis preserved
(e appl the test on all dependencies in ! tochec i a decomposition is dependenc preservin'
;his procedure taes polnomial time, instead othe eponential time reuired to compute !#and!!5!6 !n$@
-
7/25/2019 Unit 6 RDB Design
70/103
)ample R = !A, B, " $
! = >ABB "?
De 1 >A?
Ris not in B"NF &ecomposition R51 !A, B), R61 (B, ")
R5and R6in B"NF
3ossless-join decomposition
&ependenc preservin'
-
7/25/2019 Unit 6 RDB Design
71/103
;estin' or B"NF ;o chec i a non-trivial dependenc causes a
violation o B"NF5 compute @!the attribute closure o $, and6 veri that it includes all attributes o R, that is, it is a
supere o R .implifed test0 ;o chec i a relation schema Ris in
B"NF, it su2ces to chec onl the dependencies in
the 'iven set !or violation o B"NF, rather thanchecin' all dependencies in !@ I none o the dependencies in !causes a violation o
B"NF, then none o the dependencies in !@will cause aviolation o B"NF either
A B, B" *?
&ecompose Rinto R5 1!A,B$ and R6 1!A,",*, +$ Neither o the dependencies in !contain onl attributes rom
!A,",*,+$ so we mi'ht be mislead into thinin' R6satisfesB"NF
In act, dependencA"*in !@shows R6is not in B"NF
;estin' &ecomposition or
-
7/25/2019 Unit 6 RDB Design
72/103
;estin' &ecomposition orB"NF
6o c0ec. if a relation "iin adecomposition of "is in &'NF )it0er test Ri for &'NF 3it0 respect to t0e
restrictionof F to Ri 1t0at is all FDs in FEt0at contain only attributes from Ri2
or use t0e original set of dependencies &t0at 0old on " but 3it0 t0e follo3ing test:
for e"ery set of attributes "i c0ec. t0at E1t0eattribute closure of 2 eit0er includes no attribute of"i+ or includes all attributes of "i5
If t0e condition is "iolated by some in &t0e dependency 1E + 2 "ican be s0o3n to 0old on "i and "i"iolates &'NF5
4e use abo"e dependency to decompose "i
B"NF &ecomposition
-
7/25/2019 Unit 6 RDB Design
73/103
B"NF &ecomposition4l'orithm
result 01 >R ?Adone 01 alseAcompute ! @A30ile 1not done) doif !there is a schema Riin result that is not in B"NF$
t0en beginlet be a nontrivial unctional dependenc thatholds on Ri such that Riis not in ! @,
and = A
result 01 !result Ri) !Ri $ !, ) endelsedone 01 trueK
Note0 each Riis in B"NF, and decomposition is lossless-
join
)ample o B"NF
-
7/25/2019 Unit 6 RDB Design
74/103
)ample o B"NF&ecomposition
" * 1$! #! C 2& * >$#
# C?
ey 7 >$? "is not in &'NF 1# C but
# is not super.ey2
Decomposition "#7 1#! C/
"$7 +$!#/
)ample o B"NF
-
7/25/2019 Unit 6 RDB Design
75/103
)ample o B"NF&ecomposition
@riginal relation" andfunctional dependency& " 71branch_name! branch_city! assets!
customer_name! loan_number! amount 2 & 7>branch_name assets branch_city
loan_number amount branch_name ?ey 75loan_number! customer_name?
Decomposition "#7 1branch_name! branch_city! assets 2 "$7 1branch_name! customer_name! loan_number!
amount 2 "%7 1branch_name! loan_number! amount 2 "(7 1customer_name! loan_number 2
Final decomposition"#! "%! "(
%reservation
-
7/25/2019 Unit 6 RDB Design
76/103
%reservation
R = !., /, L $! = >./ L
L / ?
;wo candidate es 1./ and.L R is not in B"NF 4n decomposition o Rwill ail
to preserve
./ L ;his implies that testin' or./L reuires a join
It is not always possible to get a BCNF decomposition that is
dependency preserving
;hird Normal Form0
-
7/25/2019 Unit 6 RDB Design
77/103
;hird Normal Form0Eotivation
;here are some situations where B"NF is not dependenc preservin', and e2cient checin' or F& violation on updates
is important
.olution0 defne a weaer normal orm,called ;hird Normal Form !NF$ 4llows some redundanc !with resultant
problemsA we will see eamples later$
But unctional dependencies can be checedon individual relations without computin' ajoin
;here is alwas a lossless-join, dependenc-preservin' decomposition into NF
l
-
7/25/2019 Unit 6 RDB Design
78/103
NF )ample
Relation R:
" * 16! %! 2& * >6% ! % ?
63o candidate .eys: 6% and6
"is in %NF
6% 6% is a super.ey
% % is contained in acandidate .ey
Redundanc in NF
-
7/25/2019 Unit 6 RDB Design
79/103
;here is some redundanc in thisschema
)ample o problems due toredundanc in NF
R = !., /, L)! = >./ L, L / ?
J
j1
j2
j3
null
Ll1
l1
l1
l2
Kk1
k1
k1
k2
repetition of information (e.g., the relationshipl1,k1)
need to use null values (e.g., to represent the relationship l2,k2where there is no corresponding value forJ).
; i NF
-
7/25/2019 Unit 6 RDB Design
80/103
;estin' or NF
GptimiHation0 Need to chec onl F&s in !,need not chec all F&s in !#
9se attribute closure to chec or eachdependenc , i is a supere
I is not a supere, we have to veri ieach attribute in is contained in acandidate e o R this test is rather more epensive, since it involve
fndin' candidate es
testin' or NF has been shown to be N%-hard Interestin'l, decomposition into third normal
orm !described shortl$ can be done inpolnomial time
%NF Decomposition%NF Decomposition
-
7/25/2019 Unit 6 RDB Design
81/103
ppAlgorit0mAlgorit0m
3et !cbe a canonical cover or !i 01 8Afor eac0 unctional dependenc in !cdoif none o the schemas R0, 5 0 i contains t0en begini 01 i # 5ARi 01 endifnone o the schemas R0, 5 0 i contains a candidatee or Rt0en begini 01i @ 5ARi01 an candidate e or Rendreturn (R5, R6, , Ri)
!"ont $
-
7/25/2019 Unit 6 RDB Design
82/103
!"ont$
4bove al'orithm ensures0
each relation schema Riis in NF
decomposition is dependencpreservin' and lossless-join
%roo o correctness is at end o thispresentation !clic here$
NF &ecomposition0 4n
-
7/25/2019 Unit 6 RDB Design
83/103
NF &ecomposition0 4n)ample
Relation schema0
custbanerbranch = !customerid, em2loyeeid,branchname, ty2e $
;he unctional dependencies or this relationschema are01$ customerid, em2loyeeid branchname, ty2e
3$ em2loyeeid branchname4$ customerid, branchname em2loyeeid
(e frst compute a canonical cover branchname is etraneous in the rhs o the 5st
dependenc
No other attribute is etraneous, so we 'et F" 1 customerid, em2loyeeid ty2e
em2loyeeid branchname customerid, branchname em2loyeeid
-
7/25/2019 Unit 6 RDB Design
84/103
;he forloop 'enerates ollowin' NF schema0
!customerid, em2loyeeid, ty2e $ !em2loyeeid, branchname$ !customerid, branchname, em2loyeeid)
Gbserve that !customerid, em2loyeeid, ty2e $ contains a candidatee o the ori'inal schema, so no urther relation schema needs beadded
I the F&s were considered in a di*erent order, with the 6ndoneconsidered ater the rd,
!em2loyeeid, branchname$would not be included in the decomposition because it is a subset o
!customerid, branchname, em2loyeeid)
Einor etension o the NF decomposition al'orithm0 at end o or loop,detect and delete schemas, such as !em2loyeeid, branchname$, which
are subsets o other schemas result will not depend on the order in which F&s are considered
;he resultant simplifed NF schema is0
!customerid, em2loyeeid, ty2e$ !customerid, branchname, em2loyeeid)
"omparison o B"NF and
-
7/25/2019 Unit 6 RDB Design
85/103
"omparison o B"NF andNF
It is alwas possible to decompose a relationinto a set o relations that are in NF suchthat0
the decomposition is lossless
the dependencies are preserved
It is alwas possible to decompose a relationinto a set o relations that are in B"NF such
that0 the decomposition is lossless
it ma not be possible to preserve dependencies
&esi'n =oals
-
7/25/2019 Unit 6 RDB Design
86/103
&esi'n =oals
-
7/25/2019 Unit 6 RDB Design
87/103
Eultivalued &ependencies!E&s$
3et Rbe a relation schema and let RandR$ ;he multi5alued de2endency
holds on Ri in an le'al relation r(R),or allpairs or tuples t5 and t3in rsuch that t5JK 1t3 JK, there eist tuples t4and tLin r suchthat0t5JK 1 t3 JK 1 tJK 1 tLJK
tJK 1 t5 JKtJR K 1 t6JR KtL JK 1 t6JKtLJR K 1 t5JR K
E& !" t $
-
7/25/2019 Unit 6 RDB Design
88/103
E& !"ont$ ;abular representation o
) l
-
7/25/2019 Unit 6 RDB Design
89/103
)ample
3et Rbe a relation schema with a set oattributes that are partitioned into nonemptsubsets
6, 7, 8
(e sa that 6 7 !6multidetermines7 $i and onl i or all possible relations r !R $My5,95, w5 rand My5,96, w6 r
thenMy5,95, w6 rand My5,96, w5 r
Note that since the behavior o7and 8areidentical it ollows that6 7 i 68
) ample !"ont $
-
7/25/2019 Unit 6 RDB Design
90/103
)ample !"ont$ In our eample0
course teachercourse boo
;he above ormal defnition is supposed toormaliHe the notion that 'iven a
particular value o 6 !course$ it hasassociated with it a set o values o7(teacher) and a set o values o 8 (boo),and these two sets are in some senseindependent o each other
Note0 I 6 7 then 6 7 Indeed we have !in above notation$75= 76;he claim ollows
9se o Eultivalued
-
7/25/2019 Unit 6 RDB Design
91/103
&ependencies (e use multivalued dependencies in two was0
5 ;o test relations to determinewhether the are le'alunder a 'iven set o unctional and multivalueddependencies
6 ;o speci constraintson the set o le'al relations (eshall thus concern ourselves onlywith relations thatsatis a 'iven set o unctional and multivalueddependencies
I a relationrails to satis a 'iven multivalued
dependenc, we can construct a relations r thatdoes satis the multivalued dependenc baddin' tuples to r$
;heor o E&s
-
7/25/2019 Unit 6 RDB Design
92/103
;heor o E&s
From the defnition o multivalued dependenc, wecan derive the ollowin' rule0 I , then
;hat is, ever unctional dependenc is also amultivalued dependenc
;he closure&@o *is the set o all unctional andmultivalued dependencies lo'icall implied b * (e can compute &@rom *, usin' the ormal defnitions
o unctional dependencies and multivalueddependencies
(e can mana'e with such reasonin' or ver simple
multivalued dependencies, which seem to be mostcommon in practice
For comple dependencies, it is better to reason aboutsets o dependencies usin' a sstem o inerence rules
Fourth Normal Form
-
7/25/2019 Unit 6 RDB Design
93/103
Fourth Normal Form 4 relation schema Ris in LNF with
respect to a set *o unctional andmultivalued dependencies i or allmultivalued dependencies in *@o the
orm , where Rand R, atleast one o the ollowin' hold0
is trivial !ie, or = R)
is a supere or schema R I a relation is in LNF it is in B"NF
Restriction o Eultivalued
-
7/25/2019 Unit 6 RDB Design
94/103
&ependencies
60e restriction of D to Riis t0eset Diconsisting of All functional dependencies in DE
t0at include only attributes of Ri
All multi"alued dependencies of t0e
form + Ri2
30ere Ri and is in DE
LNF &ecomposition
-
7/25/2019 Unit 6 RDB Design
95/103
p4l'orithm result:1 >R?A
done01 alseAcom2ute *@A3et &idenote the restriction o &@to Ri
30ile !not done$if !there is a schema Riin result that is not in LNF$
t0en
beginlet be a nontrivial multivalued dependencthat holds on Risuch that Ri is not in *i, and =A
result 01 !result - Ri$ !Ri- $ !, $Aend
else done01 trueANote0 each Riis in LNF, and decomposition is lossless-
join
)ample
-
7/25/2019 Unit 6 RDB Design
96/103
)ample R1!A, B, ", %, &, '$
! 1>A BB&'"% &?
Ris not in LNF sinceABandAis not a supereor R
&ecompositiona$ R11 !A, B$ !R1is in LNF$
b$ R61 !A, ", %, &, '$ !R3is not in LNF$
c$ RF1 !", %, &$ !RFis in LNF$
d$ R:1 !A, ", %, '$ !R:is not in LNF$ .inceABand B&',A&',A'
e$ R;1 !A, '$ !R;is in LNF$
$R
-
7/25/2019 Unit 6 RDB Design
97/103
Further Normal Forms oin dependencies'eneraliHe multivalued
dependencies lead to pro!ect+!oin normal form 1%7NF$ !alsocalled -ft0 normal form$
4 class o even more 'eneral constraints, leadsto a normal orm called domain+.ey normalform
%roblem with these 'eneraliHed constraints0 arehard to reason with, and no set o sound andcomplete set o inerence rules eists
-
7/25/2019 Unit 6 RDB Design
98/103
'%rocess
(e have assumed schema Ris 'iven Rcould have been 'enerated when convertin'
)-R dia'ram to a set o tables
Rcould have been a sin'le relation containin'allattributes that are o interest !called
uni"ersal relation$ NormaliHation breas Rinto smaller relations
Rcould have been the result o some ad hocdesi'n o relations, which we then test/convertto normal orm
)R Eodel and NormaliHation
-
7/25/2019 Unit 6 RDB Design
99/103
)R Eodel and NormaliHation
(hen an )-R dia'ram is careull desi'ned,identiin' all entities correctl, the tables 'eneratedrom the )-R dia'ram should not need urthernormaliHation
-
7/25/2019 Unit 6 RDB Design
100/103
PerformancePerformance May 3ant to use non+normalized sc0ema for performance
For e*ample displaying customer_namealong 3it0account_numberand balancereuires !oin of account3it0 depositor
Alternati"e #: Use denormalized relation containingattributes of accountas 3ell as depositor3it0 all abo"eattributes
faster loo.up
e*tra space and e*tra e*ecution time for updates
e*tra coding 3or. for programmer and possibility oferror in e*tra code
Alternati"e $: use a materialized "ie3 de-ned as account depositor
&ene-ts and dra3bac.s same as abo"e e*cept noe*tra coding 3or. for programmer and a"oids possibleerrors
Gther &esi'n Issues
-
7/25/2019 Unit 6 RDB Design
101/103
Gther &esi'n Issues
.ome aspects o database desi'n are not cau'ht bnormaliHation
)amples o bad database desi'n, to be avoided0Instead o earnings !com2anyid, year, amount $, use earnings3, earnings3;, earnings3
-
7/25/2019 Unit 6 RDB Design
102/103
Eodelin' ;emporal &ata
>em2oral datahave an association time interval durin' whichthe data are 5alid$ 4 sna2shotis the value o the data at a particular point in
time .everal proposals to etend )R model b addin' valid time to
attributes, e' address o a customer at di*erent points in time entities, e' time duration when an account eists relationships, e' time durin' which a customer owned an
account But no accepted standard 4ddin' a temporal component results in unctional
dependencies liecustomerid customerstreet, customercity
not to hold, because the address varies over time 4 tem2oral functional de2endency P Q holds on schema R
i the unctional dependenc P Q holds on all snapshots orall le'al instances r !R $
Eodelin' ;emporal &ata
-
7/25/2019 Unit 6 RDB Design
103/103
!"ont$ In practice, database desi'ners ma add
start and end time attributes to relations
)' course!courseid, coursetitle$
course!courseid, coursetitle, start, end$
"onstraint0 no two tuples can have overlappin' validtimes