0% found this document useful (0 votes)
4 views46 pages

Understanding Relational Normalization

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views46 pages

Understanding Relational Normalization

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Relational Normalization

Theory

1
Limitations of E-R Designs
• Provides a set of guidelines, does not result
in a unique database schema
• Does not provide a way of evaluating
alternative schemas
• Normalization theory provides a mechanism
for analyzing and refining the schema
produced by an E-R design

2
Redundancy
• Dependencies between attributes cause
redundancy
– Eg. All addresses in the same town have the
same zip code

SSN Name Town Zip


1234 Joe Stony Brook 11790 Redundancy

4321 Mary Stony Brook 11790


5454 Tom Stony Brook 11790
………………….

3
Example
ER Model
SSN Name Address Hobby
1111 Joe 123 Main {biking, hiking}

Relational Model
SSN Name Address Hobby
1111 Joe 123 Main biking
1111 Joe 123 Main hiking
…………….
Redundancy
4
Anomalies
• Redundancy leads to anomalies:
– Update anomaly: A change in Address must be
made in several places
– Deletion anomaly: Suppose a person gives up
all hobbies. Do we:
• Set Hobby attribute to null? No, since Hobby is part
of key
• Delete the entire row? No, since we lose other
information in the row
– Insertion anomaly: Hobby value must be
supplied for any inserted row since Hobby is
part of key
5
Decomposition
• Solution: use two relations to store Person
information
– Person1 (SSN, Name, Address)
– Hobbies (SSN, Hobby)
• The decomposition is more general: people
with hobbies can now be described
• No update anomalies:
– Name and address stored once
– A hobby can be separately supplied or
deleted
6
Normalization Theory
• Result of E-R analysis need further
refinement
• Appropriate decomposition can solve
problems
• The underlying theory is referred to as
normalization theory and is based on
functional dependencies (and other kinds,
like multi-valued dependencies)

7
Functional Dependencies
• Definition: A functional dependency (FD) on a
relation schema R is a constraint X → Y, where X
and Y are subsets of attributes of R.
• An FD X → Y is satisfied in an instance r of R if
for every pair of tuples, t and s: if t and s agree on
all attributes in X then they must agree on all
attributes in Y
– Key constraint is a special kind of functional
dependency: all attributes of relation occur on the
right-hand side of the FD:
• SSN → SSN, Name, Address

8
Functional Dependencies
(Examples)
• Address → ZipCode
– Stony Brook’s ZIP is 11733
• ArtistName → BirthYear
– Picasso was born in 1881
• Autobrand → Manufacturer, Engine type
– Pontiac is built by General Motors with gasoline engine
• Author, Title → PublDate
– Shakespeare’s Hamlet published in 1600

9
Functional Dependency - Example
• Brokerage firm allows multiple clients to share an
account, but each account is managed from a single
office and a client can have no more than one account in
an office
– HasAccount (AcctNum, ClientId, OfficeId)
• keys are (ClientId, OfficeId), (AcctNum, ClientId)
– Client, OfficeId → AcctNum
– AcctNum → OfficeId
• Thus, attribute values need not depend only on key values

10
Entailment, Closure, Equivalence
• Definition: If F is a set of FDs on schema R and f is
another FD on R, then F entails f if every instance r of
R that satisfies every FD in F also satisfies f
– Ex: F = {A → B, B→ C} and f is A → C
• If Streetaddr → Town and Town → Zip then Streetaddr → Zip
• Definition: The closure of F, denoted F+, is the set of
all FDs entailed by F
• Definition: F and G are equivalent if F entails G and G
entails F

11
Armstrong’s Axioms for FDs
• This is the syntactic way of computing/testing
the various properties of FDs

• Reflexivity: If Y  X then X → Y (trivial FD)


– Name, Address → Name
• Augmentation: If X → Y then X Z→ YZ
– If Town → Zip then Town, Name → Zip, Name
• Transitivity: If X → Y and Y → Z then X → Z

12
Derived inference rules
• Union: if X→Y and X→Z, then X→YZ.
• Decomposition: if X→YZ, then X→Y and X→Z.
• Pseudotransitivity: if X→Y and WY→Z, then
WX→Z.

• These additional rules are not essential; their soundness


can be proved using Armstrong’s Axioms.
• Exercise: Prove rules Decomposition and
Pseudotransitivity using A.A.

13
Generating F+

AB→ C
union AB→ BCD decomp
aug
A→ D AB→ BD trans AB→ BCDE AB→ CDE
aug
D→ E BCD → BCDE

