Download presentation
Presentation is loading. Please wait.
1
Chapter 9 Part-2: Normalization
Database Design Chapter 9 Part-2: Normalization
2
Outline Part Two Normalization 1NF 2NF 3NF
3
Study the problems with this table.
This is why we need Normalization! 12/2/2015 Carla called to inquire about her appointment time on 12/2/2015. She also gives us her new phone number: Update Anomaly Mike called to cancel his appointment. Delete Anomaly Sue calls to make another appointment for December 15th. Insert Anomaly Appointment Table Appt No Appt Date Appt Time Planned Duration Appt Type Patient ID First Nm Last Nm Phone Doctor ID Doctor Nm 1 12/1/2015 3:00 AM 1.00 Physical 466927 Lisa Garcia C678 Chapman 2 0.25 Shot 456789 Sue Carey A528 Lopez 3 3:15 AM 0.50 Flu 194756 Brandon Pierre S626 Smith 4 12/2/2015 10:00 AM Migraine 329657 Marcus Schwartz 5 10:15 AM 987453 Mike Jones G123 Gray 6 10:30 AM 384788 Tonya Johnson 7 10:45 AM 438754 Iliana Hnatt 8 11:00 AM 345875 Carla Basich 9 12/3/2015 10 9:00 AM
4
Normalization (def) the process of converting complex data structures into simple, stable data structures. Every non-primary key attribute depends upon the whole primary key and nothing but the primary key. Purpose: Create well-structured relations/tables Why? Data Redundancy = Data Quality & Integrity Result: where we can insert, modify, and delete the rows without errors or inconsistencies.
5
Normalization 1NF 2NF 3NF Normal Form Definition
No Multi-Valued Attributes! 2NF No Partial Dependencies! The table is in 1NF, and … every non-key attribute is functionally dependent on the entire primary key 3NF No Transitive Dependencies! The table is in 2NF, and … each non-key attribute is not functionally dependent on another non-key attribute
6
First Normal Form (1NF) A table is in 1NF if it has no multi-valued attributes. Original Table: EMP (EmpID, LastName, FirstName, Title, Dependents) Bad Solution: creating a “Repeating field” is a poor design decision! EMP (EmpID, LastName, FirstName, Title, Dep1, Dep2, Dep3) Employee Table EmpID LastName FirstName Title Dependents 111 Smith Robert Accountant Bobbie, Sue 222 Jones Leo Programmer 333 Lopez Trent Sales Rep Trent Jr, Trevor, Sue Employee Table EmpID LastName FirstName Title Dep1 Dep2 Dep3 111 Smith Robert Accountant Bobbie Sue 222 Jones Leo Programmer 333 Lopez Trent Sales Rep Trent Jr Trevor
7
First Normal Form (1NF) 1NF: The table has no multi-valued attributes
Original Table: Employee (EmpID, LastName, FirstName, Title, Dependents) Solution – create a new table! Employee (EmpID, LastName, FirstName, Title) EmpDep (EmpID, DepName) Employee Table EmpID LastName FirstName Title Dependents 111 Smith Robert Accountant Bobbie, Sue 222 Jones Leo Programmer 333 Lopez Trent Sales Rep Trent Jr, Trevor, Sue EmpDep Table EmpID DepName 111 Bobbie Sue 333 Trent Jr Trevor Employee Table EmpID LastName FirstName Title 111 Smith Robert Accountant 222 Jones Leo Programmer 333 Lopez Trent Sales Rep
8
Functional Dependency
B A (def) Field A is functionally dependent on Field B if for each value of B, there is only 1 corresponding value of A. “Field B functionally determines Field A” i.e. If we know the value of B, we can obtain the value of A. B A OrderDate OrderID
9
Functional Dependency
B A (def) Field A is functionally dependent on Field B if for each value of B, there is only 1 corresponding value of A. Functional Dependency involves a one-to-one relationship between the values of fields LastName FirstName BirthDate Hometown StudentID StudentID LastName FirstName BirthDate Hometown
10
Second Normal Form (2NF)
A table is in 2NF if each non-key attribute is functionally dependent on the entire primary key. Original Table: 1NF ________________ (CatalogID, ProductID, Price, CatIssueDate) What is the PK? What is the table name? Analyze it! CatalogID, ProductID _________________ CatalogID _________________ ProductID _________________
11
Second Normal Form (2NF)
Solution: 2NF Catalog (CatalogID, CatIssueDate) CatalogProduct (CatalogID, ProductID, Price)
12
Third Normal Form (3NF) A table is in 2NF if no non-key attribute is functionally dependent on any other non-key attribute Original Table: 2NF ________________ (AcctNo, Address, State, ZipCode) What is the PK? What is the table name? Analyze it! Address _________________ State _________________ ZipCode _________________
13
Third Normal Form (3NF) Solution: Account (AcctNo, Address, ZipCode)
Zip (ZipCode, State)
14
Model the Courses Completed Report
15
Transform this report into 1NF
Courses Completed Report EmpID Name Dept Salary Course Date Completed Dept Phone Ext. 100 John MKTG 42000 SPSS 6/19/2013 5325 Surveys 11/3/2014 140 Sue ACCT 41000 Tax Acc 12/1/2014 4422 110 Bob INFO 70000 Java 3/21/2013 7373 C# 10/23/2015 190 Alex FINA 65000 Investmts. 2/1/2013 4477 150 Jennifer 49000 3/11/2012 Oracle 5/19/2013 Notice in this report that Employees can take multiple courses.
16
One way to represent the report data
ERROR: This solution contains a multi-valued attribute and is a violations of 1NF. EmpID Name Dept Salary Course Date Completed Dept Phone Ext. 100 John MKTG 42000 SPSS, Surveys 6/19/2013 5325 140 Sue ACCT 41000 Tax Acc 12/1/2014 4422 110 Bob INFO 70000 Java, C# 3/21/2013 7373 190 Alex FINA 65000 Investmts. 2/1/2013 4477 150 Jennifer 49000 Java, Oracle 3/11/2012 I already taught you how to quickly resolved a Multi-Valued Atrribute. However, let’s assume you do not know that and decide to just flatten the report data as shown on the next slide. Let’s proceed with this approach (in order to study all 3 notrmal forms).
17
Now, it’s in 1NF: No Multi-Values Attributes
_________(EmpID, Name, Dept, Salary, Course, Date Completed, DeptPhoneExt) EmpID Name Dept Salary Course Date Completed Dept Phone Ext. 100 John MKTG 42000 SPSS 6/19/2013 5325 Surveys 11/3/2014 140 Sue ACCT 41000 Tax Acc 12/1/2014 4422 110 Bob INFO 70000 Java 3/21/2013 7373 C# 10/23/2015 190 Alex FINA 65000 Investmts. 2/1/2013 4477 150 Jennifer 49000 3/11/2012 Oracle 5/19/2013 What is the PK? What is the table’s name?
18
Transform this table from 1NF to 2NF
Ensure that all non-key attributes are functionally dependent on the entire primary key! – i.e. No Partial Dependencies EmpCourse (EmpID, Course, Name, Dept, Salary, Date Completed, DeptPhoneExt) Original Table: EmpID Course Name Dept Salary Date Completed Dept Phone Ext. 100 SPSS John MKTG 42000 6/19/2013 5325 Surveys 11/3/2014 140 Tax Acc Sue ACCT 41000 12/1/2014 4422 110 Java Bob INFO 70000 3/21/2013 7373 C# 10/23/2015 190 Investmts. Alex FINA 65000 2/1/2013 4477 150 Jennifer 49000 3/11/2012 Oracle 5/19/2013 1NF Analyze it! EmpID, Course EmpID Course
19
Now, both are in 2NF: each non-key attribute is functionally dependent on the entire primary key.
Solution: 2NF Employee (EmpID, Name, Dept, Salary, DeptPhoneExt) EmpID Name Dept Salary Dept Phone Ext. 100 John MKTG 42000 5325 140 Sue ACCT 41000 4422 110 Bob INFO 70000 7373 190 Alex FINA 65000 4477 150 Jennifer 49000 EmpCourse( EmpID, Course, Date Completed EmpID Course Date Completed 100 SPSS 6/19/2013 Surveys 11/3/2014 140 Tax Acc 12/1/2014 110 Java 3/21/2013 C# 10/23/2015 190 Investmts. 2/1/2013 150 3/11/2012 Oracle 5/19/2013 Tip: _____________
20
Transform these tables from 2NF to 3NF
Ensure that no non-key attribute is functionally dependent on any other non-key attribute - i.e. no transitive dependencies Employee EmpCourse EmpID Name Dept Salary Dept Phone Ext. 100 John MKTG 42000 5325 140 Sue ACCT 41000 4422 110 Bob INFO 70000 7373 190 Alex FINA 65000 4477 150 Jennifer 49000 EmpID Course Date Completed 100 SPSS 6/19/2013 Surveys 11/3/2014 140 Tax Acc 12/1/2014 110 Java 3/21/2013 C# 10/23/2015 190 Investmts. 2/1/2013 150 3/11/2012 Oracle 5/19/2013 Name ____________ Dept ____________ Salary ____________ DeptPhExt ____________ Tip: _____________
21
Now, these are in 3NF: no non-key attribute is functionally dependent on any other non-key attribute
Solution: 3NF EmpCourse(EmpID, Course, Date Completed Employee(EmpID, Name, Dept, Salary) EmpID Course Date Completed 100 SPSS 6/19/2013 Surveys 11/3/2014 140 Tax Acc 12/1/2014 110 Java 3/21/2013 C# 10/23/2015 190 Investmts. 2/1/2013 150 3/11/2012 Oracle 5/19/2013 EmpID Name Dept Salary 100 John MKTG 42000 140 Sue ACCT 41000 110 Bob INFO 70000 190 Alex FINA 65000 150 Jennifer 49000 Department(Dept, DeptPhoneExt) Dept DeptPhone Ext. MKTG 5325 ACCT 4422 INFO 7373 FINA 4477
22
Using Normalization we learned…
that to represent the data on this report in a relational database, we will need to create 3 separate tables. EmpID Name Dept Salary 100 John MKTG 42000 140 Sue ACCT 41000 110 Bob INFO 70000 190 Alex FINA 65000 150 Jennifer 49000 Employee EmpID Course Date Completed 100 SPSS 6/19/2013 Surveys 11/3/2014 140 Tax Acc 12/1/2014 110 Java 3/21/2013 C# 10/23/2015 190 Investmts. 2/1/2013 150 3/11/2012 Oracle 5/19/2013 EmpCourse Dept Dept Phone Ext. MKTG 5325 ACCT 4422 INFO 7373 FINA 4477 Dept
23
Here's our final solution!
Domain Class Diagram Table Notation Employee (EmpID, Name, Dept, Salary) EmpCourse (EmpID, Course, Date Completed Department (Dept, DeptPhoneExt) EmpCourse EmpID Course DateCompleted PK 0..* Employee EmpID - PK Name DeptCode Salary 1..1 Dept DeptCode - PK DeptPhoneExt 1..* Denote the foreign keys 1..1
24
Summarizing
25
Normalization 1NF 2NF 3NF Normal Form Definition
No Multi-Valued Attributes! e.g. EMP(EmpID, LastName, FirstName, Title, Dependents) 2NF No Partial Dependencies! The table is in 1NF, and … every non-key attribute is functionally dependent on the entire primary key e.g. EmpCourse(EmpID, Course, Name, Dept, Salary, DtCompleted, DeptPhoneExt) 3NF No Transitive Dependencies! The table is in 2NF, and … each non-key attribute is not functionally dependent on another non-key attribute e.g. Employee(EmpID, Name, Dept, Salary, DeptPhoneExt) If EmpID Dept & Dept DeptPhoneExt, then EmpID DeptPhoneExt
26
More Practice Examples
27
Normalization Practice
OrderProduct Table Order No Cust No Name Addr City St Zip OrderDt Promised Dt Prod No Desc Qty Ord Unit Price 61384 1273 Cont. Designs 123 Oak Austin TX 28384 11/04/18 11/21/18 M128 Bookcase 4 200 B381 Cabinet 2 150 R210 Table 1 500 62890 3891 J Consultants 523 Pine Waco 76712 11/15/18 A891 Chair 300 8 63129 12/10/18 12/29/18 6 OrdProd (OrderNo, ProdNo, CustNo, Name, Addr, City, St, Zip, OrderDt, PromisedDt, Desc, QtyOrd, UnitPrice) It’s already in 1NF …but you need to understand why! Convert this to 2NF. How many tables do you now have? Convert this to 3NF. How many tables do you now have?
28
Normalization Practice
Appointment Table Appt No Appt Date Appt Time Planned Duration Appt Type Patient ID First Nm Last Nm Phone Doctor ID Doctor Nm 1 12/1/2015 3:00 AM 1.00 Physical 466927 Lisa Garcia C678 Chapman 2 0.25 Shot 456789 Sue Carey A528 Lopez 3 3:15 AM 0.50 Flu 194756 Brandon Pierre S626 Smith 4 12/2/2015 10:00 AM Migraine 329657 Marcus Schwartz 5 10:15 AM 987453 Mike Jones G123 Gray 6 10:30 AM 384788 Tonya Johnson 7 10:45 AM 438754 Iliana Hnatt 8 11:00 AM 345875 Carla Basich 9 12/3/2015 10 9:00 AM Appt (ApptNo, ApptDt, ApptTm, PlannedDur, ApptType, PatientID, FirstNm, LastNm, Phone, DoctorID, DoctorNm) It’s already in 1NF, and 2NF …but you need to understand why! Convert this to 3NF. How many tables do you now have?
29
Normalization Practice
Invoice Table InvoiceNo InvoiceDate Item CustID LastName FirstName 1 01/15/2018 Bicycle, Jump rope 466927 Garcia Lisa 2 03/21/2018 Bicycle 456789 Carey Sue 3 03/25/2018 Notebook 194756 Pierre Brandon 4 06/02/2018 Jump rope 329657 Schwartz Marcus 5 08/19/2018 Notebook, Jump rope, Bicycle 987453 Jones Mike Invoice (InvoiceNo, InvoiceDate, Item, CustID, LastName, FirstName) Put this table in 1NF. Proceed to put it in 2NF, then 3NF.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.