chapter 5knreddycse.weebly.com/uploads/5/7/2/0/57207825/imdb-chapter05.pdf · database systems:...

56
5 1 Chapter 5 Normalization of Database Tables Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel

Upload: nguyenthien

Post on 16-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

5

1

Chapter 5

Normalization of Database Tables

Database Systems:Design, Implementation, and Management,

Sixth Edition, Rob and Coronel

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

2

In this chapter, you will learn:

•What normalization is and what role it plays inthe database design process

•About the normal forms 1NF, 2NF, 3NF, BCNF,and 4NF

•How normal forms can be transformed fromlower normal forms to higher normal forms

•That normalization and ER modeling are usedconcurrently to produce a good databasedesign

•That some situations require denormalization togenerate information efficiently

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

3

Database Tables and Normalization

•Normalization

–Process for evaluating and correcting tablestructures to minimize data redundancies

•helps eliminate data anomalies

–Works through a series of stages callednormal forms:

•Normal form (1NF)

•Second normal form (2NF)

•Third normal form (3NF)

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

4

Database Tables and Normalization(continued)

–2NF is better than 1NF; 3NF is better than2NF

–For most business database design purposes,3NF is highest we need to go in thenormalization process

–Highest level of normalization is not alwaysmost desirable

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

5

The Need for Normalization

•Example: company that manages buildingprojects

–Charges its clients by billing hours spent oneach contract

–Hourly billing rate is dependent on employee’sposition

–Periodically, a report is generated that containsinformation displayed in Table 5.1

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

6

A Sample Report Layout

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

7

A Table in the Report Format

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

8

The Need for Normalization (continued)

•Structure of data set in Figure 5.1 does nothandle data very well

•The table structure appears to work; report isgenerated with ease

•Unfortunately, the report may yield differentresults, depending on what data anomaly hasoccurred

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

9

Conversion to First Normal Form

•Repeating group

–Derives its name from the fact that a group ofmultiple (related) entries can exist for anysingle key attribute occurrence

•Relational table must not contain repeatinggroups

•Normalizing the table structure will reducethese data redundancies

•Normalization is three-step procedure

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

10

Step 1: Eliminate the Repeating Groups

•Present data in a tabular format, where eachcell has a single value and there are norepeating groups

•Eliminate repeating groups by eliminatingnulls, making sure that each repeating groupattribute contains an appropriate data value

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

11

Data Organization: First Normal Form

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

12

Step 2: Identify the Primary Key

•Primary key must uniquely identify attributevalue

•New key must be composed

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

13

Step 3: Identify all Dependencies

•Dependencies can be depicted with the helpof a diagram

•Dependency diagram:

–Depicts all dependencies found within a giventable structure

–Helpful in getting bird’s-eye view of allrelationships among a table’s attributes

–Use makes it much less likely that animportant dependency will be overlooked

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

14

A Dependency Diagram:First Normal Form (1NF)

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

15

First Normal Form

•Tabular format in which:–All key attributes are defined–There are no repeating groups in the table–All attributes are dependent on primary key

•All relational tables satisfy 1NF requirements•Some tables contain partial dependencies

–Dependencies based on only part of theprimary key

–Sometimes used for performance reasons, butshould be used with caution

–Still subject to data redundancies

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

16

Conversion to Second Normal Form

•Relational database design can be improvedby converting the database into secondnormal form (2NF)

•Two steps

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

17

Step 1: Identify All Key Components

•Write each key component on separate line,and then write the original (composite) key onthe last line

•Each component will become the key in anew table

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

18

Step 2: Identify the Dependent Attributes

•Determine which attributes are dependent onwhich other attributes

•At this point, most anomalies have beeneliminated

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

19

Second Normal Form (2NF)Conversion Results

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

20

Second Normal Form

•Table is in second normal form (2NF) if:

–It is in 1NF and

–It includes no partial dependencies:

•No attribute is dependent on only a portion ofthe primary key

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

21

Conversion to Third Normal Form

•Data anomalies created are easily eliminatedby completing three steps

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

22

Step 1: Identify Each New Determinant

•For every transitive dependency, write itsdeterminant as a PK for a new table

–Determinant

•Any attribute whose value determines othervalues within a row

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

23

Step 2: Identify the Dependent Attributes

•Identify the attributes dependent on eachdeterminant identified in Step 1 and identifythe dependency

•Name the table to reflect its contents andfunction

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

24

Step 3: Remove the Dependent Attributesfrom Transitive Dependencies

•Eliminate all dependent attributes in transitiverelationship(s) from each table that has sucha transitive relationship

•Draw a new dependency diagram to show alltables defined in Steps 1–3

•Check new tables and modified tables fromStep 3 to make sure that each has adeterminant and does not containinappropriate dependencies

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

25

Third Normal Form (3NF)Conversion Results

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

26

Third Normal Form

•A table is in third normal form (3NF) if:

–It is in 2NF and

