functional dependency in dbms
TRANSCRIPT
-
8/13/2019 Functional Dependency in DBMS
1/57
FUNCTIONAL D EPENDENCIES AND S CHEMA R EFINEMENT
Science is the knowledge ofconsequences, and dependenceof one fact upon another.
Thomas Hobbes(1588-1679 )
-
8/13/2019 Functional Dependency in DBMS
2/57
R EVIEW: D ATABASE DESIGN
Requirements Analysis user needs; what must database do?
Conceptual Design
high level descr (often done w/ER model)Logical Design
translate ER into DBMS data model
Schema Refinement consistency,normalizationPhysical Design - indexes, disk layoutSecurity Design - who accesses what
-
8/13/2019 Functional Dependency in DBMS
3/57
FUNCTIONAL DEPENDENCIES (FDS)
-
8/13/2019 Functional Dependency in DBMS
4/57
FUNCTIONAL DEPENDENCIES (FDS)
-
8/13/2019 Functional Dependency in DBMS
5/57
FUNCTIONAL DEPENDENCIES (FDS)
A functional dependency X Y holds overrelation schema R if, for every allowableinstance r of R:
t1 r, t2 r, p X (t1 ) = p X (t2 )implies p Y (t1 ) = p Y (t2 )(where t1 and t2 are tuples;X and Y are sets of attributes)
In other words: X Y meansGiven any two tuples in r , if the X valuesare the same, then the Y values must alsobe the same. (but not vice versa)
-
8/13/2019 Functional Dependency in DBMS
6/57
EXAMPLE: CONSTRAINTS ON ENTITY SET
Consider relation obtained from Hourly_Emps:Hourly_Emps ( ssn, name, lot, rating, wage_per_hr ,hrs_per_wk )
We sometimes denote a relation schema by listing the
attributes: e.g., SNLRWHThis is really the set of attributes {S,N,L,R,W,H}.Sometimes, we refer to the set of all attributes of arelation by using the relation name. e.g.,
Hourly_Emps
for SNLRWH
What are some FDs on Hourly_Emps (Given)?ssn is the key: S SNLRWHrating determines wage_per_hr : R W
lot determines lot : L L (
trivial
dependnency)
-
8/13/2019 Functional Dependency in DBMS
7/57
F UNCTIONAL D EPENDENCIES E XAMPLE
For Student(name, matric, dob)
we write:{matric} {name, dob}
If two tuples in the relation student have the same matricnumber then they must be the same tuple ( no duplicate )
-
8/13/2019 Functional Dependency in DBMS
8/57
F UNCTIONAL D EPENDENCIES E XAMPLE
For Student(name, matric, dob)
we write:{matric, dob} {name}
-
8/13/2019 Functional Dependency in DBMS
9/57
F UNCTIONAL D EPENDENCIES T YPE
Trivial FDsNon-Trivial FDsCompletely Non-Trivial FDs
-
8/13/2019 Functional Dependency in DBMS
10/57
TRIVIAL FD S
X Y
Y X
Eg. {name, address} {name}
-
8/13/2019 Functional Dependency in DBMS
11/57
N ON -T RIVIAL FD S
X Y Y X
{name, address} {dob} {name, address} {name, dob}
-
8/13/2019 Functional Dependency in DBMS
12/57
COMPLETELY N ON -T RIVIAL FD S
X Y
Y X =
{name, address} {dob}
-
8/13/2019 Functional Dependency in DBMS
13/57
S UPERKEYS
A set of attributes whose knowledge determinesthe value of the entire tuple is a superkey
Example:Student(NRIC, matric, name, dob, )
e.g.
superkey {matric, name, dob}superkey {NRIC, name}
-
8/13/2019 Functional Dependency in DBMS
14/57
C ANDIDATE K EYS
A minimal set of attributes whose knowledgedetermines the value of the entire tuple is acandidate key
Example:Student(NRIC, matric, name, dob, )
e.g. candidate key {matric}e.g. candidate key {NRIC}
-
8/13/2019 Functional Dependency in DBMS
15/57
P RIMARY K EYS
The designer chooses a minimal set of attributeswhose knowledge determines the value of theentire tuple to be the primary key
-
8/13/2019 Functional Dependency in DBMS
16/57
REASONING ABOUT F UNCTIONAL DEPENDENCIES
It is sometimes possible to infer new functionaldependencies from a set of given functionaldependencies
-
8/13/2019 Functional Dependency in DBMS
17/57
REASONING ABOUT F UNCTIONAL DEPENDENCIES
For example:
From
{ssn } {first_name}and
{ssn} {last_name}
We can infer{ssn } {first_name, last_name}
-
8/13/2019 Functional Dependency in DBMS
18/57
A RMSTRONG S A XIOMS
Be X, Y, Z be subset of the relation scheme of arelation R
Reflexivity :If Y X, then X Y
Augmentation :If X Y , then X ZY Z
Transitivity :If XY and YZ, then XZ
-
8/13/2019 Functional Dependency in DBMS
19/57
A RMSTRONG S A XIOMS
-
8/13/2019 Functional Dependency in DBMS
20/57
A RMSTRONG S A XIOMS
-
8/13/2019 Functional Dependency in DBMS
21/57
A RMSTRONG S A XIOMS
-
8/13/2019 Functional Dependency in DBMS
22/57
A RMSTRONG S A XIOMS
-
8/13/2019 Functional Dependency in DBMS
23/57
A RMSTRONG S A XIOMS
-
8/13/2019 Functional Dependency in DBMS
24/57
A RMSTRONG S A XIOMS
For a relation scheme:employee(name, office_number,telephone_number, age)
Reflexivity :{name} {name,office_number}, then{name,office_number }{name}
-
8/13/2019 Functional Dependency in DBMS
25/57
A RMSTRONG S A XIOMS
For a relation scheme:employee(name, office_number,telephone_number, age)
Augmentation :{name} { office_number} , then{name,age }{office_number,age}
-
8/13/2019 Functional Dependency in DBMS
26/57
A RMSTRONG S A XIOMS
For a relation scheme:employee(name, office_number,telephone_number, age)
Transitivity :{name}{ telephone_number} and{telephone_number }{office_number},then {name}{ office_number}
-
8/13/2019 Functional Dependency in DBMS
27/57
A RMSTRONG S A XIOMS
Theorem: Armstrongs axioms are a sound and
complete set of inference rules
Sound : all Armstrong axioms are valid (correct / hold)Complete : all fds that are entailed can be deducedwith the help of the Armstrong axioms
How to:Prove the soundness?
27
-
8/13/2019 Functional Dependency in DBMS
28/57
A RMSTRONG S A XIOMS Armstrongs axioms are sound
for example: TransitivityLet X, Y, Z be subsets of the relation R
If XY and YZ, then XZ
Proof:
if two tuples T1 and T2 of |R| are such that, for allattributes Ax in X, T1. Ax = T2.Ax,
since XY then for all Ay in Y, T1. Ay = T2.Ay since YZ and for all Ay in Y, T1. Ay = T2.Ay then
for all Az in Z, T1. Az = T2.Az
-
8/13/2019 Functional Dependency in DBMS
29/57
A RMSTRONG S A XIOMS
Armstrongs axioms are sound
For example: Consider the scheme {name, room,tel} with the set of functional dependencies:
{{room} { tel}, {tel } {name}}
Then we can deduct that the following functionaldependency hold:
{room} {name}
Proof:By Transitivity
-
8/13/2019 Functional Dependency in DBMS
30/57
A RMSTRONG S A XIOMS
Armstrongs axioms are sound
For example: Weak-AugmentationLet X, Y, Z be subsets of the relation R
If X Y , then X ZY
ProofIf X Y
(1) Then by Augmentation X ZY Z(2) And by Reflexivity Y Z Y because Y Y Z(3) Then by Transitivity of (1) and (2) we have X ZY
Q.E.D.
-
8/13/2019 Functional Dependency in DBMS
31/57
-
8/13/2019 Functional Dependency in DBMS
32/57
CLOSURE OF A S ET OF F UNCTIONAL DEPENDENCIES : E XAMPLE
Consider the relation scheme R(A,B,C,D)
F = {{A} {B},{B,C} {D}}
F + = {{A} {A}, {B}{B}, {C}{C}, {D}{D},[],{A}{B}, {A,B}{B}, {A,D}{B,D},{ A,C}{B,C},{A,C,D}{B,C,D}, {{A} {A,B },{ A,B}{A,B},{A,D}{A,B,D}, {A,C}{A,B,C },{ A,C,D}{A,B,C,D}, {B,C} {D}, [], {A,C} {D},[]}
-
8/13/2019 Functional Dependency in DBMS
33/57
E QUIVALENCE OF S ETS OF F UNCTIONAL DEPENDENCIES
Two sets of functional dependencies F and G areequivalent if and only if
F+ = G+
-
8/13/2019 Functional Dependency in DBMS
34/57
A RMSTRONG S A XIOMS
Armstrongs axioms are complete
-
8/13/2019 Functional Dependency in DBMS
35/57
35
BOGUS RULES
Disprove that if X and Y are sets of attributes ofa relational schema R, and the fd X Y holdsin R, then Y X also holds in R.
Disprove that if X, Y and Z are sets of attributesof a relational schema R, and the fds X Yand Y Z hold in R, then Z X also holds inR.
Disprove that if X, Y and Z are sets of attributesof a relational schema R, and the fd XY Zholds in R, then X YZ also holds in R.
-
8/13/2019 Functional Dependency in DBMS
36/57
36
BOGUS RULES
Disprove that if X and Y are sets ofattributes of a relational schema R, and thefd X Y holds in R, then Y X also holdsin R.
Solution:Consider the following relation instance,where we use singletons for X and Y:
We see that X Y holds, but not Y XX Y
1 0
0 0
-
8/13/2019 Functional Dependency in DBMS
37/57
37
BOGUS RULES
Disprove that if X, Y and Z are sets ofattributes of a relational schema R, and thefds X Y and Y Z hold in R, then Z Xalso holds in R.
Solution:Consider the following relation instance, wherewe use singletons for X, Y, and Z: We see thatboth X Y and Y Z hold But not Z X.
X Y Z
0 0 0
1 0 0
-
8/13/2019 Functional Dependency in DBMS
38/57
38
BOGUS RULES
Disprove that if X, Y and Z are sets ofattributes of a relational schema R, andthe fd XY Z holds in R, then X YZalso holds in R.
Solution:Consider the following relationinstance where we use singletons for X,
Y, and Z: We see that XY Z holds,but not X YZ.
X Y Z
0 0 0
0 1 0
-
8/13/2019 Functional Dependency in DBMS
39/57
F INDING K EYS : E XAMPLE
Example: Consider the relation schemeR(A,B,C,D)
with functional dependencies{ A}{C} and {B}{D}.
Is {A,B} a candidate key?
-
8/13/2019 Functional Dependency in DBMS
40/57
F INDING K EYS : E XAMPLE
Example: {A,B} is a superkey.Indeed from Armstrongs Axioms we can infer:
{ A}{C} {A,B}{A,B,C} (augmentation by AB){B}{D} {A,B,C}{A,B,C,D} (augmentation by
A,B,C)We obtain {A,B}{A,B,C,D} (transitivity)
-
8/13/2019 Functional Dependency in DBMS
41/57
-
8/13/2019 Functional Dependency in DBMS
42/57
CLOSURE OF A S ET OF A TTRIBUTES
For a set A of attributes,we call the closure of A (with respect to a set of
functional dependencies F) , noted A+, the maximum setof attributes such that AA+ (as a consequence of F)
-
8/13/2019 Functional Dependency in DBMS
43/57
CLOSURE OF A S ET OF A TTRIBUTES :E XAMPLE
Consider the relation scheme R(A,B,C,D) withfunctional dependencies
{A }{C} and {B}{D }.
{A}+ = {A,C}
{B}+ = {B,D}
{A,B}+ = {A,B,C,D}
-
8/13/2019 Functional Dependency in DBMS
44/57
CLOSURE OF A S ET OF A TTRIBUTES : A LGORITHM 1
Input:R a relation schemeF a set of functional dependenciesX R
Output:X+ the closure of X w.r.t. F
-
8/13/2019 Functional Dependency in DBMS
45/57
CLOSURE OF A S ET OF A TTRIBUTES : A LGORITHM 1
-
8/13/2019 Functional Dependency in DBMS
46/57
CLOSURE OF A S ET OF A TTRIBUTES :E XAMPLE
R = {A,B,C,D,E,G}F = { {A,B}{C}, {C}{A}, {B,C}{D },{ A,C,D}{B}, {D}{E,G}, {B,E}{C },{C,G}{B,D}, {C,E}{A,G}} X = {B,D}
-
8/13/2019 Functional Dependency in DBMS
47/57
CLOSURE OF A S ET OF A TTRIBUTES :E XAMPLE
-
8/13/2019 Functional Dependency in DBMS
48/57
A TTRIBUTE CLOSURE ( EXAMPLE)
R = {A, B, C, D, E}F = { B CD, D E, B A, E C, AD B }Is B E in F + ?B+ = BB+ = BCDB+ = BCDAB+ = BCDAE Yes!
and B is a key for R too! Is D a key for R?D+ = DD+ = DED+ = DEC
Nope!
Is AD a key for R? AD+ = AD
AD+ = ABD and B is a key, so Yes!
Is AD a candidate keyfor R? A + = A
A not a key, nor is D so Yes! Is ADE a candidate key
for R?No! AD is a key, so ADE is a
superkey, but not a cand. key
-
8/13/2019 Functional Dependency in DBMS
49/57
-
8/13/2019 Functional Dependency in DBMS
50/57
A TTRIBUTE CLOSURE ( EXAMPLE)
-
8/13/2019 Functional Dependency in DBMS
51/57
E QUIVALENCE OF S ETS OF F UNCTIONAL DEPENDENCIES
Given two sets of functional dependencies F and G
Option 1Generate FD closures for both Set and Check, F+ = G+
Option 2 For each functional dependency YZ in F
Compute Y+ with respect to GCheck whether Z Y+If yes then YZ is in G +
And vice versa
-
8/13/2019 Functional Dependency in DBMS
52/57
M INIMAL S ET OF D EPENDENCIES
A set of dependencies F is minimal if andonly if:
1. Every right-hand side is a single attribute
2. For no XA in F and proper subset Z of X is F {XA} {ZA} equivalent to F
3. For no X A in F is the set F {XA } equivalent toF
-
8/13/2019 Functional Dependency in DBMS
53/57
-
8/13/2019 Functional Dependency in DBMS
54/57
M INIMAL C OVER : E XAMPLE
F = { {A,B}{C}, {C}{A}, {B,C}{D }, { A,C,D}{B},{D}{E,G}, {B,E}{C }, {C,G}{B,D}, {C,E}{A,G}}
-
8/13/2019 Functional Dependency in DBMS
55/57
M INIMAL C OVER : E XAMPLE (1)
-
8/13/2019 Functional Dependency in DBMS
56/57
M INIMAL C OVER : E XAMPLE (2)
-
8/13/2019 Functional Dependency in DBMS
57/57
M INIMAL C OVER : E XAMPLE (3)