You are on page 1of 85

Chapter 6

Normalisation of Database
Tables (Part 1)
CMT221/CMM222
Database Organisation and Design

Teh Je Sen (2019) 1


Learning Objectives

Normalisation and its role in database design


The various normal forms and how to transform them

Teh Je Sen (2019) 2


Re-cap Quiz

Let’s first re-cap what we learned last week!

Teh Je Sen (2019) 3


Normalisation

Evaluate and correct tables to


minimise data redundancies and
reduce data anomalies

Stages
First normal form (1NF)
Second normal form (2NF)
Third normal form (3NF)

Teh Je Sen (2019) 4


Normalisation

Generally, higher normal forms are better than lower


normal forms
3NF is sufficient for most business database needs

Teh Je Sen (2019) 5


Denormalisation

Produces a lower normal form


Higher redundancy but may lead to
increased performance

Performance Redundancy

Teh Je Sen (2019) 6


Redundancies and Anomalies

Identify redundancies of the following table

How do we fix it?


Teh Je Sen (2019) 7
Redundancies and Anomalies

No repetition!

How is this better?


Teh Je Sen (2019) 8
Redundancies and Anomalies

Redundancy leads to anomalies


What anomalies can you remember?
Insertion
Deletion
Modification

Teh Je Sen (2019) 9


Insertion Anomaly

How will insertion anomaly occur for the following


unnormalised table?

Teh Je Sen (2019) 10


Insertion Anomaly

If new staff member is inserted, correct details of the


branch has to be entered

Teh Je Sen (2019) 11


Insertion Anomaly

Cannot enter information for a new branch without


creating a null staff info.

Teh Je Sen (2019) 12


Insertion Anomaly

How will deletion anomaly occur for the following


unnormalised table?

Teh Je Sen (2019) 13


Insertion Anomaly

What happens if we delete the record for Art Peters or


Sally Stern??

Teh Je Sen (2019) 14


Insertion Anomaly

We lose all information for branch B003 and B004

Teh Je Sen (2019) 15


Insertion Anomaly

How will update anomaly occur for the following


unnormalised table?

Teh Je Sen (2019) 16


Insertion Anomaly

If branch information is changed, all records of staff


located at that branch needs to be updated.

Teh Je Sen (2019) 17


Importance of Normalisation

Produce a set of tables with desirable properties


(avoid anomalies)
Support the requirements of a user or company

Teh Je Sen (2019) 18


Example – Need for Normalisation

Assume there’s a company called Bob Ltd. that


manages building projects
It charges its clients by billing hours spent by each
employee on each project
The hourly billing rate depends on the employee’s
position

Teh Je Sen (2019) 19


Example – Need for Normalisation

Based on this table, Bob can generate a report


Teh Je Sen (2019) 20
Example – Need for Normalisation

Teh Je Sen (2019) 21


Example – Need for Normalisation

The original table seems to be sufficient because it


can generate a report to bill the client
But what potential problems can you identify from
the table?
Let’s look at it again

Teh Je Sen (2019) 22


Example – Need for Normalisation

NULL

NULL

NULL

NULL

There are many null values for the primary key


Teh Je Sen (2019) 23
Example – Need for Normalisation

NULL

NULL

NULL

NULL

Update anomaly
Teh Je Sen (2019) 24
Example – Need for Normalisation

NULL

NULL

NULL

NULL

JOB_CLASS can lead to inconsistencies during data entry


E.g. Database Designer, DB Designer, DD Designer
Teh Je Sen (2019) 25
Example – Need for Normalisation

NULL

NULL

NULL

NULL

Other anomalies such as insertion and deletion


Teh Je Sen (2019) 26
Normalisation

So what do you think are the characteristics of a


normalised table?

Each table represents a single subject


All nonprime attributes are dependent only on the
primary key
No anomalies

Teh Je Sen (2019) 27


How do we normalise?

Work with one table at a time


Identify undesirable dependencies
Break down the table into new tables to remove
dependencies
Ensure that all tables are at least in 3NF

Teh Je Sen (2019) 28


Recap: Functional Dependency

What is functional dependency?


What does it mean when we say
attribute B is functionally dependent on attribute A?
Attribute A determines exactly one value of attribute
B
What examples can you give me?

Teh Je Sen (2019) 29


Recap: Functional Dependency

In the following functional dependence, which


attribute is the determinant and which one is the
dependent?
STU_NUM  (STU_LNAME, STU_FNAME)

Teh Je Sen (2019) 30


Recap: Functional Dependency

In the following functional dependence, which


attribute is the determinant and which one is the
dependent?
STU_NUM  (STU_LNAME, STU_FNAME)

Teh Je Sen (2019) 31


Recap: Full Functional Dependency

Is the following functional dependency a fully


functional dependency?
(STU_NUM, STU_LNAME)STU_GPA

Teh Je Sen (2019) 32


Recap: Full Functional Dependency

No, because STU_LNAME is not required to


uniquely identify STU_GPA
(STU_NUM, STU_LNAME)STU_GPA

Teh Je Sen (2019) 33


Recap: Full Functional Dependency

