74
NORMALIZATION 1

NORMALIZATION

Embed Size (px)

DESCRIPTION

NORMALIZATION. Objectives. The purpose of normalization. How normalization can be used when designing a relational database. The potential problems associated with redundant data in base relations. The concept of functional dependency, which describes the relationship between attributes. - PowerPoint PPT Presentation

Citation preview

Page 1: NORMALIZATION

NORMALIZATION

1

Page 2: NORMALIZATION

Objectives

•The purpose of normalization. •How normalization can be used when

designing a relational database.•The potential problems associated

with redundant data in base relations.•The concept of functional

dependency, which describes the relationship between attributes.

•The characteristics of functional dependencies used in normalization.

2

Page 3: NORMALIZATION

•How to identify functional dependencies for a given relation.

•How functional dependencies identify the primary key for a relation.

•How to undertake the process of normalization.

•How normalization uses functional dependencies to group attributes into relations that are in a known normal form.

3

Page 4: NORMALIZATION

•How to identify the most commonly used normal forms, namely First Normal Form (1NF), Second Normal Form (2NF), and Third Normal Form (3NF).

•The problems associated with relations that break the rules of 1NF, 2NF, or 3NF.

•How to represent attributes shown on a form as 3NF relations using normalization.

4

Page 5: NORMALIZATION

Purpose of Normalization

•Normalization is a technique for producing a set of suitable relations that support the data requirements of an enterprise.

5

Page 6: NORMALIZATION

Purpose of Normalization•Characteristics of a suitable set of

relations include: ▫the minimal number of attributes

necessary to support the data requirements of the enterprise;

▫attributes with a close logical relationship are found in the same relation;

▫minimal redundancy with each attribute represented only once with the important exception of attributes that form all or part of foreign keys.

6

Page 7: NORMALIZATION

Purpose of Normalization

•The benefits of using a database that has a suitable set of relations is that the database will be:▫easier for the user to access and

maintain the data;▫take up minimal storage space on the

computer.

7

Page 8: NORMALIZATION

How Normalization Supports Database Design

8

Page 9: NORMALIZATION

Data Redundancy and Update Anomalies•Major aim of relational database

design is to group attributes into relations to minimize data redundancy.

9

Page 10: NORMALIZATION

Data Redundancy and Update Anomalies•Potential benefits for implemented database include:▫Updates to the data stored in the

database are achieved with a minimal number of operations thus reducing the opportunities for data inconsistencies.

▫Reduction in the file storage space required by the base relations thus minimizing costs.

10

Page 11: NORMALIZATION

Data Redundancy and Update Anomalies•Problems associated with data

redundancy are illustrated by comparing the Staff and Branch relations with the StaffBranch relation.

11

Page 12: NORMALIZATION

Data Redundancy and Update Anomalies

12

Page 13: NORMALIZATION

Data Redundancy and Update Anomalies•StaffBranch relation has redundant

data; the details of a branch are repeated for every member of staff.

•In contrast, the branch information appears only once for each branch in the Branch relation and only the branch number (branchNo) is repeated in the Staff relation, to represent where each member of staff is located.

13

Page 14: NORMALIZATION

Data Redundancy and Update Anomalies•Relations that contain redundant

information may potentially suffer from update anomalies.

•Types of update anomalies include▫Insertion▫Deletion▫Modification

14

Page 15: NORMALIZATION

•Update Anomaly exists when one or more instances of duplicated data is updated, but not all. For example, consider Jones moving address - you need to update all instances of Jones's address.

15

Page 16: NORMALIZATION

•Delete Anomaly exists when certain attributes are lost because of the deletion of other attributes. For example, consider what happens if Student S30 is the last student to leave the course - All information about the course is lost.

16

Page 17: NORMALIZATION

• Insert Anomaly occurs when certain attributes cannot be inserted into the database without the presence of other attributes. For example this is the converse of delete anomaly - we can't add a new course unless we have at least one student enrolled on the course.

17

Page 18: NORMALIZATION

Functional Dependencies

• Important concept associated with normalization.

• Functional dependency describes relationship between attributes.

• For example, if A and B are attributes of relation R, B is functionally dependent on A (denoted A B), if each value of A in R is associated with exactly one value of B in R.

18

Page 19: NORMALIZATION

Characteristics of Functional Dependencies

• Property of the meaning or semantics of the attributes in a relation.

• Diagrammatic representation.

• The determinant of a functional dependency refers to the attribute or group of attributes on the left-hand side of the arrow.