–It contains no transitive dependencies

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

27

Improving the Design

•Table structures are cleaned up to eliminatethe troublesome initial partial and transitivedependencies

•Normalization cannot, by itself, be relied on tomake good designs

•It is valuable because its use helps eliminatedata redundancies

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

28

Improving the Design (continued)•The following changes were made:

–PK assignment

–Naming conventions

–Attribute atomicity

–Adding attributes

–Adding relationships

–Refining PKs

–Maintaining historical accuracy

–Using derived attributes

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

29

The Completed Database

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

30

The Completed Database (continued)

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

31

Limitations on System-Assigned Keys

•System-assigned primary key may notprevent confusing entries

•Data entries in Table 5.2 are inappropriatebecause they duplicate existing records

–Yet there has been no violation of either entityintegrity or referential integrity

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

32

Duplicate Entries in the JOB Table

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

33

The Boyce-Codd Normal Form (BCNF)

•Every determinant in the table is a candidatekey

–Has same characteristics as primary key, butfor some reason, not chosen to be primary key

•If a table contains only one candidate key, the3NF and the BCNF are equivalent

•BCNF can be violated only if the tablecontains more than one candidate key

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

34

The Boyce-Codd Normal Form (BCNF)(continued)

•Most designers consider the Boyce-Coddnormal form (BCNF) as a special case of 3NF

•A table is in 3NF if it is in 2NF and there are notransitive dependencies

•A table can be in 3NF and not be in BCNF–A transitive dependency exists when one

nonprime attribute is dependent on anothernonprime attribute

–A nonkey attribute is the determinant of a keyattribute

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

35

A Table That is in 3NF but not in BCNF

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

36

Decomposition to BCNF

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

37

Sample Data for a BCNF Conversion

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

38

Another BCNF Decomposition

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

39

Normalization and Database Design

•Normalization should be part of designprocess

•Make sure that proposed entities meetrequired normal form before table structuresare created

•Many real-world databases have beenimproperly designed or burdened withanomalies if improperly modified duringcourse of time

•You may be asked to redesign and modifyexisting databases

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

40

Normalization and Database Design(continued)

•ER diagram

–Provides the big picture, or macro view, of anorganization’s data requirements andoperations

–Created through an iterative process

•Identifying relevant entities, their attributes andtheir relationship

•Use results to identify additional entities andattributes

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

41

Normalization and Database Design(continued)

•Normalization procedures

–Focus on the characteristics of specific entities

–A micro view of the entities within the ER diagram

•Difficult to separate normalization processfrom ER modeling process

•Two techniques should be used concurrently

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

42

The Initial ERD for aContracting Company

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

43

The Modified ERD for aContracting Company

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

44

The Incorrect Representationof a M:N Relationship

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

45

The Final (Implementable) ERD for aContracting Company

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

46

The Implemented Database for theContracting Company

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

47

Higher-Level Normal Forms

•In some databases, multiple multivaluedattributes exist

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

48

Tables with Multivalued Dependencies

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

49

Fourth Normal Form

•Table is in fourth normal form (4NF) if–It is in 3NF–Has no multiple sets of multivalued

dependencies

•4NF is largely academic if tables conform tothe following two rules:–All attributes are dependent on primary key

but independent of each other–No row contains two or more multivalued facts

about an entity

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

50

A Set of Tables in 4NF

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

51

Denormalization

•Creation of normalized relations is importantdatabase design goal

•Processing requirements should also be agoal

•If tables decomposed to conform tonormalization requirements

–Number of database tables expands

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

52

Denormalization (continued)

•Joining larger number of tables takesadditional disk input/output (I/O) operationsand processing logic

–Reduces system speed

•Conflicts among design efficiency,information requirements, and processingspeed are often resolved throughcompromises that may includedenormalization

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

53

Denormalization (continued)

•Unnormalized tables in a productiondatabase tend to have these defects:

–Data updates are less efficient becauseprograms that read and update tables mustdeal with larger tables

–Indexing is much more cumbersome

–Unnormalized tables yield no simple strategiesfor creating virtual tables known as views

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

54

Denormalization (continued)

•Use denormalization cautiously

•Understand why—under some circumstances—unnormalized tables are a better choice

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

55

Summary

•Normalization is a table design techniqueaimed at minimizing data redundancies

•First three normal forms (1NF, 2NF, and 3NF)are most commonly encountered

•Normalization is an important part—but only apart—of the design process

•Continue the iterative ER process until allentities and their attributes are defined and allequivalent tables are in 3NF

Database Systems: Design, Implementation, & Management, 6th Edition, Rob & Coronel

5

56

Summary (continued)

•A table in 3NF may contain multivalueddependencies that produce either numerousnull values or redundant data

•It may be necessary to convert a 3NF table tothe fourth normal form (4NF) by

–splitting such a table to remove multivalueddependencies

•Tables are sometimes denormalized to yieldless I/O which increases processing speed