0% found this document useful (0 votes)
6 views52 pages

Normal Forms

The document discusses normal forms in database design, focusing on functional dependencies (FDs) and properties of decompositions. It explains the concepts of lossless join decompositions and dependency preservation, providing examples with the Hourly-Emps relation. The lecture aims to clarify the implications of decomposing relations and the importance of maintaining data integrity through proper normalization.

Uploaded by

csity6996
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)
6 views52 pages

Normal Forms

The document discusses normal forms in database design, focusing on functional dependencies (FDs) and properties of decompositions. It explains the concepts of lossless join decompositions and dependency preservation, providing examples with the Hourly-Emps relation. The lecture aims to clarify the implications of decomposing relations and the importance of maintaining data integrity through proper normalization.

Uploaded by

csity6996
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
You are on page 1/ 52

Normal Forms

Indrakshi Ray
Webpage: http://www.cs.colostate.edu/˜iray
Email: iray@cs.colostate.edu

©2011, Indrakshi Ray – p. 1/52


Lecture Objectives
Where are we now?
Chapter 7 of Silberschatz, Korth, and Sudarshan
Talked about functional dependencies (FDs)
What do we plan to cover today?
Continue discussion on FDs
Talk about properties of decompositions
Talk about Normal Forms

©2011, Indrakshi Ray – p. 2/52


Hourly-Emps Relation Example
Hourly-Emps(SSN, Name, Lot, Rating, HourlyWages,
HoursWorked)
We will denote the attributes as S, N , L, R, W , H respectively
What are the FDs of this schema?
S → S, N, L, R,W, H
R→W

©2011, Indrakshi Ray – p. 3/52


Hourly-Emps Example Contd.
Hourly-Emps Relation
S N L R W H
123223666 Tim 48 8 10 40
231315368 Jill 22 8 10 30
131243650 John 35 5 7 30
431243650 Jack 35 5 7 32
612244134 Tom 35 8 10 40

Problems are caused by the FD R → W


Update anomaly: can we just change W in the first tuple?
Insertion anomaly: what if we want to insert an employee
and don’t know the hourly wage for this rating?
Delete anomaly: if we delete all employees with rating 5,
we lose information about the wage for rating 5

Will 2 smaller tables be better?


©2011, Indrakshi Ray – p. 4/52
Problems with Decompositions
1. Some queries become more expensive
2. Given instances of the decomposed relations, we may not be
able to reconstruct the corresponding instance of the original
relation!
3. Checking some dependencies may require joining the
instances of the decomposed relations

©2011, Indrakshi Ray – p. 5/52


Non-Decomposed Relation
Hourly-Emps Relation
SSN Name Lot Rating HourlyWages HoursWorked
123223666 Tim 48 8 10 40
231315368 Jill 22 8 10 30
131243650 John 35 5 7 30
431243650 Jack 35 5 7 32
612244134 Tom 35 8 10 40

What is the HourlyWages of Tim?

©2011, Indrakshi Ray – p. 6/52


Decomposition 1
Hourly-Emps2 Relation
SSN Name Lot Rating HoursWorked
123223666 Tim 48 8 40
231315368 Jill 22 8 30
131243650 John 35 5 30
431243650 Jack 35 5 32
612244134 Tom 35 8 40
Wages Relation
Rating HourlyWages
8 10
5 7

What is the HourlyWages of Tim?


Why is it more expensive?

©2011, Indrakshi Ray – p. 7/52


Original Relation
Hourly-Emps Relation
SSN Name Lot Rating HourlyWages HoursWorked
123223666 Tim 48 8 10 40
231315368 Jill 22 8 10 30
131243650 John 35 5 7 30
431243650 Jack 35 5 7 32
612244134 Tom 35 8 10 40

Is it possible to decompose this relation and lose


information?
We call such a decomposition lossy
Next slide will give an example of a lossy decomposition

©2011, Indrakshi Ray – p. 8/52


Decomposition 2
Emps Relation
SSN Name Lot Rating
123223666 Tim 48 8
231315368 Jill 22 8
131243650 John 35 5
431243650 Jack 35 5
612244134 Tom 35 8
Hourly-Emps3 Relation
Rating HourlyWages HoursWorked
8 10 30
5 7 30
5 7 32
8 10 40