A full functional dependence refers to a functional


dependency where the entire collection of attributes
in the determinant is required for the relationship

Teh Je Sen (2019) 34


Other Dependencies

Time to learn two new types of dependencies that will


be addressed by normalisation
Partial dependency
Transitive dependency

Teh Je Sen (2019) 35


Partial Dependency

A functional dependence whereby the determinant is


only part of the primary key
Identify the partial dependencies for the example in
the following slide

Teh Je Sen (2019) 36


Partial Dependency

(STUD_NUM, COURSE_NUM)
(STUD_NAME ,COURSE_NAME, GPA)

How about GPA?

Teh Je Sen (2019) 37


Partial Dependency

(STUD_NUM, COURSE_NUM)
(STUD_NAME ,COURSE_NAME, GPA)

STUD_NAME and COURSE_NUM are both


dependent on only part of the primary key
GPA is fully dependent on the entire primary key
Teh Je Sen (2019) 38
Transitive Dependency

A functional dependence whereby the determinant is


a nonkey attribute
In other words, an attribute functionally depends on
another nonkey attribute
Identify the transitive dependencies for the example
in the following slide

Teh Je Sen (2019) 39


Partial Dependency

(STUD_NUM, COURSE_NUM)
(STUD_NAME, COURSE_NAME, HOSTEL_NUM,
HOSTEL_NAME)

HOSTEL_NAME is dependent on HOSTEL_NUM,


which is a nonkey attribute
Teh Je Sen (2019) 40
Example

Take out a piece of paper


For all exercises, write down the solutions

Teh Je Sen (2019) 41


Example

Identify the partial dependencies in this table


(use the dependency notation XXYY)
STU_ID PROJ_ID STU_NAME PROJ_NAME HOURS
1001 9221 Bob Plane 2
1002 9222 Eve Boat 3
1001 9223 Bob Car 5

STUD_IDSTU_NAME
PROJ_IDPROJ_NAME

Teh Je Sen (2019) 42


Example

Identify the transitive dependencies in this table


(use the dependency notation XXYY)
STU_ID STU_NAME POSTCODE CITY
1001 Bob 11700 Gelugor
1002 Eve 11900 Bayan Lepas
1003 Wan 11000 Balik Pulau

POSTCODECITY

Teh Je Sen (2019) 43


Example

Identify the partial and transitive dependencies in this


table (E – Employee, J – Job)
E_ID J_ID E_NAME E_AGE J_SALARY J_TITLE DEPT_ID DEPT_NAME
001 A001 Bob 26 1000 Manager B001 Sales
002 A002 Eve 21 2500 Clerk B001 Sales
003 A003 Clare 25 1000 Manager B002 IT

E_ID(E_NAME, E_AGE, DEPT_ID)


J_ID(J_SALARY, J_TITLE)
DEPT_IDDEPT_NAME
J_TITLEJ_SALARY
Teh Je Sen (2019) 44
Normalisation Process

Teh Je Sen (2019) 45


First Normal Form (1NF)

Steps:
Eliminate repeating groups
Identify primary key
Identify all dependencies

Teh Je Sen (2019) 46


Repeating Groups

A group of multiple entries of the same or multiple


types can exist for any single key attribute
occurrence
One primary key value determines multiple entries

Teh Je Sen (2019) 47


Normalising to 1NF

Step 1:
Eliminate repeating groups by ensuring that each
record is complete

Teh Je Sen (2019) 48


Normalising to 1NF

Teh Je Sen (2019) 49


Normalising to 1NF

Teh Je Sen (2019) 50


Normalising to 1NF

Step 2:
Identify a primary key that can uniquely identify
each record
What would be a suitable primary key for the table?

Teh Je Sen (2019) 51


Normalising to 1NF
Why do we need a composite key?

Teh Je Sen (2019) 52


Normalising to 1NF

Step 3:
Identify all dependencies
Depict all dependencies using a dependency
diagram

Teh Je Sen (2019) 53


How do we draw a dependency diagram?

Steps:
List out all attribute names in a row
Draw arrows to indicate all functional dependencies

A B C D

Teh Je Sen (2019) 54


Transitive
Identify the dependencies Dependency
Functional Dependency
Partial
Dependency

Teh Je Sen (2019) 55


Transitive
Draw the dependency diagram Dependency
Functional Dependency
Partial
Dependency

Teh Je Sen (2019) 56


1NF Dependency Diagram

Teh Je Sen (2019) 57


Draw the dependency diagram

No partial dependency Transitive dependency

staffNo name position salary branchNo branchAdd telNo

Functional Dependency

How about positionsalary?


Teh Je Sen (2019) 58
Alternative Solution

Partial dependency

staffNo name position salary branchNo branchAdd telNo

Functional Dependency Transitive dependency

Teh Je Sen (2019) 59


Last one!

E_ID J_ID E_NAME E_AGE J_SALARY J_TITLE DEPT_ID DEPT_NAME


001 A001 Bob 26 1000 Manager B001 Sales
002 A002 Eve 21 2500 Clerk B001 Sales
003 A003 Clare 25 1000 Manager B002 IT
004 A004 Adam 23 2500 Clerk B002 IT

Partial dependency Transitive dependency Transitive dependency

E_ID J_ID E_NAME E_AGE J_SALARY J_TITLE DEPT_ID DEPT_NAME

Functional Dependency

Why can J_TITLE determine J_SALARY?


Teh Je Sen (2019)
60
Second Normal Form (2NF)

Table is already in 1NF


and has no partial dependencies
How do you eliminate partial dependencies?

Teh Je Sen (2019) 61


Eliminating Partial Dependencies

Let’s take a look at the following table.


What are the partial dependencies?
How do you think we can eliminate them?
STU_ID PROJ_ID STU_NAME PROJ_NAME HOURS
1001 9221 Bob Plane 2
1002 9222 Eve Boat 3
1001 9223 Bob Car 5

Teh Je Sen (2019) 62


Eliminating Partial Dependencies

Steps:
Create a new table for each partial dependency
Remove dependent attributes from the main table
and put them into the new tables
Draw the normalised versions of the following tables!
STU_ID PROJ_ID STU_NAME PROJ_NAME HOURS
1001 9221 Bob Plane 2
1002 9222 Eve Boat 3
1001 9223 Bob Car 5

Teh Je Sen (2019) 63


Eliminating Partial Dependencies

STU_ID STU_NAME PROJ_ID PROJ_NAME


1001 Bob 9221 Plane
1002 Eve 9222 Boat
1001 Bob 9223 Car

STU_ID PROJ_ID HOURS


1001 9221 2
1002 9222 3
1001 9223 5

Is every table in 2NF?


Teh Je Sen (2019) 64
Exercise 1

Convert the following dependency diagram into 2NF

A B C D E F G H

Teh Je Sen (2019) 65


Solution 1

Ensure each table is in 2NF!

A B C F

C D E

B G H

Teh Je Sen (2019) 66


Exercise 2

Convert the following dependency diagram into 2NF

Teh Je Sen (2019) 67


Solution 2

Name change to reflect


Teh Je Sen (2019) table name 68
2NF Summary

A table is in 2NF if it is
1NF
Has no partial dependencies

Teh Je Sen (2019) 69


Third Normal Form (3NF)

Table is already in 2NF


and has no transitive dependencies

How do you eliminate transitive dependencies?


Make new tables
Use the determinant as primary key
Reassign corresponding dependent attributes

Teh Je Sen (2019) 70


Which tables are already in 3NF?

Transitive
dependency

Teh Je Sen (2019) 71


Normalising to 3NF

How do we normalise this table?

Create new table with the nonkey determinant(s) as


primary key
Reassign the corresponding dependent(s) into the
new table

Teh Je Sen (2019) 72


Solution

Teh Je Sen (2019) 73


3NF Summary

A table is in 3NF if it is
2NF
Has no transitive dependencies

Teh Je Sen (2019) 74


Practice

Normalise the following dependency diagram to be


3NF

A B C D E F G

Teh Je Sen (2019) 75


Solution

All tables are in 3NF

A B F

A E F G

B C E D

Teh Je Sen (2019) 76


Practice 2

Convert the following ERD into a dependency


diagram that is at least 3NF

APPOINTMENT
PK App_Num
DOCTOR
FK1 Doc_EmpID
PK Doc_EmpID
App_Date
Doc_Fname
App_PatientID
Doc_Lname
App_P_Name
Doc_CellPhone
App_Postcode
App_City

Teh Je Sen (2019) 77


Solution

Draw a dependency diagram for each table


Identify their dependencies and normal forms

App_
App_Num Doc_EmpID App_Date App_PatientID App_P_Name App_Postcode
City

2NF

Doc_EMPID Doc_Fname Doc_Lname Doc_CellPhone

3NF

Teh Je Sen (2019) 78


Solution

There are no partial dependencies

App_
App_Num Doc_EmpID App_Date App_PatientID App_P_Name App_Postcode
City

2NF

Teh Je Sen (2019) 79


Solution

Remove transitive dependencies

App_
App_Num Doc_EmpID App_Date App_PatientID App_P_Name App_Postcode
City

2NF

Teh Je Sen (2019) 80


Solution

Remove transitive dependencies

App_Num Doc_EmpID App_Date PatientID

3NF
PatientID P_Name Postcode City

2NF

Teh Je Sen (2019) 81


Solution

Remove transitive dependencies

App_Num Doc_EmpID App_Date PatientID

3NF
PatientID P_Name Postcode

3NF
Postcode City

3NF
Teh Je Sen (2019) 82
Higher Normal Forms

3NF is sufficient for business transactional databases


But there are also higher-level normal forms:
Boyce-Codd Normal Form (BCNF)
Fourth Normal Form (4NF)

Teh Je Sen (2019) 83


BCNF

Every determinant is a candidate key


If there is a determinant that is not a candidate key,
the table is not in BCNF
If a table only has only one candidate key, BCNF is
equivalent to 3NF

Teh Je Sen (2019) 84


4NF

A table that is 3NF


And also has no multivalued dependency

Teh Je Sen (2019) 85

You might also like