Thus, AB→ BD, AB → BCD, AB → BCDE, and AB → CDE


are all elements of F+

14
Attribute Closure
• Calculating attribute closure leads to a more
efficient way of checking entailment
• The attribute closure of a set of attributes, X,
with respect to a set of functional dependencies,
F, (denoted X+F) is the set of all attributes, A,
such that X → A
– X +F1 is not necessarily the same as X +F2 if F1  F2
• Attribute closure and entailment:
– Algorithm: Given a set of FDs, F, then X → Y if and
only if X+F  Y
15
Example - Computing Attribute Closure

X XF+

F: AB → C A {A, D, E}
A→D AB {A, B, C, D, E}
D→E (Hence AB is a key)
AC → B
B {B}
Is AB → E entailed by F? D {D, E}
Yes
Is D→ C entailed by F?
No
Result: XF+ allows us to determine FDs
of the form X → Y entailed by F
16
Computation of Attribute Closure X+F

closure := X; // since X  X+F


repeat
old := closure;
if there is an FD Z → V in F such that
Z  closure and V  closure
then closure := closure  V
until old = closure
– If T  closure then X → T is entailed by F
17
Example: Computation of Attribute
Closure
Problem: Compute the attribute closure of AB with
respect to the set of FDs : AB → C (a)
A→D (b)
D → E (c)
AC → B (d)
Solution:
Initially closure = {AB}
Using (a) closure = {ABC}
Using (b) closure = {ABCD}
Using (c) closure = {ABCDE}

18
Normal Forms
• Each normal form is a set of conditions on a schema
that guarantees certain properties (relating to
redundancy and update anomalies)
• First normal form (1NF) is the same as the definition
of relational model (relations = sets of tuples; each
tuple = sequence of atomic values)
• Second normal form (2NF) – a research lab accident;
has no practical or theoretical value – won’t discuss
• The two commonly used normal forms are third
normal form (3NF) and Boyce-Codd normal form
(BCNF)
19
BCNF
• Definition: A relation schema R is in BCNF if
for every FD X→ Y associated with R either
– Y  X (i.e., the FD is trivial) or
– X is a superkey of R
• Example: Person1(SSN, Name, Address)
– The only FD is SSN → Name, Address
– Since SSN is a key, Person1 is in BCNF

20
(non) BCNF Examples
• Person (SSN, Name, Address, Hobby)
– The FD SSN → Name, Address does not satisfy
requirements of BCNF
• since the key is (SSN, Hobby)
• HasAccount (AccountNumber, ClientId, OfficeId)
– The FD AcctNum→ OfficeId does not satisfy BCNF
requirements
• since keys are (ClientId, OfficeId) and (AcctNum, ClientId)

21
Redundancy
• Suppose R has a FD A → B. If an instance has 2 rows with
same value in A, they must also have same value in B (=>
redundancy, if the A-value repeats twice)

redundancy
SSN → Name, Address
SSN Name Address Hobby
1111 Joe 123 Main stamps
1111 Joe 123 Main coins

• If A is a superkey, there cannot be two rows with same


value of A
– Hence, BCNF eliminates redundancy

22
Third Normal Form
• A relational schema R is in 3NF if for every
FD X→ Y associated with R either:
– Y  X (i.e., the FD is trivial); or
– X is a superkey of R; or BCNF

– Every A Y is part of some key of R conditions

• 3NF is weaker than BCNF (every schema


that is in BCNF is also in 3NF)

23
3NF Example

• HasAccount (AcctNum, ClientId, OfficeId)


– ClientId, OfficeId → AcctNum
• OK since LHS contains a key
– AcctNum → OfficeId
• OK since RHS is part of a key
• HasAccount is in 3NF but it might still contain
redundant information due to AcctNum → OfficeId
(which is not allowed by BCNF)

24
3NF Example
• HasAccount might store redundant data:

ClientId OfficeId AcctNum 3NF: OfficeId part of key


1111 Stony Brook 28315 FD: AcctNum → OfficeId
2222 Stony Brook 28315
3333 Stony Brook 28315 redundancy

• Decompose to eliminate redundancy:

ClientId AcctNum OfficeId AcctNum


1111 28315 Stony Brook 28315
2222 28315
3333 28315 BCNF: AcctNum is key
FD: AcctNum → OfficeId
BCNF (only trivial FDs)
25
(Non) 3NF Example
• Person (SSN, Name, Address, Hobby)
– (SSN, Hobby) is the only key.
– SSN→ Name violates 3NF conditions
since Name is not part of a key and SSN
is not a superkey