Is this decomposition lossy?

©2011, Indrakshi Ray – p. 9/52


Lossy Decomposition Contd.
Hourly-Emps Relation
SSN Name Lot Rating HourlyWages HoursWorked
123223666 Tim 48 8 10 30
123223666 Tim 48 8 10 40
231315368 Jill 22 8 10 30
231315368 Jill 22 8 10 40
131243650 John 35 5 7 30
131243650 John 35 5 7 32
431243650 Jack 35 5 7 30
431243650 Jack 35 5 7 32
612244134 Tom 35 8 10 30
612244134 Tom 35 8 10 40

Spurious tuples introduced by joining the decomposed


relations

©2011, Indrakshi Ray – p. 10/52


Loss-less Join Decompositions
Let R be a relation schema, F be a set of FDs on R
Suppose ρ is decomposed into relation schemas R1 , R2 , . . .,
Rk
We say that ρ is a loss-less join decomposition of R w.r.t. F if
for every relation r(R) satisfying F
r = ΠR1 (r) ✶ ΠR2 (r) ✶ . . . ✶ ΠRk (r)
Note that Decomposition 2 does not have this property as
the original relation had 5 tuples, but the relation formed by
joining the decomposed relation had 10 tuples

©2011, Indrakshi Ray – p. 11/52


Loss-less Join Decomposition
Let ρ = {R1 , R2 } be a decomposition of R and F be a set of
functional dependencies on R
ρ is loss-less w.r.t. F iff one of the following dependencies is
in F +
1. R1 ∩ R2 → R1 or
2. R1 ∩ R2 → R2
Does Decomposition 2 have this property?
R1 = (SSN, Name, Lot, Rating)
R2 = (Rating, HourlyWages, HoursWorked)
R1 ∩ R2 = Rating
Rating → Rating, HourlyWages

©2011, Indrakshi Ray – p. 12/52


