24
PC Store Normalization Prepared by : Mais M. Fatayer Arab Open University Riyadh – KSA

Normalization Chapter3

Embed Size (px)

Citation preview

Page 1: Normalization Chapter3

PC Store Normalization

Prepared by : Mais M. FatayerArab Open University

Riyadh – KSA

Page 2: Normalization Chapter3

@PC Store Owner

Database Designer and Developer(YOU)

I need a system that organize my work in the

store . But initially tell me the costs?

Initially I need to make a simple study, but lets

discuss what you want your system to do? What are the forms and reports

you will need?

Page 3: Normalization Chapter3

• When you will set with PC Store owner, you will ask him lot of questions about the sections in his store, the employees, the customers, how he design the invoice, the suppliers…

Page 4: Normalization Chapter3

• Its important now to specify the initial objects in this system, and to design the class models for it.

Page 5: Normalization Chapter3

• The database designer and developer suggested the following table

Employees( FirstName, LastName,Salary,DateofHire,Section)

Items(ItemName, SupplierName, ItemPrice)

Invoice(Invoice#, ItemName,InvoiceDate,Total)

Customers( FirstName, LastName,TelephoneNumber,eMail)

Page 6: Normalization Chapter3

Customer

+Fname+Lname+TelephoneNumber

Employee

+Fname+Lname+Salary+JobTitle+DateofHire+Section

Invoice

+Invoice#+ItemName+InvoiceDate+Total

Item

+ItemName+SupplierName+Price

1

*

* *

1

1

Page 7: Normalization Chapter3

• Now you (database designer and developer) will start to develop this system for PC store .

• You will need 3 golden rules to design a good tables for the database system

• These rules are called the Normalization rules

Page 8: Normalization Chapter3

First Normal Form

1. Gather all related data and put them into a separate table. Where each table has no repeating groups

2. Each cell must have an atomic value, for example ( for employee named John Smith, use FirstName =John, LastName=Smith)

3. Assign for each table a key

Page 9: Normalization Chapter3

• Lets go to the forms designs you collected from the owner of the PC Store

Invoice#

Invoice Date

Customer #

Phone

Item#

Name

Tax

Total Price

Name

eMail

Invoice Form

Page 10: Normalization Chapter3

• Now you will need a table to store these information for each invoice

• Invoice table ( Invoice#,InvoiceDate,Customer#,CustomerName,Phone,email,Item#,ItemName,Tax,TotalPrice)

Page 11: Normalization Chapter3

Invoice table ( Invoice#,InvoiceDate,Customer#,CustomerName,Phone,email,Item#,ItemName,Tax,Price)

Invoice# InvoiceDate Customer# CustomerName

Phone Email Item# ItemName Tax Price

1200 2-9-2007 1001 John Smith

77766555 [email protected]

A10 Webcam 0.05 30

1201 2-9-2007 1004 Sami gate

44466333 [email protected]

A10 Webcam 0.05 30

1202 4-9-2007 1002 Bob James

22334444 [email protected]

A20 Mouse 0.11 10

1203 18-9-2007 1002 Bob James

22334444 [email protected]

A30 Keyboard 0.11 18

1204 15-10-2007 1024 Luice bat 99988811 [email protected]

A20 Mouse 0.11 10

Page 12: Normalization Chapter3

• Initially, we need values in tables to be atomic , so we need

– Invoice table ( Invoice#,InvoiceDate,Customer#,CustLName,custFName,Phone,email,Item#, ItemName,Tax,Price)

• We need a primary key for this table

• Which are the Candidate keys?

• What you will choose for this table?

• What is a surrogate key?

• What is a composite key?

• What is a foreign key?

Page 13: Normalization Chapter3

• Table still contains repeating groups!– Invoice table

( Invoice#,InvoiceDate,Customer#,CustLName,custFName,Phone,email,Item#, ItemName,Tax,Price)

So we will split this table into two tablesInvoice ( Invoice#, Customer# ,Item# , InvoiceDate,ItemName,Tax,Price)Customer(Customer# ,CustLName, custFName, Phone,email)

Invoice# InvoiceDate Customer# CustomerName

Phone Email Item# ItemName Tax Price

1200 2-9-2007 1001 John Smith

77766555 [email protected]

A10 Webcam 0.05 30

1201 2-9-2007 1004 Sami gate 44466333 [email protected]

A10 Webcam 0.05 30

1202 4-9-2007 1002 Bob James

22334444 [email protected]

A20 Mouse 0.11 10

1203 18-9-2007 1002 Bob James

22334444 [email protected]

A30 Keyboard 0.11 18

1204 15-10-2007 1024 Luice bat 99988811 [email protected]

A20 Mouse 0.11 10

Page 14: Normalization Chapter3

Customer# FName LName Phone Email

1001 John Smith 77766555 [email protected]

1002 Bob James 44466333 [email protected]

1003 Sami gate 55666677 [email protected]

1004 tiddy Rool 22334444 [email protected]

1005 Luice bat 99988811 [email protected]

Invoice# InvoiceDate Customer# Item# ItemName Tax Price

1200 2-9-2007 1001 A10 Webcam 0.05 30

1201 2-9-2007 1004 A30 Keyboard 0.05 20

1202 4-9-2007 1002 A20 Mouse 0.11 10

1203 18-9-2007 1002 A30 Keyboard 0.05 20

1204 15-10-2007 1024 A20 Mouse 0.11 10

Page 15: Normalization Chapter3

• Can we use the phone number in Customer table as a primary key?

Can I use the

telephone number as

primary key?

Customer(Customer# ,CustLName, custFName, Phone,email)

Page 16: Normalization Chapter3

Second Normal FormInvoice ( Invoice#, Customer# ,Item# , InvoiceDate,ItemName,Tax,Price)

What is wrong with this notation????

Invoice# InvoiceDate Customer# Item# ItemName Tax Price

1200 2-9-2007 1001 A10 Webcam 0.05 30

1201 2-9-2007 1004 A30 Keyboard 0.05 20

1202 4-9-2007 1002 A20 Mouse 0.11 10

1203 18-9-2007 1002 A30 Keyboard 0.05 18

1204 15-10-2007 1024 A20 Mouse 0.11 10

Page 17: Normalization Chapter3

• We want each non key to be fully dependent on the entire key

• So applying 2NF, we have:• Invoice#,Customer#,Item#InvoiceDate• But; Item#ItemName• The problem that Item name depends on Item#

(part of the key)

• So we will write our relational tables in 2NF

Page 18: Normalization Chapter3

– Invoice ( Invoice#, Customer# ,Item# , InvoiceDate)– Customer(Customer# ,CustLName, custFName, Phone,email)– Item(Item#,ItemName,Tax,Price)

Now we have each non key is fully dependent on the entire key , and not only part of it

Page 19: Normalization Chapter3

Invoice# InvoiceDate Customer# Item#

1200 2-9-2007 1001 A10

1201 2-9-2007 1004 A30

1202 4-9-2007 1002 A20

1203 18-9-2007 1002 A30

1204 15-10-2007 1024 A20

Item# ItemName Tax Price

A10 Webcam 0.05 30

A20 Mouse 0.11 10

A30 Keyboard 0.05 18

A40 Monitor 0.05 130

Page 20: Normalization Chapter3

Third Normal Form

• Lets examine the Employee table

Employees( EMPID,FirstName, LastName,Salary,DateofHire,JobCat#,JobTitle,Section)

Emp# FirstName LastName JobCat# Salary DateOfHire JobTitle Section

100 John Smith MAN1 $25000 8-8-2004 Manager Sales

101 Scott Adams MAN1 $25000 9-2-2005 Manager Marketing

102 Bill Allan MA-SUP $15000 8-9-2005 Supervisor Sales

103 Kate Paul MAN2 $17000 19-9-2005 Assistant GM Marketing

104 Sam Adams MAN1 $25000 6-6-2006 Manager Accounting

105 Tom Gate MA-SUP $15000 7-9-2005 Supervisor Accounting

106 Harry Thomson MAN2 $17000 3-12-2004 Assistant GM Sales

Page 21: Normalization Chapter3

What do you think of this table?Some of the attributes depends on a key rather than the Primary Key.JOBCAT# JobTitleJOBCAT# SalaryEmployee is not in 3NF because JOBCAT# (non key in Employee

relation) Salary( other non key in same relation)…Hidden Dependency

TO be in Third Normal Form:• Table must be in 2NF• Every non key column must depend on nothing but the primary key

Page 22: Normalization Chapter3

Emp# FirstName LastName JobCat# DateOfHire Section

100 John Smith MAN1 8-8-2004 Sales

101 Scott Adams MAN1 9-2-2005 Marketing

102 Bill Allan MA-SUP 8-9-2005 Sales

103 Kate Paul MAN2 19-9-2005 Marketing

104 Sam Adams MAN1 6-6-2006 Accounting

105 Tom Gate MA-SUP 7-9-2005 Accounting

106 Harry Thomson MAN2 3-12-2004 Sales

JobCat# Salary JobTitle

MAN1 $25000 Manager

MAN2 $17000 Assistant GM

MA-SUP $15000 Supervisor

MA-SECRETARY $10000 Secretary

Page 23: Normalization Chapter3

Invoice# InvoiceDate Customer# Item#

1200 2-9-2007 1001 A10

1201 2-9-2007 1004 A30

1202 4-9-2007 1002 A20

1203 18-9-2007 1002 A30

1204 15-10-2007 1024 A20

Item# ItemName Tax Price

A10 Webcam 0.05 30

A20 Mouse 0.11 10

A30 Keyboard 0.05 20

A40 Monitor 0.05 130

Emp# FirstName LastName JobCat# DateOfHire Section

100 John Smith MAN1 8-8-2004 Sales

101 Scott Adams MAN1 9-2-2005 Marketing

102 Bill Allan MA-SUP 8-9-2005 Sales

103 Kate Paul MAN2 19-9-2005 Marketing

104 Sam Adams MAN1 6-6-2006 Accounting

105 Tom Gate MA-SUP 7-9-2005 Accounting

106 Harry Thomson MAN2 3-12-2004 Sales

JobCat# Salary JobTitle

MAN1 $25000 Manager

MAN2 $17000 Assistant GM

MA-SUP $15000 Supervisor

MA-SECRETARY $10000 Secretary

Page 24: Normalization Chapter3

The End