26
Decompositions
• Goal: Eliminate redundancy by
decomposing a relation into several
relations in a higher normal form
• Decomposition must be lossless: it must be
possible to reconstruct the original relation
from the relations in the decomposition
• We will see why

27
Decomposition
• Schema R = (R, F)
– R is a set of attributes
– F is a set of functional dependencies over R
• The decomposition of schema R is a collection of
schemas Ri = (Ri, Fi) where
– R = i Ri for all i (no new attributes)
– Fi is a set of functional dependences involving only
attributes of Ri
– F entails Fi for all i (no new FDs)
• The decomposition of an instance, r, of R is a set
of relations ri = Ri(r) for all i

28
Example Decomposition
Schema (R, F) where
R = {SSN, Name, Address, Hobby}
F = {SSN→ Name, Address}
can be decomposed into
R1 = {SSN, Name, Address}
F1 = {SSN → Name, Address}
and
R2 = {SSN, Hobby}
F2 = { }
29
Lossless Schema Decomposition

• A decomposition should not lose information


• A decomposition (R1,…,Rn) of a schema, R, is
lossless if every valid instance, r, of R can be
reconstructed from its components:

r = r1 r2 …… rn

where each ri = Ri(r)


30
Lossy Decomposition
The following is always the case (Think why?):
r  r1 r2 ... rn
But the following is not always true:
r  r1 r2 ... rn
Example: r  r1 r2
SSN Name Address SSN Name Name Address
1111 Joe 1 Pine 1111 Joe Joe 1 Pine
2222 Alice 2 Oak 2222 Alice Alice 2 Oak
3333 Alice 3 Pine 3333 Alice Alice 3 Pine
The tuples (2222, Alice, 3 Pine) and (3333, Alice, 2 Oak) are in the join,
but not in the original
31
Lossy Decompositions:
What is Actually Lost?
• In the previous example, the tuples (2222, Alice, 3
Pine) and (3333, Alice, 2 Oak) were gained, not lost!
– Why do we say that the decomposition was lossy?

• What was lost is information:


– That 2222 lives at 2 Oak: In the decomposition, 2222 can
live at either 2 Oak or 3 Pine
– That 3333 lives at 3 Pine: In the decomposition, 3333 can
live at either 2 Oak or 3 Pine

32
Testing for Losslessness
• A (binary) decomposition of R = (R, F)
into R1 = (R1, F1) and R2 = (R2, F2) is
lossless if and only if :
– either the FD
• (R1  R2 ) → R1 is in F+
– or the FD
• (R1  R2 ) → R2 is in F+

33
Example
Schema (R, F) where
R = {SSN, Name, Address, Hobby}
F = {SSN → Name, Address}
can be decomposed into
R1 = {SSN, Name, Address}
F1 = {SSN → Name, Address}
and
R2 = {SSN, Hobby}
F2 = { }
Since R1  R2 = SSN and SSN → R1 the
decomposition is lossless

34
Dependency Preservation
• Consider a decomposition of R = (R, F) into R1 = (R1,
F1) and R2 = (R2, F2)
– An FD X → Y of F is in Fi iff X  Y  Ri
– An FD, f F may be in neither F1, nor F2, nor even
(F1  F2)+
• Checking that f is true in r1 or r2 is (relatively) easy
• Checking f in r1 r2 is harder – requires a join
• Ideally: want to check FDs locally, in r1 and r2, and have
a guarantee that every f F holds in r1 r2
• The decomposition is dependency preserving iff the sets
F and F1  F2 are equivalent: F+ = (F1  F2)+
– Then checking all FDs in F, as r1 and r2 are updated, can be
done by checking F1 in r1 and F2 in r2
35
Dependency Preservation
• If f is an FD in F, but f is not in F1  F2,
there are two possibilities:
– f  (F1  F2)+
• If the constraints in F1 and F2 are maintained, f
will be maintained automatically.
– f  (F1  F2)+
• f can be checked only by first taking the join of r1
and r2. This is costly.

36
Example
Schema (R, F) where
R = {SSN, Name, Address, Hobby}
F = {SSN → Name, Address}
can be decomposed into
R1 = {SSN, Name, Address}
F1 = {SSN → Name, Address}
and
R2 = {SSN, Hobby}
F2 = { }
Since F = F1  F2 the decomposition is
dependency preserving