19

Page 20: NORMALIZATION

An Example Functional Dependency

20

Page 21: NORMALIZATION

Example Functional Dependency that holds for all Time• Consider the values shown in staffNo

and sName attributes of the Staff relation (see Slide 12).

• Based on sample data, the following functional dependencies appear to hold.

staffNo → sNamesName → staffNo

• However, the only functional dependency that remains true for all possible values for the staffNo and sName attributes of the Staff relation is:

staffNo → sName

21

Page 22: NORMALIZATION

Characteristics of Functional Dependencies

•Determinants should have the minimal number of attributes necessary to maintain the functional dependency with the attribute(s) on the right hand-side.

•This requirement is called full functional dependency.

22

Page 23: NORMALIZATION

Characteristics of Functional Dependencies•Full functional dependency indicates that 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.

23

Page 24: NORMALIZATION

Example Full Functional Dependency• Exists in the Staff relation (see Slide 12).

staffNo, sName → branchNo

• True - each value of (staffNo, sName) is associated with a single value of branchNo.

• However, branchNo is also functionally dependent on a subset of (staffNo, sName), namely staffNo. Example above is a partial dependency.

24

Page 25: NORMALIZATION

Characteristics of Functional Dependencies•Main characteristics of functional

dependencies used in normalization:▫There is a one-to-one relationship

between the attribute(s) on the left-hand side (determinant) and those on the right-hand side of a functional dependency.

▫Holds for all time.▫The determinant has the minimal

number of attributes necessary to maintain the dependency with the attribute(s) on the right hand-side.

25

Page 26: NORMALIZATION

Transitive Dependencies•Important to recognize a transitive

dependency because its existence in a relation can potentially cause update anomalies.

•Transitive dependency describes a condition where A, B, and C are attributes of a relation such that if A → B and B → C, then C is transitively dependent on A via B (provided that A is not functionally dependent on B or C).

26

Page 27: NORMALIZATION

Example Transitive Dependency

•Consider functional dependencies in the StaffBranch relation (see Slide 12).

staffNo → sName, position, salary, branchNo, bAddress branchNo → bAddress

•Transitive dependency, branchNo → bAddress exists on staffNo via branchNo.

27

Page 28: NORMALIZATION

The Process of Normalization

•Formal technique for analyzing a relation based on its primary key and the functional dependencies between the attributes of that relation.

•Often executed as a series of steps. Each step corresponds to a specific normal form, which has known properties.

28

Page 29: NORMALIZATION

Identifying Functional Dependencies •Identifying all functional dependencies

between a set of attributes is relatively simple if the meaning of each attribute and the relationships between the attributes are well understood.

•This information should be provided by the enterprise in the form of discussions with users and/or documentation such as the users’ requirements specification.

29

Page 30: NORMALIZATION

Identifying Functional Dependencies •However, if the users are unavailable

for consultation and/or the documentation is incomplete then depending on the database application it may be necessary for the database designer to use their common sense and/or experience to provide the missing information.

30

Page 31: NORMALIZATION

Example - Identifying a set of functional dependencies for the StaffBranch relation•Examine semantics of attributes in

StaffBranch relation (see Slide 12). Assume that position held and branch determine a member of staff’s salary.

31

Page 32: NORMALIZATION

Example - Identifying a set of functional dependencies for the StaffBranch relation•With sufficient information available,

identify the functional dependencies for the StaffBranch relation as:

staffNo → sName, position, salary, branchNo, bAddressbranchNo → bAddressbAddress → branchNobranchNo, position → salarybAddress, position → salary

32

Page 33: NORMALIZATION

Example - Using sample data to identify functional dependencies.•Consider the data for attributes

denoted A, B, C, D, and E in the Sample relation (see Slide 33).

•Important to establish that sample data values shown in relation are representative of all possible values that can be held by attributes A, B, C, D, and E. Assume true despite the relatively small amount of data shown in this relation.

33

Page 34: NORMALIZATION

Example - Using sample data to identify functional dependencies.

34

Page 35: NORMALIZATION

Example - Using sample data to identify functional dependencies.•Function dependencies between

attributes A to E in the Sample relation.

A C (fd1)C A (fd2)B D (fd3)A, B E (fd4)

35

Page 36: NORMALIZATION

Identifying the Primary Key for a Relation using Functional Dependencies•Main purpose of identifying a set of

functional dependencies for a relation is to specify the set of integrity constraints that must hold on a relation.