Algorithm Chase
Input: A relation R containing n attributes A1 , A2 , . . ., An , a
decomposition of R in ρ = {R1 , R2 , . . . , Rk }, k < n and a set
of FDs on R
Output: Whether the decomposition is loss-less or not
Step 1:
1. Construct a tableau containing n columns (one
column for each attribute in R) and k rows (one row for
each element in ρ)
2. A row i in the tableau (corresponding to relation
scheme Ri has entry a j in the jth column iff Ri
contains attribute A j , otherwise row i has entry bi j in
the jth column

©2011, Indrakshi Ray – p. 13/52


Algorithm Chase Contd.
Step 2: Apply the following rule to the tableau until no more
changes can be made to the tableau
Let X → Y be a functional dependency in F
If the tableau has two rows that agree on all the X
columns, then we equate the symbols of the Y columns
if one of the equated symbols is a j , make the other
one a j
if they are bi j or bl j make them both either bi j or bl j
arbitrarily
Repeatedly consider all FDs until no further change is
possible
Step 3: If after modifying tableau as above we find a row with all
as, then the decomposition is lossless, otherwise not so

©2011, Indrakshi Ray – p. 14/52


Algorithm Chase Examples
R = {ABCDEF}
ρ = {ABC, AD, DEF}, that is, R1 = {ABC}, R2 = {AD}, and
R3 = {DEF}
F = {A → B, A → C, D → F, D → E}
Applying Chase’s Algorithm, we find that the decomposition
is lossless
Is Decomposition 1 of Hourly-Emps lossy or lossless?
Can you demonstrate using Chase’s Algorithm that
Decomposition 2 is lossy?

©2011, Indrakshi Ray – p. 15/52


Original Relation
Hourly-Emps Relation
SSN Name Lot Rating HourlyWages HoursWorked
123223666 Tim 48 8 10 40
231315368 Jill 22 8 10 30
131243650 John 35 5 7 30
431243650 Jack 35 5 7 32
612244134 Tom 35 8 10 40

Is it possible to decompose this relation and lose


dependency?
We call such a decomposition not dependency
preserving
Next slide will give an example of one such
decomposition ©2011, Indrakshi Ray – p. 16/52
Decomposition 3
Emps Relation
SSN Name Lot Rating
123223666 Tim 48 8
231315368 Jill 22 8
131243650 John 35 5
431243650 Jack 35 5
612244134 Tom 35 8
Hourly-Emps3 Relation
SSN HourlyWages HoursWorked
123223666 10 40
231315368 10 30
131243650 7 30
431243650 7 32
612244134 10 40

Is this decomposition lossy?


What happens to dependency Rating → HourlyWages?
©2011, Indrakshi Ray – p. 17/52
Set of Original FDs
FD1
1. SSN → Name
2. SSN → Lot
3. Lot → SSN
4. SSN → HoursWorked
5. SSN → Rating
6. Rating → HourlyWages
Which of these dependencies are maintained in the
decomposed tables?
Which of these dependencies are lost?
To answer this question, we have to compute what
dependencies hold on each of the decomposed table

©2011, Indrakshi Ray – p. 18/52


Dependencies in the Decomposed Table
Let G be the set of FDs on Emps(SSN, Name, Lot, Rating)
G ⊆ FD+
1 , such that all FDs in G involve attributes in
Emps
Let H be the set of FDs on Hourly-Emps3(SSN,
HourlyWages, HoursWorked)
H ⊆ FD+
1 , such that all FDs in H involve attributes in
Hourly-Emps

©2011, Indrakshi Ray – p. 19/52


pendencies in the Decomposed Table Contd.
G is the set of dependencies in Emps
1. SSN → Name
2. SSN → Lot
3. Lot → SSN
4. SSN → Rating
H is the set of dependencies in Hourly-Emps3
1. SSN → HoursWorked
2. SSN → HourlyWages
If G ∪ H ≡ FD1 , dependency is preserved

©2011, Indrakshi Ray – p. 20/52


Why Dependency Preservation?
Whenever an update is made to the database, the system
should be able to check that the update will not create an
illegal relation (that is, one that does not satisfy all the FDs)
We must make sure that such checks can be made by
looking at one relation only

©2011, Indrakshi Ray – p. 21/52


Dependency Preserving
Let R be a relation scheme, F a set of FDs on R
Let ρ = {R1 , R2 , . . . , Rn } be a decomposition of R, that is,
(R = ∪ni=1 Ri )
Let ΠRi (F) denote the set of all FDs X → Y in F + such that
XY ⊆ Ri (we are projecting FDs on Ri )
A decomposition ρ of R preserves FDs in F if
{ΠR1 (F) ∪ ΠR2 (F). . . ΠRn (F)}+ = F +

©2011, Indrakshi Ray – p. 22/52


Dependency Preserving Decomposition?
R = {ABCD}
F = {A → B, B → C, BC → D}
ρ = {ABC, AD}
ΠABC (F) = {A → B, B → C}
ΠAD (F) = {A → D}

©2011, Indrakshi Ray – p. 23/52


Dependency Preserving Decomposition
R = {ABC} F = {A → B, B → C,C → A}
ρ = {AB, BC}
ΠAB (F) = {A → B, B → A}
ΠBC (F) = {B → C,C → B}
Dependency C → A can be derived from ΠAB (F) ∪ ΠBC (F)

©2011, Indrakshi Ray – p. 24/52


Decomposition Examples
R = (A, B,C)
F = {A → B, B → C}
Are the following decompositions loss-less and dependency
preserving?
1. R1 = (A, B), R2 = (B,C)
2. R1 = (A, B), R2 = (A,C)

©2011, Indrakshi Ray – p. 25/52


Decomposition
We first need to determine if any decomposition is needed at
all
What normal form is the relation in?
Each normal form has certain characteristics that will tell us
whether decomposition is needed or not

©2011, Indrakshi Ray – p. 26/52


Unnormalized Relation

©2011, Indrakshi Ray – p. 27/52


Normalized Relation

©2011, Indrakshi Ray – p. 28/52


First Normal Form (1NF)
First Normal Form (1NF): A relation is in 1NF iff each
attribute value in the relation is atomic, that is, each cell
contains one and only one value

©2011, Indrakshi Ray – p. 29/52


Some Definitions
Prime attribute: An attribute of a relation schema R is said to
be a prime attribute of R if it is a member of some candidate
key of R
Non-prime attribute: An attribute is called non-prime if it is
not a prime attribute, that is, if it is not a member of any
candidate key
Consider the schema (ssn, pro jNum, hours)
prime attributes: ssn, pro jNum
non-prime attributes: hours
Full Functional Dependency: If A and B are attributes of a
relation, B is fully functionally dependent on A if B is
functionally dependent on A but not on any proper subset of
A

©2011, Indrakshi Ray – p. 30/52


Example FDs

What are the prime and non-prime attributes?


Are there any non-prime attribute that is fully functionally
dependent?

©2011, Indrakshi Ray – p. 31/52


Full Functional Dependency
R(A, B, C, D)
F = { A → BC, C → AD }
Candidate keys: C and A
B is fully functionally dependent on A, but not on C

©2011, Indrakshi Ray – p. 32/52


Second Normal Form (2NF)
Second Normal Form (2NF): A relation is in 2NF if it is in 1NF
and every non-prime attribute is fully functionally dependent
on the candidate key

©2011, Indrakshi Ray – p. 33/52


Example Relation
DeptInv(Stock#, Dept#, Qty, Manager)
F = {Dept# → Manager, (Stock#, Dept#) → Qty}
Is the relation in 2NF?
candidate key: (Stock#, Dept#) because
(Stock#, Dept#)+ = (Stock#, Dept#, Qty, Manager)
Qty is fully functionally dependent on the candidate key
Manager is not fully functionally dependent on the
candidate key as it is dependent on Dept# which is a
proper subset of the candidate key
Relation schema is not in 2NF
Why is this bad?
Redundancy
Suppose we do not have any stock, then we cannot store
information about dept and manager
©2011, Indrakshi Ray – p. 34/52
Example 2NF Relation
Supplier(SNum, City, Status)
F = { SNum → City, SNum → Status, City → Status }
Candidate Key = SNum
Is relation in 2NF?
Is relation in 3NF?

©2011, Indrakshi Ray – p. 35/52


Normal Forms (3NF)
Third Normal Form (3NF): A relation R is in 3NF if it is in 2NF
and for every FD X → A where A 6∈ X , either X is a superkey
for R or A is a prime attribute
Is the following relation in 3NF? Yes, as shown below.
Address(City, Street, Zip)
City, Street → Zip
Zip → City
For FD1 , (City, Street)+ = (City, Street, Zip), so
(City, Street) is a superkey
For FD2 , (Zip)+ = (Zip,City), so Zip is not a superkey.
But, (City) is a prime attribute as (City, Street) is a
candidate key

©2011, Indrakshi Ray – p. 36/52


Address Relation
City Street Zip
Fort Collins Howes Street 80523
Fort Collins Stillwater Creek Drive 80528
Fort Collins Stillwater Creek Court 80528
Fort Collins Raintree Drive 80526

City, Street → Zip


Zip → City

©2011, Indrakshi Ray – p. 37/52


Normal Foms (BCNF)
Boyce Codd Normal Forms (BCNF): A relation is in BCNF if
for every FD X → A, A 6∈ X , X is a superkey for R
Our goal is to get BCNF schemas, but if we cannot then we
will aim for 3NF

©2011, Indrakshi Ray – p. 38/52


Normal Forms Contd.
What do we do if a relation is not in 3NF or BCNF?
Decompose them

©2011, Indrakshi Ray – p. 39/52


Normalization
Given a relation schema R and a set F of FDs on R we want
to have a decomposition ρ = {R1 , R2 , . . . , Rn } such that
ρ should be loss-less w.r.t. F
ρ should be dependency preserving
each Ri should be in BCNF/3NF
We have an algorithm that guarantees loss-less and
dependency preserving properties and the Ri ’s will be in 3NF
We first try to have BCNF, loss-less join, and dependency
preserving properties
If we cannot get the above we accept 3NF, loss-less join and
dependency preserving properties

©2011, Indrakshi Ray – p. 40/52


BCNF Decomposition + Lossless Join
Input: A universal relation R and a set of FDs on R
1. D := {R}
2. while there is a relation schema Q in D that is not in
BCNF do
(a) choose a relation schema Q in D that is not in BCNF
(b) find a FD X → Y that violates BCNF
(c) replace Q in D by two relation schemas (Q −Y ) and
(X ∪Y )

©2011, Indrakshi Ray – p. 41/52


Example BCNF Decomposition (1)
R=
(branchName, branchCity, assets, customerName, loanNumber, amount
F = {branchName → assets, branchCity
loanNumber → amount, branchName}
Step 0: Key = {loanNumber, customerName}, so relation not
in BCNF
Step 1: Dependency branchName → assets, branchCity
violates BCNF, so decompose into R1 and R2
R1 = (branchName, branchCity, assets)
R2 =
(branchName, customerName, loanNumber, amount)

©2011, Indrakshi Ray – p. 42/52


Example BCNF Decomposition (2)
Step 2: Dependency loanNumber → amount, branchName
violates BCNF, so decompose R2 into R3 and R4
R3 = (branchName, loanNumber, amount)
R4 = (customerName, loanNumber)

Final decomposition is R1 , R3 , and R4

©2011, Indrakshi Ray – p. 43/52


BCNF Decomposition Example
R = CT HRSG
F = {C → T, HR → C, HT → R,CS → G, HS → R}
C = Course, T = Teacher, H = Hour, R = Room, S = Student,
G = Grade
Find a BCNF decomposition of the above relation

©2011, Indrakshi Ray – p. 44/52


BCNF and Dependency Preservation
It is not always possible to get a BCNF decomposition that is
dependency preserving
R = (J, K, L)
F = {JK → L, L → K}
Candidate keys = JK and JL
R is not BCNF
Any decomposition of R will fail to preserve JK → L

©2011, Indrakshi Ray – p. 45/52


ependency-Preserving 3NF Decomposition
Let R be a relation
Let F is a set of FDs that is a minimal cover
Let R1 , R2 , . . ., Rn be a lossless-join decomposition of R
where each Ri (1 ≤ i ≤ n) is in 3NF
Objective is to find a dependency preserving decomposition
1. Identify the set N of dependencies that are not
preserved, that is, not in the closure of the union of Fi ’s
(1 ≤ i ≤ n)
2. For each FD X → A in N , create a relation schema XA
and add it to the decomposition of R

©2011, Indrakshi Ray – p. 46/52


3NF Synthesis Algorithm
Guarantees dependency preservation
Guarantees lossless join property
Algorithm details
1. Find a minimal cover G for F
2. For each LHS of X of a FD that appears in G create a
relation schema in D with attributes
{X ∪ {A1 } ∪ {A2 } ∪ . . . ∪ {Ak }}, where X → A1 , X → A2 ,
. . ., X → Ak are the only dependencies in G with X on
the LHS
3. If none of the relation schemas in D contains a key of R,
then create one more relation schema in D that contains
attributes that form a key of R

©2011, Indrakshi Ray – p. 47/52


Example 3NF Decomposition
Banker-info = (branch-name, customer-name, banker-name,
office-number)
Functional dependencies
banker-name → branch-name, office-number
customer-name, branch-name → banker-name
Key is { customer-name, branch-name }
Is the Banker-info schema in 3NF?

©2011, Indrakshi Ray – p. 48/52


Example 3NF Decomposition Contd.
Step 1: FDs are minimal
Step 2: We get the following schemas
R1 = (banker − name, branch − name, o f f ice − number)
R2 = (customer − name, branch − name, banker − name)

©2011, Indrakshi Ray – p. 49/52


Example 3NF Synthesis
F = {C → CSJDPQV, JP → C, SD → P, J → S}
Step 1: Minimal cover =
{C → J,C → D,C → Q,C → V, JP → C, SD → P, J → S}
Step 2: We obtain the schemas CJ , CD, CQ, CV , CJP, SDP,
and JS
Step 3: Combine all those which have the same primary key
CDJQV , CJP, SDP and JS
Step 4: Since we have a relation that has the superkey, we are
done

©2011, Indrakshi Ray – p. 50/52


Comparison of BCNF and 3NF
It is always possible to decompose a relation into relations in
3NF
the decomposition is lossless
dependencies are preserved
It is always possible to decompose a relation into relations in
BCNF
the decomposition is lossless
it may not be possible to preserve dependencies

©2011, Indrakshi Ray – p. 51/52


Lecture Objectives
What did we cover today?
Completed schema refinement
What do we plan to do next?
We plan to discuss about relational algebra
Chapter 2

©2011, Indrakshi Ray – p. 52/52

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy