21
Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

Embed Size (px)

Citation preview

Page 1: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

Database Design:Normalization

J.G. ZhengJune 29th 2005

DB Chapter 4

Page 2: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

2

Overview

Normalization First three normal forms

Page 3: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

3

Introduction/Review

Two perspectives to design database E-R Diagram (top-down) Normalization (bottom-up)

Normalization is a process of efficiently modeling data, by analyzing relationships between attributes The goal is to remove redundancy and anomaly Usually a table is divided into 2 or more tables

Combining the two perspectives in practice

Page 4: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

4

Normal FormsNormal forms are guidelines (steps) for the normalization process

Unnormalized Form (UNF)

1st Normal Form (1NF)

2nd Normal Form (2NF)

3rd Normal Form (3NF)

Boyce Codd Normal Form (BC/NF)

4th Normal Form (4NF)

5th Normal Form

DK/NF

Few

er

an

om

alie

s... …

but m

ore

tab

les

Norm

aliz

ati

on Norm

aliza

tion

Page 5: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

5

Normalization – 1NF

A relation is in 1NF if it satisfies the definition of a relation Review: what are the characteristics

of a relation?

Particularly No repeating value sets (single value

property)

Page 6: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

6

Transforming to 1NF

Row transformation (Figure 4.5 and 4.6 on page 287)

UNF 1NF

Page 7: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

7

Higher Normal Forms

Normal forms higher than 1NF deal with functional dependence

Identify which normal form the table is by analyzing the functional dependence between attributes (fields)

Page 8: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

8

Functional Dependence

If each value of attribute A is associated with only one value of attribute B, we say

A determines B Or, B is dependent on A Denoted as: A B

Functional dependence describes relationships between attributes (not relations)

Note: A (or B) could be a set of fields

Page 9: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

9

Functional Dependence Examples

Dependence example For each SSN, there is only one

corresponding first name (or last name), so: SSN determines FirstName SSN FirstName

Non-dependence example Each instructor teaches multiple courses, so: InstructorId does not determines

CourseNumber

Page 10: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

10

Functional Dependence and Keys

By definition, primary key functionally determines all other attributes Primary key Surrogate key Composite primary key

Dependency diagram

ISBN Title PubDate ListPrice

Page 11: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

11

Functional Dependency Exercise

CustomerNum CustomerName?{Street, City, State} Zip?CustomerName Balance?State (?) ZipRepNum ( ? ) CustomerName

Page 12: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

12

Normalization – 2NF

A relation is in 2NF, if It is in 1st normal formal, and All nonkey attributes must be functionally

dependent on the whole primary key (Fully dependence)

No partial dependence

It also implies that a relation is in 2NF if the primary key is a single attribute

Page 13: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

13

2NF Identify primary key (PK) If PK consists of only one field, then it is in 2NF If PK is a composite key, then analyze functional

dependence between part of primary key and other non-key attributes

Page 14: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

14

Normalization – 3NF

A relation is in 3NF, if It is in 2nd normal formal, and All attributes must, and only, be

functionally dependent on candidate keys

3NF deals with [transitive dependence] A B C No transitive dependence

Page 15: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

15

3NF

Identify primary key (PK) and Look for transitive dependence

Page 16: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

16

Some Practical Tips

If there are attributes of two different entities in one table, there are usually anomalies

To identify the normalization level, determine the primary key first; then look for partial dependence and transitive dependence

Page 17: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

17

Normalization Exercise 1

Which normal form is the above table in?

A. 1NFB. 2NFC. 3NFD. UNF

Page 18: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

18

Normalization Exercise 2

Which normal form is the above table (2nd one) in?

A. 1NFB. 2NFC. 3NFD. UNF

Page 19: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

19

Normalization Exercise 3

Which normal form is the above table (first one) in?

1NF 2NF 3NF UNF

Page 20: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

20

Final database design with 3 tables

Page 21: Database Design: Normalization J.G. Zheng June 29 th 2005 DB Chapter 4

21

Summary

1NF: If the tables are relations

2NF: If the tables are in 1NF, and every non-key attribute is dependent on the key, the whole key

3NF: If the tables are in 2NF, and every non-key attribute is dependent on the key, the whole key, and nothing but the key

UNF

Split repeating groups in separate rows

Eliminate partial dependencies

Eliminate transitive dependencies