37
Example
• Schema: R= (ABC; F) , F = {A → B, B→ C, C→ B}
• Decomposition:
– (AC, F1), F1 = {A→C}
• Note: A→C  F, but in F+
– (BC, F2), F2 = {B→ C, C→ B}

• A → B  (F1  F2), but A → B  (F1  F2)+.


– So F+ = (F1  F2)+ and thus the decompositions is still
dependency preserving

38
Example
• HasAccount (AccountNumber, ClientId, OfficeId)
f1: AccountNumber → OfficeId
f2: ClientId, OfficeId → AccountNumber
• Decomposition:
AcctOffice = (AccountNumber, OfficeId; {AccountNumber → OfficeId})
AcctClient = (AccountNumber, ClientId; {})
• Decomposition is lossless: R1  R2= {AccountNumber} and
AccountNumber → OfficeId
• In BCNF
• Not dependency preserving: f2  (F1  F2)+
• HasAccount does not have BCNF decompositions that are both
lossless and dependency preserving! (Check, eg, by enumeration)
• Hence: BCNF+lossless+dependency preserving decompositions
are not always achievable!
39
BCNF Decomposition Algorithm
Input: R = (R; F)
Decomp := R
while there is S = (S; F’)  Decomp and S not in BCNF do
Find X → Y  F’ that violates BCNF // X isn’t a superkey in S
Replace S in Decomp with S1 = (XY; F1), S2 = (S - (Y - X); F2)
// F1 = all FDs of F’ involving only attributes of XY
// F2 = all FDs of F’ involving only attributes of S - (Y - X)
end
return Decomp

40
Example
Given: R = (R; T) where R = ABCDEFGH and
T = {ABH→ C, A→ DE, BGH→ F, F→ ADH, BH→ GE}
step 1: Find a FD that violates BCNF
Not ABH → C since (ABH)+ includes all attributes
(BH is a key)
A → DE violates BCNF since A is not a superkey (A+ =ADE)
step 2: Split R into:
R1 = (ADE, {A→ DE })
R2 = (ABCFGH; {ABH→ C, BGH→ F, F→ AH , BH→ G})
Note 1: R1 is in BCNF
Note 2: Decomposition is lossless since A is a key of R1.
Note 3: FDs F → D and BH → E are not in T1 or T2. But
both can be derived from T1 T2
(E.g., F→ A and A→ D implies F→ D)
Hence, decomposition is dependency preserving.
41
Example (con’t)
Given: R2 = (ABCFGH; {ABH→C, BGH→F, F→AH, BH→G})
step 1: Find a FD that violates BCNF.
Not ABH → C or BGH → F, since BH is a key of R2
F→ AH violates BCNF since F is not a superkey (F+ =AH)
step 2: Split R2 into:
R21 = (FAH, {F → AH})
R22 = (BCFG; {})

Note 1: Both R21 and R22 are in BCNF.


Note 2: The decomposition is lossless (since F is a key of R21)
Note 3: FDs ABH→ C, BGH→ F, BH→ G are not in T21
or T22 , and they can’t be derived from T1  T21  T22 .
Hence the decomposition is not dependency-preserving

42
Properties of BCNF Decomposition
Algorithm

• A BCNF decomposition is not necessarily


dependency preserving
• But always lossless
• BCNF+lossless+dependency preserving is
sometimes unachievable (recall HasAccount)

43
Exercise
1) Consider the following table.
• Give an example of update anomaly, an example of deletion
anomaly and an example of insertion anomaly knowing that
– A product has many suppliers and can have many other products as a
substitute (i.e. a product can be replaced by its substitute).
– The purchase price is determined by a supplier for a product, while
the sale price is for a given product regardless of the supplier.
– The quantity is for a given product, again regardless of the supplier.

44
Solution
Update Anomaly:
- Changing the quantity of a product implies updating the
quantity for as many suppliers and substitutes there is for
the product.

Deletion Anomaly:
- By deleting the only substitute of a product, the whole
product entry needs to be removed.

Insertion Anomaly:
- We can’t add a substitute of a product if we do not know the
supplier of the product.

45
Exercise (cont.)
2) Give a schema of a decomposition that
avoids such anomalies.
• Solution:
Product(ProductID, Quantity, SalePrice)
Suppliers( ProductID, SupplierID, PurchasePrice)
Substitutes( ProductID, Substitute)

46

You might also like