•An important integrity constraint to consider first is the identification of candidate keys, one of which is selected to be the primary key for the relation.

36

Page 37: NORMALIZATION

Example - Identify Primary Key for StaffBranch Relation• StaffBranch relation has five functional

dependencies (see Slide 31).

• The determinants are staffNo, branchNo, bAddress, (branchNo, position), and (bAddress, position).

• To identify all candidate key(s), identify the attribute (or group of attributes) that uniquely identifies each tuple in this relation.

37

Page 38: NORMALIZATION

Example - Identifying Primary Key for StaffBranch Relation

•All attributes that are not part of a candidate key should be functionally dependent on the key.

•The only candidate key and therefore primary key for StaffBranch relation, is staffNo, as all other attributes of the relation are functionally dependent on staffNo.

38

Page 39: NORMALIZATION

Example - Identifying Primary Key for Sample Relation

•Sample relation has four functional dependencies (see Slide 31).

•The determinants in the Sample relation are A, B, C, and (A, B). However, the only determinant that functionally determines all the other attributes of the relation is (A, B).

•(A, B) is identified as the primary key for this relation.

39

Page 40: NORMALIZATION

The Process of Normalization•As normalization proceeds, the

relations become progressively more restricted (stronger) in format and also less vulnerable to update anomalies.

40

Page 41: NORMALIZATION

The Process of Normalization

41

Page 42: NORMALIZATION

The Process of Normalization

42

Page 43: NORMALIZATION

Unnormalized Form (UNF)

•A table that contains one or more repeating groups.

•To create an unnormalized table ▫Transform the data from the

information source (e.g. form) into table format with columns and rows.

43

Page 44: NORMALIZATION

First Normal Form (1NF)

•A relation in which the intersection of each row and column contains one and only one value.

44

Page 45: NORMALIZATION

UNF to 1NF

•Nominate an attribute or group of attributes to act as the key for the unnormalized table.

•Identify the repeating group(s) in the unnormalized table which repeats for the key attribute(s).

45

Page 46: NORMALIZATION

UNF to 1NF

•Remove the repeating group by▫Entering appropriate data into the

empty columns of rows containing the repeating data (‘flattening’ the table).

▫Or by▫Placing the repeating data along with a

copy of the original key attribute(s) into a separate relation.

46

Page 47: NORMALIZATION

Second Normal Form (2NF)•Based on the concept of full functional

dependency.•Full functional dependency indicates

that if ▫A and B are attributes of a relation, ▫B is fully dependent on A if B is

functionally dependent on A but not on any proper subset of A.

•A relation that is in 1NF and every non-primary-key attribute is fully functionally dependent on the primary key.

47

Page 48: NORMALIZATION

1NF to 2NF•Identify the primary key for the 1NF

relation.

•Identify the functional dependencies in the relation.

•If partial dependencies exist on the primary key remove them by placing then in a new relation along with a copy of their determinant.

48

Page 49: NORMALIZATION

Third Normal Form (3NF)•Based on the concept of transitive

dependency.•Transitive Dependency is a condition

where ▫A, B and C are attributes of a relation

such that if A B and B C, ▫then C is transitively dependent on A

through B. (Provided that A is not functionally dependent on B or C).

•A relation that is in 1NF and 2NF and in which no non-primary-key attribute is transitively dependent on the primary key.

49

Page 50: NORMALIZATION

2NF to 3NF•Identify the primary key in the 2NF

relation.

•Identify functional dependencies in the relation.

•If transitive dependencies exist on the primary key remove them by placing them in a new relation along with a copy of their dominant.

50

Page 51: NORMALIZATION

General Definitions of 2NF and 3NF •Second normal form (2NF)

▫A relation that is in first normal form and every non-primary-key attribute is fully functionally dependent on any candidate key.

•Third normal form (3NF)▫A relation that is in first and second

normal form and in which no non-primary-key attribute is transitively dependent on any candidate key.

51

Page 52: NORMALIZATION

Example: Normalization• Normalize the following table into 3NF

52

No Penyewa Nama PenyewaCR76

CR56Johan Karim

Aru Khan

NoPenyewa

NoRumah

AlamatRumah

MulaSewa

TamatSewa

HargaSewa

NoPemilik

NamaPemilik

CR76 CR76 

PG04 PG16

Subang Jaya, Selangor.Pasir Gudang, Johor.

1/7/93 1/9/00

31/8/00 1/9/01

750 850

C040 C093

Karim Fendi

Kasim SelamatCR56

 CR56 CR56

