1 NORMALIZATION 2 Objectives The purpose of normalization
1 NORMALIZATION
2 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.
3 • 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.
4 • How to identify the most commonly used normal forms, namely First Normal Form (1 NF), Second Normal Form (2 NF), and Third Normal Form (3 NF). • The problems associated with relations that break the rules of 1 NF, 2 NF, or 3 NF. • How to represent attributes shown on a form as 3 NF relations using normalization.
5 Purpose of Normalization • Normalization is a technique for producing a set of suitable relations that support the data requirements of an enterprise.
6 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.
7 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.
8 How Normalization Supports Database Design
9 Data Redundancy and Update Anomalies • Major aim of relational database design is to group attributes into relations to minimize data redundancy.
10 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.
11 Data Redundancy and Update Anomalies • Problems associated with data redundancy are illustrated by comparing the Staff and Branch relations with the Staff. Branch relation.
12 Data Redundancy and Update Anomalies
13 Data Redundancy and Update Anomalies • Staff. Branch 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 (branch. No) is repeated in the Staff relation, to represent where each member of staff is located.
14 Data Redundancy and Update Anomalies • Relations that contain redundant information may potentially suffer from update anomalies. • Types of update anomalies include ▫ Insertion ▫ Deletion ▫ Modification
15 • 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.
16 • Delete Anomaly exists when certain attributes are lost because of the deletion of other attributes. For example, consider what happens if Student S 30 is the last student to leave the course - All information about the course is lost.
17 • 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.
18 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.
19 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.
20 An Example Functional Dependency
21 Example Functional Dependency that holds for all Time • Consider the values shown in staff. No and s. Name attributes of the Staff relation (see Slide 12). • Based on sample data, the following functional dependencies appear to hold. staff. No → s. Name → staff. No • However, the only functional dependency that remains true for all possible values for the staff. No and s. Name attributes of the Staff relation is: staff. No → s. Name
22 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.
23 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.
24 Example Full Functional Dependency • Exists in the Staff relation (see Slide 12). staff. No, s. Name → branch. No • True - each value of (staff. No, s. Name) is associated with a single value of branch. No. • However, branch. No is also functionally dependent on a subset of (staff. No, s. Name), namely staff. No. Example above is a partial dependency.
25 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.
26 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).
27 Example Transitive Dependency • Consider functional dependencies in the Staff. Branch relation (see Slide 12). staff. No → s. Name, position, salary, branch. No, b. Address branch. No → b. Address • Transitive dependency, branch. No → b. Address exists on staff. No via branch. No.
28 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.
29 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.
30 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.
31 Example - Identifying a set of functional dependencies for the Staff. Branch relation • Examine semantics of attributes in Staff. Branch relation (see Slide 12). Assume that position held and branch determine a member of staff’s salary.
32 Example - Identifying a set of functional dependencies for the Staff. Branch relation • With sufficient information available, identify the functional dependencies for the Staff. Branch relation as: staff. No → s. Name, position, salary, branch. No, b. Address branch. No → b. Address → branch. No, position → salary b. Address, position → salary
33 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.
34 Example - Using sample data to identify functional dependencies.
35 Example - Using sample data to identify functional dependencies. • Function dependencies between attributes A to E in the Sample relation. A C C A B D A, B E (fd 1) (fd 2) (fd 3) (fd 4)
36 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.
37 Example - Identify Primary Key for Staff. Branch Relation • Staff. Branch relation has five functional dependencies (see Slide 31). • The determinants are staff. No, branch. No, b. Address, (branch. No, position), and (b. Address, position). • To identify all candidate key(s), identify the attribute (or group of attributes) that uniquely identifies each tuple in this relation.
38 Example - Identifying Primary Key for Staff. Branch 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 Staff. Branch relation, is staff. No, as all other attributes of the relation are functionally dependent on staff. No.
39 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.
40 The Process of Normalization • As normalization proceeds, the relations become progressively more restricted (stronger) in format and also less vulnerable to update anomalies.
41 The Process of Normalization
42 The Process of Normalization
43 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.
44 First Normal Form (1 NF) • A relation in which the intersection of each row and column contains one and only one value.
45 UNF to 1 NF • 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).
46 UNF to 1 NF • 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.
47 Second Normal Form (2 NF) • 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 1 NF and every nonprimary-key attribute is fully functionally dependent on the primary key.
48 1 NF to 2 NF • Identify the primary key for the 1 NF 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.
49 Third Normal Form (3 NF) • 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 1 NF and 2 NF and in which no non-primary-key attribute is transitively dependent on the primary key.
50 2 NF to 3 NF • Identify the primary key in the 2 NF 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.
51 General Definitions of 2 NF and 3 NF • Second normal form (2 NF) ▫ 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 (3 NF) ▫ 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.
52 Example: Normalization • Normalize the following table into 3 NF Hubungan Penyewa No Penyewa CR 76 CR 56 Nama Penyewa Johan Karim Aru Khan Hubungan Pemilik. Rumah. Sewa No Penyewa No Rumah Alamat Rumah Mula Sewa Tamat Sewa Harga Sewa No Pemilik Nama Pemilik CR 76 PG 04 1/7/93 31/8/00 750 C 040 Karim Fendi CR 76 PG 16 Subang Jaya, Selangor. Pasir Gudang, Johor. Subang Jaya, Selangor Ampang, Selangor Pasir Gudang, Johor 1/9/00 1/9/01 850 C 093 CR 56 PG 04 CR 56 PG 36 CR 56 PG 16 20/3/90 19/6/93 750 C 040 21/6/93 23/1/00 1000 C 093 25/1/00 30/8/00 850 C 093 Kasim Selamat Karim Fendi Kasim Selamat
53 • Consider the dependencies No Penyewa No Rumah Nama Penyewa Alamat Rumah Mula Sewa Tamat Sewa Harga Sewa No Pemilik Nama Pemilik Kf 1 Kf 2 Kf 3 Kf 4
54 Dependencies Partial dependency: Kf 2: No. Penyewa Nama. Penyewa Kf 3: No. Rumah Alamat. Pemilik, Harga. Sewa, No. Pemilik, Nama. Pemilik Full Dependency: Kf 1: No. Penyewa, No. Rumah Mula. Sewa, Tamat. Sewa Transitive Dependency: Kf 4: No. Pemilik Nama. Pemilik
55 Relationship in 1 NF: Penyewa (No. Penyewa, Nama. Penyewa) Pemilik. Rumah. Sewa (No. Penyewa, No. Rumah, Alamat. Rumah, Mula. Sewa, Tamat. Sewa, No. Pemilik, Nama. Pemilik)
56 Relationship in 2 NF: Penyewa (No. Penyewa, Nama. Penyewa) Sewa (No. Penyewa, No. Rumah, Mula. Sewa, Tamat. Sewa) Rumah. Untuk. Sewa (No. Rumah, Alamat. Rumah, Harga. Sewa, No. Pemilik)
57 Relationship in 3 NF: Penyewa (No. Penyewa, Nama. Penyewa) Sewa (No. Penyewa, No. Rumah, Mula. Sewa, Tamat. Sewa) Rumah. Untuk. Sewa (No. Rumah, Alamat. Rumah, Harga. Sewa, No. Pemilik) Pemilik (No. Pemilik, Nama. Pemilik)
58 Example
59 • 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.
60 Unnormalized table
61 • Transform a table of unnormalised data into first normal form (1 NF). 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.
62
63 • transform 1 NF data into second normal form (2 NF). 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 2 NF 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.
64 • Notes: • The first table went straight to 2 NF 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.
65
66 • data in second normal form (2 NF) into third normal form (3 NF). 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 3 NF with no conversion).
67 • 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 2 NF to 3 NF 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.
68
69
70 • 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 nonkey 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.
71 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.
72 Answer ; • UNF • Lecturer Number , Lecturer Name, Lecturer Grade, Department Code, Department Name, Subject Code, Subject Name, Subject Level • 1 NF • Lecturer Number, Lecturer Name, Lecturer Grade, Department Code, Department Name • Lecturer Number , Subject Code, Subject Name, Subject Level • 2 NF • Lecturer Number, Lecturer Name, Lecturer Grade, Department Code, Department Name • Lecturer Number, Subject Code • Subject Code, Subject Name, Subject Level • 3 NF • Lecturer Number, Lecturer Name, Lecturer Grade • *Department Code • Department Code, Department Name • Lecturer Number, Subject Code • Subject Code, Subject Name, Subject Level
73 Exercise staff. No branch. Address S 4555 B 002 City Center Plaza, Seattle, WA Ellen 98122 S 4555 B 004 B 002 position hours. Per. Week Assistant 16 Assistant 9 Layman 16 - 14 th Avenue, Seattle, WA Ellen 98128 S 4612 name Layman City Center Plaza, Seattle, WA Dave Sinclair Assistant 14 98122 S 4612 B 004 16 - 14 th Avenue, Seattle, WA Dave Sinclair Assistant 98128 10
74 • Why is this table not in 2 NF? • Describe and illustrate the process of normalising the data shown in this table to third normal form (3 NF). • Identify the primary , (alternate) and foreign keys in your 3 NF relations.
- Slides: 74