Anda di halaman 1dari 55

RELATONAL DATABASE DESGN

FUNCTONAL DEPENDENCES
Constraints on the set of legal relations.
Require that the value for a oertain set of
attributes determines uniquely the value for
another set of attributes.
A funotional dependenoy is a generalization of
the notion of a ke,.
FUNCTONAL DEPENDENCES (CONT.)
Let R be a relation sohema and X, be sets of attributes in R.
A funotional dependenoy from X to exists if and only if:
lor every instanoe of R, if two tuples in R agree
on the values of the attributes in X, then they
agree on the values of the attributes in
we write X F and say that X determines
Lxample on 3tudent (sid, name, supervisor_id, speoialization):
supervisor_id F speoialization
lf two student reoords have the same supervisor (e.g.,
Uimitris), then their speoialization (e.g., Uatabases)
must be the same
0n the other hand, if the supervisors of 2 students are
different, we do not oare about their speoializations
(they may be the same or different).
FUNCTONAL DEPENDENCES (CONT.)
K is a superkey for relation sohema l if and only if K F l
K is a oandidate key for l if and only if
K F l, and
for no - K, - F l
lunotional dependenoies allow us to express oonstraints that
oannot be expressed using superkeys. Consider the sohema:
loan-|nfo-sohema = (ousromer-name, |oan-number,
branoh-name, amounr).
we expeot this set of funotional dependenoies to hold:
|oan-number F amounr
|oan-number F branoh-name
but would not expeot the following to hold:
|oan-number F ousromer-name
FUNCTONAL DEPENDENCES (CONT.)
lor 3tudent (sid, name, supervisor_id,
speoialization),
we write:
sid F name, supervisor_id, speoialization
1he sid determines all attributes (i.e., the entire reoord)
lf two tuples in the relation student have the same sid, then they must
have the same values on all attributes.
ln other words they must be the same tuple (sinoe the relational modes
does not allow duplioate reoords)
USE OF FUNCTONAL DEPENDENCES
we use funotional dependenoies to:
test relations to see if they are legal under a given set
of funotional dependenoies.
lf a relation r is legal under a set l of funotional
dependenoies, we say that r satisfies l.
speoify oonstraints on the set of legal relations
we say that l holds on l if all legal relations on l satisfy the
set of funotional dependenoies l.
Note: A speoifio instanoe of a relation sohema may
satisfy a funotional dependenoy even if the
funotional dependenoy does not hold on all legal
instanoes. lor example, a speoifio instanoe of
loan-sohema may, by ohanoe, satisfy
|oan-number F ousromer-name.
FUNCTONAL DEPENDENCES (CONT.)
funotional dependenoy is trivial if it is
satisfied by all instanoes of a relation
.g.
ousromer-name, |oan-number F ousromer-name
ousromer-name F ousromer-name
ln general, - F . is trivial if . -
name, supervisor_id F name
lf two reoords have the same values on both the name
and supervisor_id attributes, then they obviously have
the same supervisor_id.
1rivial dependenoies hold for all relation instanoes
CLOSURE OF A SET OF FUNCTONAL
DEPENDENCES
0iven a set l set of funotional dependenoies, there are oertain
other funotional dependenoies that are logioally implied by l.
L.g. lf A F B and B F C, then we oan infer that A F C
1he set of all funotional dependenoies logioally implied by l is
the o|osure of l.
we denote the o|osure of l by l
+
.
we oan find all of l
+
by applying Armstrong's Axioms:
if . -, then - F . (reflexivity)
if - F ., then - F . (augmentation)
if - F ., and . F , then - F (transitivity)
1hese rules are
sound (generate only funotional dependenoies that aotually hold)
and
oomplete (generate all funotional dependenoies that hold).
EXAMPLE
l = (, B, 0, C, l, l)
l = F B
F 0
0C F l
0C F l
B F l
some members of l
+
F l
by transitivity from F B ano B F l
C F l
by augmenting F 0 with 0, to get C F 0C
and then transitivity with 0C F l
0C F ll
from 0C F l ano 0C F l . union rule" oan be inferred
from
definition of funotional dependenoies, or
Augmentation of 0C F l to infer 0C F C0l, augmentation of
0C F l to infer 0Cl F ll, and then transitivity
PROCEDURE FOR COMPUTNG F
+
1o oompute the olosure of a set of funotional
dependenoies l:
l
+
= l
repeat
for eaoh funotional dependenoy f in l
+
apply reflexivity and augmentation rules on f
add the resulting funotional dependenoies to
l
+
for eaoh pair of funotional dependenoies f
1
and f
2
in l
+
if f
1
and f
2
oan be oombined using transitivity
then add the resulting funotional
dependenoy to l
+
until l
+
does not ohange any further
CLOSURE OF FUNCTONAL
DEPENDENCES (CONT.)
we oan further simplify manual
oomputation of l
+
by using the following
additional rules.
lf - F . holds and - F holds, then - F .
holds (union)
lf - F . holds, then - F . holds and - F
holds (deoomposition)
lf - F . holds and . F 0 holds, then - F
0 holds (pseudotransitivity)
1he above rules oan be inferred from
Armstrong's axioms.
CLOSURE OF ATTRBUTE SETS
0iven a set of attributes - we define the o|osure of -
under l (denoted by -
+
) as the set of attributes that are
funotionally determined by - under l.
- F . is in l
+
. -
+
Algorithm to oompute -
+
, the olosure of - under l
resu|r := -,
while (ohanges to resu|r) do
for eaoh . F in l do
begin
if . resu|r then resu|r := resu|r
end
EXAMPLE OF ATTRBUTE SET
CLOSURE
l = (, B, 0, C, l, l)
l = F B
F 0
0C F l
0C F l
B F l}
(C)
+
1. resu|r = C
2. resu|r = B0C ( F 0 and F B)
3. resu|r = B0Cl (0C F l and 0C CB0)
4. resu|r = B0Cll (0C F l and 0C CB0l)
USES OF ATTRBUTE CLOSURE
1here are several uses of the attribute olosure algorithm:
1esting for superkey:
1o test if - is a superkey, we oompute -
+,
and oheok if -
+
oontains all attributes of l.
1esting funotional dependenoies
1o oheok if a funotional dependenoy - F . holds (or, in
other words, is in l
+
), just oheok if . -
+
.
1hat is, we oompute -
+
by using attribute olosure, and then
oheok if it oontains ..
ls a simple and oheap test, and very useful
Computing olosure of l
lor eaoh l, we find the olosure
+
, and for eaoh $
+
,
we output a funotional dependenoy F $.
CANONCAL COVER
3ets of funotional dependenoies may have redundant
dependenoies that oan be inferred from the others
Lg: A F C is redundant in: A F B, B F C, A F C}
Parts of a funotional dependenoy may be redundant
L.g. on R3: A F B, B F C, A F CU} oan be simplified to
A F B, B F C, A F U}
L.g. on L3: A F B, B F C, AC F U} oan be simplified to
A F B, B F C, A F U}
lntuitively, a oanonioal oover of l is a minimal" set of
funotional dependenoies equivalent to l, with no
redundant dependenoies or having redundant parts of
dependenoies
EXTRANEOUS ATTRBUTES
Lxample of extraneous/redundant attribute
on R3:
AFB, BFC, AFC,U oan be simplified to
AFB, BFC, AFU
(beoause AFC is inferred from A F B, BFC)
Lxample of extraneous/redundant attribute
on L3:
AFB, BFC, A,CFU oan be simplified to
AFB, BFC, AFU
(beoause of AFC)
DATABASE NORMALZATON
Uatabase normalization is the prooess of removing redundant
data from your tables in to improve storage effioienoy, data
integrity, and soalability.
ln the relational model, methods exist for quantifying how
effioient a database is. 1hese olassifioations are oalled normal
forms (or Nl), and there are algorithms for oonverting a given
database between them.
Normalization generally involves splitting existing tables into
multiple ones, whioh must be re-joined or linked eaoh time a
query is issued.
PROCESS OF NORMALZATON
Ueoide whether a partioular relation l is in good" form.
ln the oase that a relation l is not in good" form, deoompose
it into a set of relations l
1
, l
2
, ..., l
n
} suoh that
eaoh relation is in good form
the deoomposition is a lossless-join deoomposition
0ur theory is based on:
funotional dependenoies
multivalued dependenoies
THE NORMALZATON PROCESS
1he normalization prooess is often exeouted in a
step-by-step fashion, and eaoh step
oorresponds to a speoifio normal form.
ln eaoh step, a set of rules are tested against
individual relations in order to normalize the
database to a oertain degree.
ln testing, when a requirement is not satisfied,
the relation that violates the requirements is
deoomposed into relations that speoifioally
satisfies the requirements of the normalization.
FRST NORMAL FORM (1NF)
An unnormalized form (uNl) oontains one or more
repeating groups.
A relation is in the 1Nl, if the interseotion of eaoh row
and oolumn oontains only one value.
istorioally, the 1Nl was defined to disallow multi-
valued attributes, oomposite attributes and their
oombinations. lt requires that the domains of the
attributes must be arom|o and that the value of the
attribute must be a s|ng|e value.
1he 1Nl disallows relations within a relation or relations
as attributes of tuples.
UNNORMALZED DEPARTMENT
RELATON
Lxample:
Uepartment(Uept_No, UName, UChair, Looation)
Uept_No UName UChair Looation
10 Computer 1eoh P. Casanova Manila, Cavite,
Baoolod}
7 3oftware 1eoh R. Reyes Manila, Misamis}
12 lnformation 1eoh R. Molano Manila}
NORMALZATON TO 1NF
Normalization to 1Nl would inolude:
1. Nominate an attribute (or group of attributes) as a key
for the unnormalized table.
lor the Uepartment relation: Uept_No is the key.
2. Remove the repeating group(s) by plaoing the
repeating data, along with the oopy of the original key
attribute(s) in a separate relation.
Repeating group for the Uepartment relation : Looation
3. ldentify primary keys for the new relations
Uepartment(Uept_No, UName, UChair,
Looation)
Uepartment(Uept_No, UName , UChair)
Uept_Loo(Uept_No, Looation)
NormaIization to 1NF
DEPARTMENT RELATON
Uepartment(Uept_No, UName, UChair)
Uept_No UName UChair
10 Computer 1eoh P. Casanova
7 3oftware 1eoh R. Reyes
12 lnformation 1eoh R. Molano
DEPT_LOC RELATON
Uept_Loo(Uept_No, Looation)
Uept_No Looation
10 Manila
10 Cavite
10 Baoolod
7 Manila
7 Misamis
12 Manila
PRME & NON-PRME ATTRBUTES
An attribute of relation sohema R is oalled a
prime attribute of R if it is a member of any key
of R.
An attribute is oalled a nonprime attribute of R
if it is not a member of any oandidate key.
PRME & NON-PRME ATTRBUTES
laoulty_work(lNo, lName, w1ype, wUeso,
ours)
Prime Attributes: lNo, w1ype
Non-prime Attributes: wUeso, ours , lName
8econd Norma| Form (2NF}: ased on Pr|mary
Key
Tre 2NF |s oased or lre corcepl ol lu|| lurcl|ora|
deperdercy.
A re|al|or screra R |s |r 2NF |l every rorpr|re
allr|oule A |r R |s lu||y lurcl|ora||y deperderl or
lre pr|rary |ey ol R.
8econd Norma| Form (2NF} ased on Pr|mary
Key
Facu|ly_wor|(FNo, FNare, wType, w0esc,
lours)
Pr|rary Key: FN0,wType
Tre Facu|ly_wor| re|al|or |s |r 1NF (ro repeal|rg
groups), oul rol |r 2NF oecause:
FNo F FNare ard
wType F w0esc
8econd Norma| Form (2NF} ased on Pr|mary
Key
Norra||zal|or |rlo lre 2NF Wou|d |rvo|ve:
1. lderl|l|cal|or ol parl|a| deperderc|es.
2. Rerova| ol parl|a||y deperderl allr|oules ard
creal|or ol reW re|al|ors Wr|cr srou|d cors|sl ol
lre parl|a||y deperderl allr|oules ard lre|r
delerr|rarls.
2NF of Facu|ty_work Re|at|on
Facu|ly_Wor|(FNo, FNare, wType, w0esc, lours)
Fu|| deperdercy:
{FNo, wType Flours
1. lderl|l|cal|or ol parl|a| deperderc|es.
FNo F FNare
wType F w0esc
2.Rerova| ol parl|a||y deperderl allr|oules
Fw1(FNo, wType, lours)
3. Creal|or ol reW re|al|ors Wr|cr srou|d cors|sl ol lre parl|a||y deperderl allr|oules ard lre|r
delerr|rarls
Fw2(FNo, FNare)
Fw3(wType, w0esc)
enera| 0ef|n|t|on of 2NF
A re|al|or screra R |s |r lre 2NF |l every
rorpr|re allr|oule A |r R |s rol parl|a||y deperderl
or ary |ey ol R.
ll car a|so oe slaled lral a re|al|or screra R |s |r
lre 2NF |l every rorpr|re allr|oule A |r R |s lu||y
lurcl|ora||y deperderl or every |ey ol R.
%rans|t|ve 0ependency
A lurcl|ora| deperdercy X F |r a re|al|or
screra R |s a lrars|l|ve deperdercy |l lrere |s a
sel ol allr|oules Z lral |s rol a suosel ol ary |ey ol
R ard oolr X FZ ard ZF ro|d.
%rans|t|ve 0ependency
Facu|ly_0epl(FNare, FNo, 80ale, Address, 0No, 0Nare,
Cra|rFNo)
Furcl|ora| deperderc|es:
FNo FFNare, 80ale, Address, 0No
0No F 0Nare, Cra|rFNo
0No |s rol a suosel ol lre |ey ol Facu|ly_0epl
FNo F 0Nare |s a lrars|l|ve deperdercy
FNo F Cra|rFNo |s a lrars|l|ve deperdercy
Tre lr|rd rorra| (3NF) |s oased or lre corcepl ol
lrars|l|ve deperdercy.
A re|al|or screra R |s |r 3NF |l |l |s |r 2NF ard ro
rorpr|re allr|oule R |s lrars|l|ve|y deperderl or
lre pr|rary |ey.
%|rd Norma| Form (3NF} ased on Pr|mary Key
Norra||zal|or |rlo lre 3NF Wou|d |rvo|ve:
1. lderl|l|cal|or ol lrars|l|ve deperderc|es.
2. Rerova| ol lrars|l|ve|y deperderl allr|oules
ard creal|or ol reW re|al|ors Wr|cr srou|d cors|sl
ol lre lrars|l|ve|y deperderl allr|oules ard lre|r
delerr|rarls.
%|rd Norma| Form (3NF} ased on Pr|mary Key
Facu|ly_0epl(FNare,FNo,80ale,Address,0No,0Nare,Cra|rFNo)
1. lderl|l|cal|or ol lrars|l|ve deperderc|es
FNo FFNare, 80ale, Address, 0No
0No F 0Nare, Cra|rFNo
0No |s rol a suosel ol lre |ey ol Facu|ly_0epl
Trars|l|ve 0eperderc|es:
FNo F 0Nare |s a lrars|l|ve deperdercy
FNo F Cra|rFNo |s a lrars|l|ve deperdercy
2. Rerova| ol lrars|l|ve|y deperderl allr|oules
F01(FNare, FNo, 80ale, Address, 0No)
3. Creal|or ol reW re|al|ors Wr|cr srou|d cors|sl ol lre lrars|l|ve|y deperderl allr|oules ard lre|r
delerr|rarls
F02(0No, 0Nare, Cra|rFNo)
%|rd Norma| Form (3NF} ased on Pr|mary Key
enera| 0ef|n|t|on of %|rd Norma| Form (3NF}
A re|al|or screra R |s |r 3NF |l, Wrerever a lurcl|ora| deperdercy XFA ro|ds |r R,
e|lrer:
1. X |s a super|ey ol R, or
2. A |s a pr|re allr|oule ol R
A|lerral|ve|y, a re|al|or screra R |s |r 3NF |l every rorpr|re allr|oule ol R |s:
1. Fu||y lurcl|ora||y deperderl or every |ey ol R, ard
2. Norlrars|l|ve|y deperderl or every |ey ol R .
Norra||zal|or |rlo lre 3NF Wou|d |rvo|ve:
1. lderl|l|cal|or ol lrars|l|ve deperderc|es |r every |ey.
2. Rerova| ol lrars|l|ve|y deperderl allr|oules ard creal|or ol reW re|al|ors
Wr|cr srou|d cors|sl ol lre lrars|l|ve|y deperderl allr|oules ard lre|r delerr|rarls.
amp|e of 3NF
ols1(Properly_l0#,Courly_Nare,ol#,Area,Pr|ce)
ols2(Courly_Nare,Tax_Rale)
Furcl|ora| deperderc|es:
Properly_l0# F Courly_Nare,ol#,Area,Pr|ce,Tax_Rale
Courly_Nare FTax_Rale
Area F Pr|ce
Trerelore: Properly_l0# F Pr|ce |s a lrars|l|ve deperdercy
ols1A(Properly_l0#,Courly_Nare,ol#,Area)
ols18(Area, Pr|ce)
ols2(Courly_Nare,Tax_Rale)
oyce-6odd Norma| Form (6NF}
8oyce-Codd rorra| lorr |s slr|cler lrar 3NF, lral |s, every
re|al|or |r 8CNF |s a|so |r 3NF.
A re|al|or |r 3NF |s rol recessar||y |r 8CNF.
A re|al|or screra R |s |r 8CNF |l Wrerever a lurcl|ora|
deperdercy XFA ro|ds |r R, lrer X |s a super|ey ol R.
Tre or|y d|llererce oelWeer 8CNF ard 3NF |s lral lre
cord|l|or (2) ol 3NF, (Wr|cr a||oWs A lo oe pr|re |l X |s rol
a super|ey) |s aoserl lror 8CNF.
Norra||zal|or |rlo lre 8CNF Wou|d |rvo|ve:
1. lderl|l|cal|or ol lurcl|ora| deperderc|es lral do rol |r
|rvo|ve super|eys ard Wrere lre lurcl|ora||y deperderl
allr|oules are rol pr|re allr|oules.
2. Rerova| ol lurcl|ora||y deperderl allr|oules ard
creal|or ol reW re|al|ors Wr|cr srou|d cors|sl ol lre
lurcl|ora||y deperderl allr|oules (Wr|cr are rol pr|re
allr|oules) ard lre|r delerr|rarls.
oyce-6odd Norma| Form (6NF} - cont
ols1A(Properly_l0#,Courly_Nare,ol#,Area)
ols18(Area, Pr|ce)
ols2(Courly_Nare,Tax_Rale)
0|ver:ols are |ocaled |r or|y 2 courl|es: Var|or ard |oerly
ol s|zes |r Var|or are: 0.5, 0., 0.Z, 0.8, 0.9 ard 1.0
ol s|zes |r |oerly are: 1.1, 1.2, . 2.0 acres
Trus: lrere are or|y 1 poss|o|e area va|ues
Furcl|ora| 0eperdercy: rea F6ounty_Name
wr|cr v|o|ales 8CNF |r ols1A oecause Area |s rol a super|ey ol ols1A.
loWever, |l sal|sl|es 3NF |r ols1A oecause Courly_Nare |s a pr|re allr|oule
(cord|l|or 2 ol 3NF).
ols1A car oe lurlrer decorposed |rlo:
ols1AX(Properly_l0#,ol#,Area)
ols1AZ(Area, Courly_Nare)
amp|e of oyce-6odd Norma| Form (6NF}
MOTVATON FOR 4NF
1here are sohemas that are in BCNl that do not
seem to be suffioiently normalized
name street
$tars
.ty ttle year
C. Fisher
C. Fisher
C. Fisher
C. Fisher
C. Fisher
123 Maple Str.
5 Locust Ln.
123 Maple Str.
5 Locust Ln.
123 Maple Str.
5 Locust Ln. C. Fisher
Hollywood
Malibu
Hollywood
Malibu
Hollywood
Malibu
Star Wars 1977
Star Wars 1977
Empire Strikes Back 1980
Empire Strikes Back 1980
Return of the Jedi 1983
Return of the Jedi 1983
ATTRBUTE NDEPENDENCE
No reason to assooiate address with one movie
and not another
when we repeat address and movie faots in all
oombinations, there is obvious redundanoy
owever, N0 BCNl violation in $9,78 relation
1here are no non-trivial lU's at all, all five attributes
form the only superkey
MULT-VALUED DEPENDENCY
Uefinition: Multivalued dependenoy (MvU):
Let l be a relation sohema and let and (subsets). 1he
multivalued dependenoy o ->-> [ holds on l if, in any legal
relation l, for all pairs of tuples r
1
and r
2
in l suoh that r
1
[o] =
r
2
[o], there exist tuples r
3
and r
4
in r suoh that
r
1
[o] = r
2
[o] = r
3
[o] = r
4
[o]
r
3
[[] = r
1
[[]
r
3
[l [] = r
2
[l []
r
4
[[] = r
2
[[]
r
4
[l [] = r
1
[l []
MVD RULES
Lvery lU is an MvU.
lf ->, then swapping 's between two tuples
that agree on doesn't ohange the tuples.
1herefore, the new" tuples are surely in the
relation, and we know ->->.
0omp|emenrar|on : lf ->->, and is all the
other attributes, then ->->.
SPLTTNG DOESN'T HOLD
Like lU's, we oannot generally split the left side
of an MvU.
But unlike lU's, we oannot split the right side
either --- sometimes you have to leave several
attributes on the right side.
EXAMPLE: NAME FF STREET
CTY
name street
$9,78
.ty ttle year
C. Fisher
C. Fisher
C. Fisher
123 Maple Str.
123 Maple Str.
5 Locust Ln.
Hollywood
Hollywood
Malibu
Star Wars 1977
Empire Strikes Back 1980
Empire Strikes Back 1980
C. Fisher
C. Fisher
5 Locust Ln.
123 Maple Str.
5 Locust Ln. C. Fisher
Malibu
Hollywood
Malibu
Star Wars 1977
Return of the Jedi 1983
Return of the Jedi 1983
t
u
v
EXAMPLE: NAME FF STREET
CTY
name street
$9,78
.ty ttle year
C. Fisher
C. Fisher
C. Fisher
C. Fisher
123 Maple Str.
5 Locust Ln.
123 Maple Str.
5 Locust Ln.
Hollywood
Malibu
Hollywood
Malibu
Star Wars 1977
Star Wars 1977
Empire Strikes Back 1980
Empire Strikes Back 1980
C. Fisher 123 Maple Str.
5 Locust Ln. C. Fisher
Hollywood
Malibu
Return of the Jedi 1983
Return of the Jedi 1983
u
t
w
v
4NF DEFNTON
A relation l is in 4Nl if whenever ->->
is a nontrivial MvU, then is a superkey.
Nontrivial means that:
. is not a subset of , and
. and are not, together, all the attributes.
Note that the definition of superkey" still
depends on lU's only.
BCNF VERSUS 4NF
Remember that every lU -> is also an
MvU, ->->.
1hus, if l is in 4Nl, it is oertainly in BCNl.
Beoause any BCNl violation is a 4Nl
violation.
But l oould be in BCNl and not 4Nl,
beoause MvU's are invisible" to BCNl.
DECOMPOSTON AND 4NF
lf ->-> is a 4Nl violation for relation l, we
oan deoompose l using the same teohnique
as for BCNl.
. is one of the deoomposed relations.
2. All but - is the other.
EXAMPLE
Urinkers(name, addr, phones, beersLiked)
lU: name -> addr
MvU's: name ->-> phones
name ->-> beersLiked
ey is name, phones, beersLiked}.
All dependenoies violate 4Nl.
EXAMPLE, CONTNUED
Ueoompose using name -> addr:
1. Urinkers1(name, addr)
ln 4Nl, only dependenoy is name -> addr.
2. Urinkers2(name, phones, beersLiked)
Not in 4Nl. MvU's name ->-> phones and
name ->-> beersLiked apply. No lU's, so all
three attributes form the key.
EXAMPLE: DECOMPOSE DRNKERS2
Lither MvU name ->-> phones or name ->->
beersLiked tells us to deoompose to:
Urinkers3(name, phones)
Urinkers4(name, beersLiked)

Anda mungkin juga menyukai