Database Normalization What is Normalization n Normalization allows
Database Normalization
What is Normalization n Normalization allows us to organize data so that it: • Allows faster access (dependencies make sense) • Reduced space (less redundancy)
Data Normalization n Primarily a tool to validate and improve a logical design so that it satisfies certain constraints that avoid unnecessary duplication of data n The process of decomposing relations with anomalies to produce smaller, wellstructured relations
Results of Normalization n Removes the following modification anomalies (integrity errors) with the database • Insertion • Deletion • Update
ANOMALIES n n n Insertion • inserting one fact in the database requires knowledge of other facts unrelated to the fact being inserted Deletion • Deleting one fact from the database causes loss of other unrelated data from the database Update • Updating the values of one fact requires multiple changes to the database
Insertion Anomaly q. Suppose we want to enter the new Job 'Cooker' having $800 as a salary. We cannot enter the data into the EMPLOYEE relation until an employee enrolls in this Job. q. This restriction seems silly. Why should we have to wait until someone takes the Job before we can record its salary? This situation is called insertion anomaly. It is due to the relational model constraint that disables 'Null values' for the primary key (EMPID). EMPLOYEE EMPID NAME JOB 200 300 600 700 450 Smith John George Jean Mike Driver Guardian Gardener SALAR Y 1500 2000 2500
Insertion Anomaly These anomalies can be eliminated by decomposing the employee relation into the two ones shown in the following figure. EMP-JOB Emp. Id 200 300 600 700 450 Name Smith John George Jean Mike JOB-SAL Job driving Guardian Gardener Job Salary Driving Guardian 1500 2000 Gardener 2500
ANOMALIES EXAMPLES TABLE: COURSE# SECTION# C_NAME CIS 564 072 Database Design CIS 564 073 Database Design CIS 570 072 Oracle Forms CIS 564 074 Database Design
ANOMALIES EXAMPLES Insertion: Suppose our College has approved a new course called CIS 563: SQL & PL/SQL. Can this information about the new course be entered (inserted) into the table COURSE in its present form? COURSE# SECTION# C_NAME CIS 564 072 Database Design CIS 564 073 Database Design CIS 570 072 Oracle Forms CIS 564 074 Database Design
Deletion anomaly For the data in following figure (where we have only one driver), if we delete the employee number 200, we will lose not only that this employee is a driver, but also that driving costs' $1500. This is called a deletion anomaly; we are losing more information than we want to. We lose facts about two entities with one deletion. EMPLOYEE EMPID 200 300 600 700 450 NAME Smith John George Jean Mike JOB Driver Guardian Gardener SALARY 1500 2000 2500
Update anomaly Example 1: If the salary of the job "Guardian" changes from $2000 to $2100, the updating operation must be repeated with each Guardian employee. EMPLOYEE EMPID 200 300 600 700 450 NAME Smith John George Jean Mike JOB Driver Guardian Gardener SALARY 1500 2000 2500
ANOMALIES EXAMPLES Update: Suppose the course name (C_Name) for CIS 564 got changed to Database Management. How many times do you have to make this change in the COURSE table in its current form? COURSE# SECTION# C_NAME CIS 564 072 Database Design CIS 564 073 Database Design CIS 570 072 Oracle Forms CIS 564 074 Database Design
ANOMALIES Following Figure shows the relations issued from the decomposition phase. Now the previous anomalies are avoided so then we can : q Delete the employee Smith from EMP-JOB without losing the fact that driving has a salary $1500, q Insert a new job in the JOB-SAL table without enrolled employees, q Modify only one tuple in the JOB-SAL table instead of many tuples in the initial relation. EMP-JOB Emp. Id 200 300 600 700 450 Name Smith John George Jean Mike JOB-SAL Job driving Guardian Gardener Job Salary Driving Guardian 1500 2000 Gardener 2500
ANOMALIES n n n So, a table (relation) is a stable (‘good’) table only if it is free from any of these anomalies at any point in time. You have to ensure that each and every table in a database is always free from these modification anomalies. And, how do you ensure that? ‘Normalization’ theory helps.
NORMAL FORMS ü ü ü n n n 1 NF 2 NF 3 NF BCNF (Boyce-Codd Normal Form) 4 NF 5 NF
Normal Forms n n n Normalization is done through changing or transforming data into various Normal Forms. There are 5 Normal Forms but we almost never use 4 NF or 5 NF. We will only be concerned with 1 NF, 2 NF, and 3 NF.
Normal Forms n For a database to be in a normal form, it must meet all requirements of the previous forms: • Eg. For a database to be in 2 NF, it must already be in 1 NF. For a database to be in 3 NF, it must already be in 1 NF and 2 NF.
Sample Data n Data that is not atomic means: • • We can’t easily sort the data We can’t easily search or index the data We can’t easily change the data We can’t easily reference the data in other tables
Sample Data • We still can’t easily sort, search, or index our employees. • What if a manager has more than 2 employees, 100 employees? We’d need to add columns to our database just for these cases. • It is still hard to reference our employees in other tables.
First Normal Form n 1 NF means that we must: • Eliminate duplicate columns from the same table, and • Create separate tables for each group of related data into separate tables, each with a unique row identifier (primary key) n Let’s get started by making our columns atomic…
Primary Key n n The best primary key would be the Employee column. Every employee only has one manager, therefore an employee is unique. Students should now say that the Employee is the Primary Key since there are now multiple manager values in the table. Only Employee is unique.
First Normal Form n n Congratulations! The fact that all our data and columns is atomic and we have a primary key means that we are in 1 NF! Students should now say that the Employee is the Primary Key since there are now multiple manager values in the table. Only Employee is unique.
Moving to Second Normal Form n A database in 2 NF must also be in 1 NF: • Data must be atomic • Every row (or tuple) must have a unique primary key n Plus: • Subsets of data that apply to multiple rows (repeating data) are moved to separate tables
Moving to Second Normal Form n n The Cust. ID determines all the data in the row, but U. S. Zip codes determines the City and State. (eg. A given Zip code can only belong to one city and state so storing Zip codes with a City and State is redundant) This means that City and State are Functionally Dependent on the value in Zip code and not only the primary key.
Moving to Second Normal Form n n To be in 2 NF, this repeating data must be in its own table. So: • Let’s create a Zip code table that maps Zip codes to their City and State. • Note that Canadian Postal Codes are different: the same city and state can have many different postal codes.
Zip Code Table Customer Table
Advantages of 2 NF n n n Saves space in the database by reducing redundancies If a customer calls, you can just ask them for their Zip code and you’ll know their city and state! (No more spelling mistakes) If a City name changes, we only need to make one change to the database.
Summary So Far… n 1 NF: • All data is atomic • All rows have a unique primary key n 2 NF: • Data is in 1 NF • Subsets of data in multiple columns are moved to a new table • These new tables are related using foreign keys
Moving to 3 NF n To be in 3 NF, a database must be: • In 2 NF • All columns must be fully functionally dependent on the primary key (There are no transitive dependencies)
n n Maybe price is dependent on the Prod. ID and Quantity: The more you buy of a given product the cheaper that product becomes! So we ask the business manager and she tells us that this is the case.
n We say that Price has a transitive dependency on Prod. ID and Quantity. • This means that Price isn’t just determined by the Order. ID. It is also determined by the size (or quantity) of the order (and of course what is ordered).
n n Congratulations! We’re now in 3 NF! We can also quickly figure out what price to offer our customers for any quantity they want.
To Summarize (again) n A database is in 3 NF if: • It is in 2 NF • It has no transitive dependencies A transitive dependency exists when one attribute (or field) is determined by another non-key attribute (or field) n We remove fields with a transitive dependency to a new table and link them by a foreign key. n
Summarizing n A database is in 2 NF if: • It is in 1 NF • There is no repeating data in its tables. n Put another way, if we use a composite primary key, then all attributes are dependent on all parts of the key.
And Finally… n A database is in 1 NF if: • All its attributes are atomic (meaning they contain only a single unit or type of data), and • All rows have a unique primary key.
- Slides: 52