Top Banner
Chapter 5: Normalizing the DB
13

Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

Dec 31, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

Chapter 5: Normalizing the DB

Page 2: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• What to do with a bad database structure?

• How do we determine the right structure?

• How do we determine primary keys?

• Normalization Rules.

Page 3: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• Functional Dependence– When the values of one column depend on

the values of another column– Column A determines Col B, then B is

dependent on A.

– If all the columns are dependent on A, then A is most likely a primary key.

Page 4: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

RepNum LastName FirstName Street City State Zip Commission PayClass Rate

20 Kaiser Valerie 624 Randall

Grove FL 33321 20,542.50 1 .05

35 Hull Richard 525 Jackson

Sheldon FL 33553 39,216.00 2 .07

65 Perez Juan 1626 Taylor

Fillmore FL 33336 23,487.00 1 .05

We’ve added a new field – Pay Class, which determines the rate.Rate is dependent on Pay Class.

What other dependencies are there?

Page 5: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

OrderNum OrderDate CustomerNum

21608 10/20/2010 148

21610 10/20/2010 356

21613 10/21/2010 408

21614 10/21/2010 282

Orders Table

The order number determines the date, and the customer number. They are dependent on the order number.

The order number isn’t dependent on anything.

Page 6: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

OrderNum PartNum NumOrdered QuotedPrice

21608 AT94 11 21.95

21610 DR93 1 495.00

21610 DW11 1 399.00

21613 DR93 4 329.00

What does the NumOrdered column depend on?

What field(s) determine the quoted price?

Page 7: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• 1st Normal Form: – Tables without repeating groups

Break the repeating groups into individual rows to have 1st Normal form.

Page 8: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

Second Normal Form

OrderNum OrderDate PartNum Description NumOrdered QuotedPrice

21608 10/20/2008 AT94 Iron 11 21.95

21610 10/20/2008 DR93 Gase Range 1 495.00

21610 10/20/2008 DW11 Washer 1 399.00

21613 10/20/2008 Kl62 Dryer 4 329.00

21619 10/23/2008 DR93 Gas Range 1 495.00

What is wrong with this table structure? It is in 1NF, but not in 2NF.

Page 9: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• The redundant data leads to serious problems.– Updating– Possibility of inconsistent data– Problem of making additions– Problem of deleting

What is the primary key for that table?

What does description depend on?

Page 10: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• Third Normal Form

CustomerNum CustomerName Balance CreditLimit RepNum LastName FirstName

148 Al’s Appliance & Sport

$6,550 $7,500 20 Kaiser Valerie

282 Brookings Direct

$435 $10,000 35 Hull Richard

356 Ferguson’s $5785.00 $7,500 65 Perez Juan

462 Bargain’s Galore

$3,412 $10,000 65 Perez Juan

CustomerNum determines everything. It’s the primary key.But there is still a problem…

To get this table into 3NF, we need to break out the redundant data.

Page 11: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

Fourth Normal Form

FacultyNum StudentNum CommitteeCode

123 1280524139

ADVHSGPER

444 57384 HSG

456 241393627337573

CUR

If we redesigned this structure to this:

Page 12: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

FacultyNum StudentNum CommitteeCode

123 12805 ADV

123 12805 HSG

123 12805 PER

123 24139 ADV

123 24139 HSG

123 24139 PER

444 57384 HSG

456 24139 CUR

456 36273 CUR

This table is in 3NF, but what’s the problem here?

Page 13: Chapter 5: Normalizing the DB. What to do with a bad database structure? How do we determine the right structure? How do we determine primary keys? Normalization.

• The table tries to group together 3 entities that really shouldn’t be grouped. They aren’t exactly related.

• What primary key column would determine the other column?

• It should be: FacStudent (FacultyNum, StudentNum)

FacCommittee (FacultyNum, CommitteeCode)