PG04 PG 36 PG 16

Subang Jaya, SelangorAmpang, SelangorPasir Gudang, Johor

20/3/90 21/6/93 25/1/00

19/6/93 23/1/00 30/8/00

750 1000 850

C040 C093 C093

Karim Fendi

Kasim SelamatKasim Selamat

Hubungan Penyewa

Hubungan PemilikRumahSewa

Page 53: NORMALIZATION

• Consider the dependencies

53

No Penyewa

No Rumah

Nama Penyewa

AlamatRumah

MulaSewa

TamatSewa

HargaSewa

NoPemilik

NamaPemilik

Kf1Kf

2 Kf3Kf4

Page 54: NORMALIZATION

Dependencies

Partial dependency:Kf2: NoPenyewa NamaPenyewaKf3: NoRumah AlamatPemilik,

HargaSewa, NoPemilik, NamaPemilik

Full Dependency:Kf1: NoPenyewa, NoRumah MulaSewa,

TamatSewa

Transitive Dependency:Kf4: NoPemilik NamaPemilik

54

Page 55: NORMALIZATION

55

Relationship in 1NF:

Penyewa (NoPenyewa, NamaPenyewa)PemilikRumahSewa (NoPenyewa, NoRumah,

AlamatRumah, MulaSewa, TamatSewa, Sewa, NoPemilik, NamaPemilik)

Page 56: NORMALIZATION

56

Relationship in 2NF:

Penyewa (NoPenyewa, NamaPenyewa)

Sewa (NoPenyewa, NoRumah, MulaSewa, TamatSewa)

RumahUntukSewa (NoRumah, AlamatRumah, HargaSewa, NoPemilik)

Page 57: NORMALIZATION

Relationship in 3NF:

Penyewa (NoPenyewa, NamaPenyewa)

Sewa (NoPenyewa, NoRumah, MulaSewa, TamatSewa)

RumahUntukSewa (NoRumah, AlamatRumah, HargaSewa, NoPemilik)

Pemilik (NoPemilik, NamaPemilik)

57

Page 58: NORMALIZATION

Example

58

Page 59: NORMALIZATION

• Create column headings for the table for each data item on the report (ignoring any calculated fields). A calculated field is one that can be derived from other information on the form. In this case total staff and average hourly rate.

• Enter sample data into table. (This data is not simply the data on the report but a representative sample. In this example it shows several employees working on several projects. In this company the same employee can work on different projects and at a different hourly rate.)

