Lecture 6
Functional Dependencies and
Normalization for Relational
Databases
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe
Outline
1 Informal Design Guidelines for Relational Databases
1.1Semantics of the Relation Attributes
1.2 Redundant Information in Tuples and Update Anomalies
1.3 Null Values in Tuples
2 Functional Dependencies (FDs)
2.1 Definition of FD
2.2 Inference Rules for FDs
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 3
Outline
3 Normal Forms Based on Primary Keys
3.1 Normalization of Relations
3.2 Practical Use of Normal Forms
3.3 Definitions of Keys and Attributes Participating in Keys
3.4 First Normal Form
3.5 Second Normal Form
3.6 Third Normal Form
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 4
Informal Design Guidelines for Relational
Databases
1.1 Semantics of the Relation Attributes
GUIDELINE 1: Informally, each tuple in a relation should
represent one entity or relationship instance. (Applies to
individual relations and their attributes).
Attributes of different entities (EMPLOYEEs,
DEPARTMENTs, PROJECTs) should not be mixed in the
same relation
Only foreign keys should be used to refer to other entities
Entity and relationship attributes should be kept apart as
much as possible.
Bottom Line: Design a schema that can be explained
easily relation by relation. The semantics of attributes
should be easy to interpret.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 7
Figure 10.1 A simplified COMPANY
relational database schema
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 8
1.2 Redundant Information in Tuples and
Update Anomalies
Data redundancy is a condition in which the same
piece of data is held in two separate places
(different fields within a single database, or multiple
software environments or platforms).
Information is stored redundantly
Wastes storage
Causes problems with update anomalies
Insertion anomalies
Deletion anomalies
Modification anomalies
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 9
EXAMPLE OF AN UPDATE ANOMALY
Consider the relation:
EMP_PROJ(Emp#, Proj#, Ename, Pname,
No_hours)
Update Anomaly:
Changing the name of project number P1 from
“Billing” to “Customer-Accounting” may cause this
update to be made for all 100 employees working
on project P1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 10
EXAMPLE OF AN INSERT ANOMALY
Consider the relation:
EMP_PROJ(Emp#, Proj#, Ename, Pname,
No_hours)
Insert Anomaly:
Cannot insert a project unless an employee is
assigned to it.
Conversely
Cannot insert an employee unless he/she is
assigned to a project.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 11
EXAMPLE OF AN DELETE ANOMALY
Consider the relation:
EMP_PROJ(Emp#, Proj#, Ename, Pname,
No_hours)
Delete Anomaly:
When a project is deleted, it will result in deleting
all the employees who work on that project.
Alternately, if an employee is the sole employee on
a project, deleting that employee would result in
deleting the corresponding project.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 12
Figure 10.3 Two relation schemas
suffering from update anomalies
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 13
Figure 10.4 Example States for
EMP_DEPT and EMP_PROJ
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 14
Guideline to Redundant Information in
Tuples and Update Anomalies
GUIDELINE 2:
Design a schema that does not suffer from the
insertion, deletion and update anomalies.
If there are any anomalies present, then note them
so that applications can be made to take them into
account.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 15
1.3 Null Values in Tuples
GUIDELINE 3:
Relations should be designed such that their
tuples will have as few NULL values as possible
Attributes that are NULL frequently could be
placed in separate relations (with the primary key)
Reasons for nulls:
Attribute not applicable or invalid
Attribute value unknown (may exist)
Value known to exist, but unavailable
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 16
2.1 Functional Dependencies (1)
Functional dependencies (FDs)
Are used to specify formal measures of the
"goodness" of relational designs
And keys are used to define normal forms for
relations
Are constraints that are derived from the meaning
and interrelationships of the data attributes
A set of attributes X functionally determines a set
of attributes Y if the value of X determines a unique
value for Y
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 19
Functional Dependencies (2)
X -> Y holds if whenever two tuples have the same value
for X, they must have the same value for Y
For any two tuples t1 and t2 in any relation instance r(R): If
t1[X]=t2[X], then t1[Y]=t2[Y]
X -> Y in R specifies a constraint on all relation instances
r(R)
Written as X -> Y; can be displayed graphically on a
relation schema as in Figures. ( denoted by the arrow: ).
FDs are derived from the real-world constraints on the
attributes
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 20
Examples of FD constraints (1)
Social security number determines employee
name
SSN -> ENAME
Project number determines project name and
location
PNUMBER -> {PNAME, PLOCATION}
Employee ssn and project number determines
the hours per week that the employee works on
the project
{SSN, PNUMBER} -> HOURS
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 21
Examples of FD constraints (2)
An FD is a property of the attributes in the
schema R
The constraint must hold on every relation
instance r(R)
If K is a key of R, then K functionally determines
all attributes in R
(since we never have two distinct tuples with
t1[K]=t2[K])
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 22
2.2 Inference Rules for FDs (1)
Given a set of FDs F, we can infer additional FDs that
hold whenever the FDs in F hold
Armstrong's inference rules:
IR1. (Reflexive) If Y subset-of X, then X -> Y
IR2. (Augmentation) If X -> Y, then XZ -> YZ
(Notation: XZ stands for X U Z)
IR3. (Transitive) If X -> Y and Y -> Z, then X -> Z
IR1, IR2, IR3 form a sound and complete set of
inference rules
These rules hold and all other rules that hold can be
deduced from these
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 23
3 Normal Forms Based on Primary Keys
3.1 Normalization of Relations
3.2 Practical Use of Normal Forms
3.3 Definitions of Keys and Attributes Participating
in Keys
3.4 First Normal Form
3.5 Second Normal Form
3.6 Third Normal Form
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 29
3.1 Normalization of Relations (1)
Normalization:
The process of decomposing unsatisfactory "bad"
relations by breaking up their attributes into
smaller relations
Normal form:
Condition using keys and FDs of a relation to
certify whether a relation schema is in a particular
normal form
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 30
3.3 Definitions of Keys and Attributes
Participating in Keys (1)
A superkey of a relation schema R = {A1, A2, ....,
An} is a set of attributes S subset-of R with the
property that no two tuples t1 and t2 in any legal
relation state r of R will have t1[S] = t2[S]
A key K is a superkey with the additional
property that removal of any attribute from K will
cause K not to be a superkey any more.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 33
Definitions of Keys and Attributes
Participating in Keys (2)
If a relation schema has more than one key, each
is called a candidate key.
One of the candidate keys is arbitrarily designated
to be the primary key, and the others are called
secondary keys.
A Prime attribute must be a member of some
candidate key
A Nonprime attribute is not a prime attribute—
that is, it is not a member of any candidate key.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 34
3.2 First Normal Form
Disallows
composite attributes
multivalued attributes
nested relations; attributes whose values for an
individual tuple are non-atomic
1NF
Each cell must have a single value
Each column must have data of the same data type
Each row must be uniquely Identified – add unique
ID or add more columns to make each row unique.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 35
Figure 10.8 Normalization into 1NF
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 36
Figure 10.9 Normalization nested
relations into 1NF
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 37
3.3 Second Normal Form (1)
Uses the concepts of FDs, primary key
Definitions
Prime attribute: An attribute that is member of the primary
key K
Full functional dependency: a FD Y -> Z where removal
of any attribute from Y means the FD does not hold any
more
Examples:
{SSN, PNUMBER} -> HOURS is a full FD since neither SSN
-> HOURS nor PNUMBER -> HOURS hold
{SSN, PNUMBER} -> ENAME is not a full FD (it is called a
partial dependency ) since SSN -> ENAME also holds
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 38
Second Normal Form (2)
A relation schema R is in second normal form
(2NF) if every non-prime attribute A in R is fully
functionally dependent on the primary key
R can be decomposed into 2NF relations via the
process of 2NF normalization
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 39
Figure 10.10 Normalizing into 2NF and
3NF
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 40
3.4 Third Normal Form (1)
Definition:
Transitive functional dependency: a FD X -> Z
that can be derived from two FDs X -> Y and Y ->
Z
Examples:
SSN -> DMGRSSN is a transitive FD
Since SSN -> DNUMBER and DNUMBER ->
DMGRSSN hold
SSN -> ENAME is non-transitive
Since there is no set of attributes X where SSN -> X
and X -> ENAME
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 41
Third Normal Form (2)
A relation schema R is in third normal form (3NF) if it is
in 2NF and no non-prime attribute A in R is transitively
dependent on the primary key
R can be decomposed into 3NF relations via the process
of 3NF normalization
NOTE:
In X -> Y and Y -> Z, with X as the primary key, we consider
this a problem only if Y is not a candidate key.
When Y is a candidate key, there is no problem with the
transitive dependency .
E.g., Consider EMP (SSN, Emp#, Salary ).
Here, SSN -> Emp# -> Salary and Emp# is a candidate key.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 42
Figure 10.11 Normalization into 2NF and
3NF
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 43
Normal Forms Defined Informally
1st normal form
All attributes depend on the key
2nd normal form
All attributes depend on the whole key
3rd normal form
All attributes depend on nothing but the key
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 44
Normalization Example
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 52
Normalization Example – Create a table
with data from the form
Sample data is taken from two leases for two
different clients called John Kay and Aline
Stewart and is transformed into table format with
rows and column
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 53
Normalization Example – 1NF
1. We identify the key attribute for the ClientRental
unnormalized table as clientNo.
2. Next, we identify the repeating group in the
unnormalized table as the property rented
details, which repeats for each client.
3. The structure of the repeating group is:
Repeating Group = (propertyNo, pAddress,
rentStart, rentFinish, rent, ownerNo, oName)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 54
Normalization Example – 1NF
To transform an unnormalized table into 1NF, we
ensure that there is a single value at the intersection
of each row and column.
This is achieved by removing the repeating group by:
removing the repeating group (property rented details)
by entering the appropriate client data into each row.
removing the repeating group (property rented details)
by placing the repeating data along with a copy of the
original key attribute (clientNo) in a separate relation
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 55
Normalization Example – First Approach
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 56
Normalization Example – Second Approach
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 57
Normalization Example – 2NF - FDs
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 58
Normalization Example – FDs
The ClientRental relation has the following
functional dependencies:
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 59
Normalization Example – 2NF
Second normal form relations derived from the ClientRental relation. The
ClientRental relation has the following functional dependencies:
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 60
Normalization Example – FDs - 3NF
The functional dependencies for the Client, Rental, and PropertyOwner
relations are as follows:
Client
fd2 clientNo ® cName (Primary key)
Rental
fd1 clientNo, propertyNo ® rentStart, rentFinish (Primary key)
fd5' clientNo, rentStart ® propertyNo, rentFinish (Candidate key)
fd6' propertyNo, rentStart ® clientNo, rentFinish (Candidate key)
PropertyOwner
fd3 propertyNo ® pAddress, rent, ownerNo, oName (Primary key)
fd4 ownerNo ® oName (Transitive dependency)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 61
Normalization Example – FDs - 3NF
The PropertyForRent and Owner relations are in 3NF, as there
are no further transitive dependencies on the primary key.
All the non-primary-key attributes within the PropertyOwner
relation are functionally dependent on the primary key, with the
exception of oName, which is transitively dependent on
ownerNo
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 62
Normalization Example – FDs - 3NF
To transform the PropertyOwner relation into 3NF, we must first
remove this transitive dependency by creating two new relations
called PropertyForRent and Owner.
The new relations have the following form:
PropertyForRent (propertyNo, pAddress, rent, ownerNo)
Owner (ownerNo, oName)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 63
Normalization Example – FDs - 3NF
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 65
Chapter Outline
Informal Design Guidelines for Relational
Databases
Functional Dependencies (FDs)
Definition, Inference Rules, Equivalence of Sets of
FDs, Minimal Sets of FDs
Normal Forms Based on Primary Keys
General Normal Form Definitions (For Multiple
Keys)
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 10- 66