IF3111 Basis Data – Functional Dependency
I F3111 Basis Data – Functional Dependency
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Wikan Danar Departemen Teknik I nformatika I nstitut Teknologi Bandung
Functional Dependencies • Constraints on the set of legal relations
- Require that the value for a certain set of attributes determines uniquely the value for another set of attributes.
- A functional dependency is a generalization of the notion of a key.
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
- Let R be a relation schema
- The functional dependency
α ]
7
3
4 1 5
1
→
→
B does NOT hold, but B
β ] • Example: Consider r(A,B) with the following instance of r.
2 [
β ] = t
1 [
⇒ t
2 [
α ] = t
1 [
β . That is, t
α , they also agree on the attributes
2 of r agree on the attributes
1 and t
α → β holds on R if and only if for any legal relations r(R), whenever any two tuples t
β ⊆ R
α ⊆ R and
Functional Dependencies (Cont.)
- On this instance, A
A does hold
IF3111 – Functional Dependency
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003 Functional Dependencies (Cont.)
- K is a superkey for relation schema R if and only if K R
→
- K is a candidate key for R if and only if
- – K R, and
→
- – for no K, R
α ⊂ α →
- Functional dependencies allow us to express constraints that cannot be expressed using superkeys. Consider the schema:
Loan -info-schema = (customer -name, loan -number, branch -name, amount).
We expect this set of functional dependencies to hold:
loan-number amount
→
loan-number branch -nam e
→ but would not expect the following to hold:
loan-number customer-nam e
→
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
- We use functional dependencies to:
- – test relations to see if they are legal under a given set of functional dependencies.
- I f a relation r is legal under a set F of functional dependencies, we say that r satisfies F.
- – specify constraints on the set of legal relations
- Note: A specific instance of a relation schema may satisfy a functional dependency even if the functional dependency does not hold on all legal instances.
- – For example, a specific instance of Loan-schema may, by chance, satisfy
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Use of Functional Dependencies
loan- number → customer-name.
- A functional dependency is trivial if it is satisfied by all instances of a relation – E.g.
- customer-name, loan- number
- customer-name
- – I n general,
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Functional Dependencies (Cont.)
→ customer- name
→
customer-name
α → β is trivial if
β ⊆ α
- Given a set F set of functional dependencies, there are certain other functional dependencies that are logically implied by F.
- – E.g. I f A
- The set of all functional dependencies logically implied by F is the closure of F.
- We denote the closure of F by F
- + .
- + by applying Armstrong’s Axioms:
- We can find all of F
- – if
- – if
- – if
- These rules are
- – sound (generate only functional dependencies that actually hold)
α → γ ( transitivity)
, then
β → γ
, and
α → β
γ α → γ β ( augmentation)
, t hen
α → β
α → β ( reflexivity)
β ⊆ α
, then
C
→
C, then we can infer that A
→
B and B
→
Closure of a Set of Functional Dependencies
IF3111 – Functional Dependency
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
and – complete (generate all functional dependencies that hold).
- R = (A, B, C, G, H, I )
- – A
- some members of F
- by transitivity from A
- – AG
- by augmenting A
- – CG
- from CG
- – definition of functional dependencies, or
- – Augmentation of CG
C with G, to get AG
→
CG and then transitivity with CG
→
I
→
HI
→
H and CG
→
I : “union rule” can be inferred from
→ I t o infer CG
→
CGI , augment at ion ofCG →
H t o infer CGI →
→
I
→
H CG →
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Example
F = { A →
B A →
C CG →
I B →
H
H}
→
H
→
B and B
→
HI , and t hen t ransit ivit y
- To compute the closure of a set of functional dependencies F:
- = F
- apply reflexivity and augmentation rules on f add the resulting functional dependencies to F
- for each
- if f
in F
F
add the resulting functional dependency to F
then
can be combined using transitivity
2
and f
1
2
and f
1
pair of functional dependencies f
repeat for each functional dependency f in F
F
Procedure for Computing F
- until
- does not change any further
IF3111 – Functional Dependency
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
NOTE: We will see an alternative procedure for this task later
- We can further simplify manual computation of F
- by using the following additional rules.
- – If
- – If
- – If
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Closure of Functional Dependencies (Cont.)
α → β holds and
α → γ holds, then
α → β γ holds ( union)
α → β γ holds, then
α → β holds and
α → γ holds
( decomposition)
α → β holds and
γ β → δ holds, then
α γ → δ holds
( pseudotransitivity) The above rules can be inferred from Armstrong’s axioms. Closure of Attribute Sets
- Given a set of attributes define the closure
α, of under F (denoted by
- ) as the set of attributes that are functionally determined by under F:
α α
α is in F
ó β ⊆ α
- α → β
- α α
- Algorithm to compute , the closure of under F
result := ; α w hile (changes to result) do
F for each in do
β → γ begin if result then result := result
β ⊆ ∪ γ end
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency Example of Attribute Set Closure
R = (A, B, C, G, H, I ) •
- B F = { A
→
A C
→
CG H
→
CG
I
→
B H}
→
- (AG)
1. result = AG 2. result = ABCG (A C and A
B)
→ →
3. result = ABCGH (CG H and CG AGBC)
→ ⊆
4. result = ABCGHI (CG I and CG AGBCH)
→ ⊆
- I s AG a candidate key?
1. I s AG a super key?
- 1. Does AG R? = = I s ( AG) R
→ ⊇
2. I s any subset of AG a superkey?
- 1. Does A R? = = I s (A) R
→ ⊇
- 2. Does G R? = = I s ( G) R
→ ⊇
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency Uses of Attribute Closure There are several uses of the attribute closure algorithm:
- Testing for superkey:
- ,
- – To test if is a superkey, we compute and check if
α α α contains all attributes of R.
- Testing functional dependencies
- – To check if a functional dependency holds (or, in
α → β β ⊆ α
- other words, is in F+ ), just check if .
- – That is, we compute
by using attribute closure, and then
α check if it contains . β
- – I s a simple and cheap test, and very useful
- Computing closure of F
- – For each R, we find the closure , and for each S ,
γ ⊆ γ ⊆ γ we output a functional dependency S.
γ →
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency Canonical Cover
- Sets of functional dependencies may have redundant dependencies that can be inferred from the others
- – Eg: A C is redundant in: { A
B, B
C, A C}
→ → → →
- – Parts of a functional dependency may be redundant
- E.g. on RHS: { A
B, B
C, A CD} can be simplified
→ → →
to { A
B, B
C, A D}
→ → →
- E.g. on LHS: { A
B, B
C, AC D} can be simplified
→ → →
to { A
B, B
C, A D}
→ → →
- I ntuitively, a canonical cover of F is a “ minimal” set of functional dependencies equivalent to F, having no redundant dependencies or redundant parts of dependencies
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
- Consider a set F of functional dependencies and the functional dependency
- – Attribute A is extraneous in
- – A)
- – Attribute A is extraneous in
→
(
β – A)} logically implies F.
→
→ C }
→
C because { A
→
C, AB
C} logically implies A
{
→
C (I .e. the result of dropping B from AB
→ C).
→
→ CD}
→
CD since AB
→
α →
∪
C can be inferred even after deleting C Testing if an Attribute is Extraneous
α → β
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Extraneous Attributes
α → β in F.
α
if A
∈ α
and F logically implies (F – {
} )
} )
∪
{ (
α
→ β } .
β
if A
∈ β
and the set of functional dependencies ( F – {
α → β
- Note: implication in the opposite direction is trivial in each of the cases above, since a “ stronger” functional dependency always implies a weaker one
- Example: Given F = { A
- – B is extraneous in AB
- Example: Given F = { A
- – C is extraneous in AB
- Consider a set F of functional dependencies and the functional dependency in F.
α → β
- To test if attribute A is extraneous in
∈ α α
- 1. compute ({ } – A) using the dependencies in F
α
- 2. check that ( { } – A) contains A; if it does, A is
α extraneous
- To test if attribute A is extraneous in
∈ β β α
- 1. compute using only the dependencies in F’ = ( F – { } ) { ( – A)} ,
α → β ∪ α → β 2. check that
- contains A; if it does, A is extraneous
α
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency Canonical Cover
- A canonical cover for F is a set of dependencies F such
c that
- – F logically implies all dependencies in F and
c,
- – F logically implies all dependencies in F, and
c
- – No functional dependency in F contains an extraneous
c
attribute, and – Each left side of functional dependency in F is unique.
c
- To compute a canonical cover for F:
repeat
Use the union rule to replace any dependencies in F and with
α → β α → β α → β β
1
1
1
1
1
1
2 Find a functional dependency with an
α → β
extraneous attribute either in or in
α β
I f an extraneous attribute is found, delete it from
α → β until F does not change
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency Note: Union rule may become applicable after some extraneous attributes have been deleted, so it has to be re -applied
- R = ( A, B, C)
- Combine A
- – Set is now { A
- A is extraneous in AB
- – Check if the result of deleting A from AB
- Yes: in fact, B
- – Set is now { A
- C is extraneous in A
- – Check if A
- Yes: using transitivity on A
- – Can use attribute closure of A in more complex cases
- The canonical cover is: A
→
C
→
C is implied by the other dependencies
→ C is already present!
→
BC, B
→
C}
BC
→
→
C is logically implied by A
→
B and the other dependencies
→ B and B
→
C.
→
B B
→
C}
C, AB
→
B AB
IF-ITB/WD dari Silberschatz, modifikasi TW /27 Okt 2003
IF3111 – Functional Dependency
Example of Computing a Canonical Cover
F = { A
→
BC B
→
C A
→
→
→
C}
→
BC and A
→
B into A
→
BC
→
BC, B
C