• Identify a key for the table (and underline it).• Remove duplicate data. (In this example, for the chosen key of Project

Code, the values for Project Code, Project Title, Project Manager and Project Budget are duplicated if there are two or more employees working on the same project. Project Code chosen for the key and duplicate data, associated with each project code, is removed. Do not confuse duplicate data with repeating attributes which is descibed in the next step.

59

Page 60: NORMALIZATION

Unnormalized table

60

Page 61: NORMALIZATION

• Transform a table of unnormalised data into first normal form (1NF). any repeating attributes to a new table. A repeating attribute is a data field within the UNF relation that may occur with multiple values for a single value of the key. The process is as follows: Identify repeating attributes.

• Remove these repeating attributes to a new table together with a copy of the key from the UNF table.

• Assign a key to the new table (and underline it). The key from the original unnormalised table always becomes part of the key of the new table. A compound key is created. The value for this key must be unique for each entity occurrence.

• Notes:• After removing the duplicate data the repeating attributes are easily identified.• In the previous table the Employee No, Employee Name, Department No,

Department Name and Hourly Rate attributes are repeating. That is, there is potential for more than one occurrence of these attributes for each project code. These are the repeating attributes and have been to a new table together with a copy of the original key (ie: Project Code).

• A key of Project Code and Employee No has been defined for this new table. This combination is unique for each row in the table.

61

Page 62: NORMALIZATION

62

Page 63: NORMALIZATION

• transform 1NF data into second normal form (2NF). Remove any -key attributes (partial Dependencies) that only depend on part of the table key to a new table. What has to be determined "is field A dependent upon field B or vice versa?" This means: "Given a value for A, do we then have only one possible value for B, and vice versa?" If the answer is yes, A and B should be put into a new relation with A becoming the primary key. A should be left in the original relation and marked as a foreign key.

• Ignore tables with (a) a simple key or (b) with no non-key attributes (these go straight to 2NF with no conversion).

• The process is as follows:• Take each non-key attribute in turn and ask the question: is this attribute

dependent on one part of the key?• If yes, remove the attribute to a new table with a copy of the part of the key it

is dependent upon. The key it is dependent upon becomes the key in the new table. Underline the key in this new table.

• If no, check against other part of the key and repeat above process• If still no, ie: not dependent on either part of the key, keep attribute in current

table.

63

Page 64: NORMALIZATION

• Notes:• The first table went straight to 2NF as it has a

simple key (Project Code).• Employee name, Department No and Department

Name are dependent upon Employee No only. Therefore, they were moved to a new table with Employee No being the key.

• However, Hourly Rate is dependent upon both Project Code and Employee No as an employee may have a different hourly rate depending upon which project they are working on. Therefore it remained in the original table.

64

Page 65: NORMALIZATION

65

Page 66: NORMALIZATION

• data in second normal form (2NF) into third normal form (3NF). Remove to a new table any non-key attributes that are more dependent on other non-key attributes than the table key.

• What has to be determined is "is field A dependent upon field B or vice versa?" This means: "Given a value for A, do we then have only one possible value for B, and vice versa?" If the answer is yes, then A and B should be put into a new relation, with A becoming the primary key. A should be left in the original relation and marked as a foreign key.

• Ignore tables with zero or only one non-key attribute (these go straight to 3NF with no conversion).

66

Page 67: NORMALIZATION

• The process is as follows: If a non-key attribute is more dependent on another non-key attribute than the table key:

• Move the dependent attribute, together with a copy of the non-key attribute upon which it is dependent, to a new table.

• Make the non-key attribute, upon which it is dependent, the key in the new table. Underline the key in this new table.

• Leave the non-key attribute, upon which it is dependent, in the original table and mark it a foreign key (*).

• Notes:• The project team table went straight from 2NF to 3NF as it only

has one non-key attribute.• Department Name is more dependent upon Department No than

Employee No and therefore was moved to a new table. Department No is the key in this new table and a foreign key in the Employee table.

67

Page 68: NORMALIZATION

68

Page 69: NORMALIZATION

69

Page 70: NORMALIZATION

• First normal form: A table is in the first normal form if it contains no repeating columns.

• Second normal form: A table is in the second normal form if it is in the first normal form and contains only columns that are dependent on the whole (primary) key.

• Third normal form: A table is in the third normal form if it is in the second normal form and all the non-key columns are dependent only on the primary key. If the value of a non-key column is dependent on the value of another non-key column we have a situation known as transitive dependency. This can be resolved by removing the columns dependent on non-key items to another table.

70

Page 71: NORMALIZATION

Exercise • A college maintains details of its lecturers' subject area skills. These

details comprise:• Lecturer Number• Lecturer Name • Lecturer Grade• Department Code • Department Name• Subject Code • Subject Name • Subject Level • Assume that each lecturer may teach many subjects but may not

belong to more than one department. • Subject Code, Subject Name and Subject Level are repeating fields. • Normalise this data to Third Normal Form.

71

Page 72: NORMALIZATION

Answer ;• UNF • Lecturer Number ,Lecturer Name, Lecturer Grade, Department Code,Department

Name, Subject Code, Subject Name, Subject Level• 1NF • Lecturer Number, Lecturer Name, Lecturer Grade, Department Code, Department

Name• Lecturer Number , Subject Code, Subject Name,Subject Level• 2NF • Lecturer Number, Lecturer Name, Lecturer Grade, Department Code, Department

Name• Lecturer Number, Subject Code • Subject Code, Subject Name, Subject Level• 3NF • Lecturer Number,Lecturer Name,Lecturer Grade• *Department Code• Department Code, Department Name• Lecturer Number, Subject Code • Subject Code,Subject Name,Subject Level

72

Page 73: NORMALIZATION

Exercise

staffNo branchNo branchAddress name position hoursPerWee

k

S4555 B002 City Center Plaza, Seattle, WA

98122

Ellen

Layman

Assistant 16

S4555 B004 16 - 14th Avenue, Seattle, WA

98128

Ellen

Layman

Assistant 9

S4612 B002 City Center Plaza, Seattle, WA

98122

Dave

Sinclair

Assistant 14

S4612 B004 16 - 14th Avenue, Seattle, WA

98128

Dave

Sinclair

Assistant 10

73

Page 74: NORMALIZATION

• Why is this table not in 2NF?• Describe and illustrate the process of

normalising the data shown in this table to third normal form (3NF).

• Identify the primary , (alternate) and foreign keys in your 3NF relations.

74