the normal forms 3nf and bcnf by jasbir jassu. preview normalization normalization solution: normal...

25
The Normal Forms The Normal Forms 3NF and BCNF 3NF and BCNF BY BY Jasbir Jassu Jasbir Jassu

Upload: oscar-alexander

Post on 19-Jan-2016

253 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

The Normal FormsThe Normal Forms3NF and BCNF3NF and BCNF

BYBY

Jasbir JassuJasbir Jassu

Page 2: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

PreviewPreview

NormalizationNormalization Solution: Normal FormsSolution: Normal Forms Introducing 3NF and BCNFIntroducing 3NF and BCNF 3NF3NF ExamplesExamples BCNFBCNF

Page 3: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

NormalizationNormalization Normalization is the process of Normalization is the process of

efficiently organizing data in a efficiently organizing data in a database with two goals in minddatabase with two goals in mind

First goal: First goal: eliminate redundant dataeliminate redundant data– for example, storing the same data in for example, storing the same data in

more than one tablemore than one table Second Goal: Second Goal: ensure data ensure data

dependenciesdependencies make sense make sense – for example, only storing related data in for example, only storing related data in

a table a table

Page 4: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Benefits of NormalizationBenefits of Normalization

Less storage spaceLess storage space Quicker updatesQuicker updates Less data inconsistencyLess data inconsistency Clearer data relationshipsClearer data relationships Easier to add dataEasier to add data Flexible Structure Flexible Structure

Page 5: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

The Solution: Normal FormsThe Solution: Normal Forms

Bad database designs results in: Bad database designs results in: – redundancy: inefficient storage.redundancy: inefficient storage.– anomalies: data inconsistency, anomalies: data inconsistency,

difficulties in maintenancedifficulties in maintenance 1NF, 2NF, 3NF, BCNF are some of the 1NF, 2NF, 3NF, BCNF are some of the

early forms in the list that address early forms in the list that address this problemthis problem

Page 6: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Third Normal Form (3NF)Third Normal Form (3NF)

1)1) Meet all the requirements of the 1NFMeet all the requirements of the 1NF

2)2) Meet all the requirements of the Meet all the requirements of the 2NF2NF3)3) Remove columns that are not Remove columns that are not

dependent upon the primary key. dependent upon the primary key.

Page 7: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

1) First normal form -1NF1) First normal form -1NF

The following table is not in 1NFThe following table is not in 1NFDPT_NODPT_NO MG_NOMG_NO EMP_NOEMP_NO EMP_NMEMP_NM

D101D101 1234512345 2000020000

2000120001

2000220002

Carl SaganCarl SaganMag JamesMag JamesLarry BirdLarry Bird

D102D102 1345613456 30000300003000130001

Jim CarterJim Carter

Paul SimonPaul Simon

•1NF : if all attribute values 1NF : if all attribute values are atomic: no repeating are atomic: no repeating group, no composite attributes.group, no composite attributes.

Page 8: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Table in 1NFTable in 1NF

all attribute values are atomic because there are no all attribute values are atomic because there are no repeating group and no composite attributes.repeating group and no composite attributes.

DPT_NODPT_NO MG_NOMG_NO EMP_NOEMP_NO EMP_NMEMP_NM

D101D101 1234512345 2000020000 Carl SaganCarl Sagan

D101D101 1234512345 2000120001 Mag JamesMag James

D101D101 1234512345 2000220002 Larry BirdLarry Bird

D102D102 1345613456 3000030000 Jim CarterJim Carter

D102D102 13456134563000130001

Paul SimonPaul Simon

Page 9: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

2) Second Normal Form2) Second Normal Form

– Second normal form (2NF) further addresses Second normal form (2NF) further addresses the concept of removing duplicative data: the concept of removing duplicative data:

A relation R is in 2NF ifA relation R is in 2NF if

– (a) R is 1NF , and (a) R is 1NF , and – (b) all non-prime attributes are fully (b) all non-prime attributes are fully

dependent on the candidate keys. Which dependent on the candidate keys. Which is creating relationships between these is creating relationships between these new tables and their predecessors new tables and their predecessors through the use of foreign keys. through the use of foreign keys.

A prime attribute appears in a candidate A prime attribute appears in a candidate key.key.

There is no partial dependency in 2NF. There is no partial dependency in 2NF.

Example is next…Example is next…

Page 10: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

No dependencies on non-key attributesNo dependencies on non-key attributes

Inventory

Description Supplier Cost Supplier Address

Inventory

Description Supplier Cost

There are two non-key fields.  So, here are the questions:

•If I know just Description, can I find out Cost?  No, because we have more than one supplier for the same product.

•If I know just Supplier, and I find out Cost?  No, because I need to know what the Item is as well.

Therefore, Cost is fully, functionally dependent upon the ENTIRE PK (Description-Supplier) for its existence.

Page 11: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

CONTINUED…CONTINUED…

Supplier

Name Supplier Address

•If I know just Description, can I find out Supplier Address?  No, because we have more than one supplier for the same product.

•If I know just Supplier, and I find out Supplier Address?  Yes.  The Address does not depend upon the description of the item.

Therefore, Supplier Address is NOT functionally dependent upon the ENTIRE PK (Description-Supplier) for its existence.

Inventory

Description Supplier Cost Supplier Address

Page 12: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

So putting things togetherSo putting things together

Inventory

Description Supplier Cost Supplier Address

Inventory

Description Supplier Cost

Supplier

Name Supplier Address

The above relation is now in 2NF since the relation has The above relation is now in 2NF since the relation has

no non-key attributes.no non-key attributes.

Page 13: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

3) Remove columns that are not 3) Remove columns that are not dependent upon the primary key.dependent upon the primary key.

So for every nontrivial functional dependency X --> A, So for every nontrivial functional dependency X --> A, (1) X is a superkey, or(1) X is a superkey, or(2) A is a prime (key) attribute.(2) A is a prime (key) attribute.

Page 14: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Books

Name Author's NameAuthor's Non-de Plume

# of Pages

Books

Name Author's Name # of Pages

•If I know # of Pages, can I find out Author's Name?  No.  Can I find out Author's Non-de Plume?  No.•If I know Author's Name, can I find out # of Pages?  No.  Can I find out Author's Non-de Plume?  YES.

Therefore, Author's Non-de Plume is functionally dependent upon Author's Name, not the PK for its existence.  It has to go.

Author

Name Non-de Plume

Example of 3NFExample of 3NF

Page 15: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Another example: Suppose we have relation SAnother example: Suppose we have relation S

S(SUPP#, PART#, SNAME, QUANTITY) with the following S(SUPP#, PART#, SNAME, QUANTITY) with the following assumptions:assumptions:

(1) SUPP# is unique for every supplier.(1) SUPP# is unique for every supplier.(2) SNAME is unique for every supplier.(2) SNAME is unique for every supplier.(3) QUANTITY is the accumulated quantities of a part supplied by (3) QUANTITY is the accumulated quantities of a part supplied by a supplier.a supplier.(4) A supplier can supply more than one part.(4) A supplier can supply more than one part.(5) A part can be supplied by more than one supplier.(5) A part can be supplied by more than one supplier.

We can find the following nontrivial functional dependencies:We can find the following nontrivial functional dependencies:

(1) SUPP# --> SNAME(1) SUPP# --> SNAME(2) SNAME --> SUPP#(2) SNAME --> SUPP#(3) SUPP# PART# --> QUANTITY(3) SUPP# PART# --> QUANTITY(4) SNAME PART# --> QUANTITY(4) SNAME PART# --> QUANTITY

The candidate keys are:The candidate keys are: (1) SUPP# PART#(1) SUPP# PART#

(2) SNAME PART#(2) SNAME PART#

The relation is in 3NF.The relation is in 3NF.

Page 16: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

The table in 3NFThe table in 3NF

SUPP#SUPP# SNAMESNAME PART#PART# QTYQTY

S1S1YuesYues P1P1

100100

S1S1 YuesYues P2P2 200200

S2S2 YuesYues P3P3 250250

S2S2 JonesJones P1P1 300300

Page 17: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Example with first three formsExample with first three formsSuppose we have this Invoice TableSuppose we have this Invoice Table

First Normal Form:First Normal Form: No repeating No repeating groups.groups.

•The above table violates 1NF because it has columns for the first, second, and third line item.

•Solution: you make a separate line item table, with it's own key, in this case the combination of invoice number and line number

Page 18: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Table now in 1NFTable now in 1NF

Page 19: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Second Normal Form:Second Normal Form: Each column must depend on the *entire* primary key.Each column must depend on the *entire* primary key.

Page 20: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Third Normal Form:Third Normal Form: Each column must depend on *directly* on the primary Each column must depend on *directly* on the primary

key.key.

Page 21: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Boyce-Codd Normal Form Boyce-Codd Normal Form (BCNF)(BCNF)

Boyce-Codd normal form (BCNF)A relation is in BCNF, if and only if, every determinant is a candidate key.

The difference between 3NF and BCNF is that for a functionaldependency A B, 3NF allows this dependency in a relation if B is a primary-key attribute and A is not a candidate key,

whereas BCNF insists that for this dependency to remain in arelation, A must be a candidate key.

Page 22: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

FD1 clientNo, interviewDate interviewTime, staffNo, roomNo (Primary Key)

FD2 staffNo, interviewDate, interviewTime clientNo (Candidate key)

FD3 roomNo, interviewDate, interviewTime clientNo, staffNo (Candidate key)

FD4 staffNo, interviewDate roomNo (not a candidate key)

As a consequece the ClientInterview relation may suffer from update anmalies.

For example, two tuples have to be updated if the roomNo need be changed for staffNo SG5 on the 13-May-02.

ClientInterviewClientNClientNoo

interviewDatinterviewDatee

interviewTiminterviewTimee

staffNostaffNo roomNoroomNo

CR76CR76 13-May-0213-May-02 10.3010.30 SG5SG5 G101G101

CR76CR76 13-May-0213-May-02 12.0012.00 SG5SG5 G101G101

CR74CR74 13-May-0213-May-02 12.0012.00 SG37SG37 G102G102

CR56CR56 1-Jul-021-Jul-02 10.3010.30 SG5SG5 G102G102

Page 23: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Example of BCNF(2)Example of BCNF(2)To transform the ClientInterview relation to BCNF, we must remove the violating functional dependency by creating two new relations called Interview and StaffRoom as shown below,

Interview (clientNo, interviewDate, interviewTime, staffNo)StaffRoom(staffNo, interviewDate, roomNo)

ClientNoClientNo interviewDateinterviewDate interviewTimeinterviewTime staffNostaffNoCR76CR76 13-May-0213-May-02 10.3010.30 SG5SG5

CR76CR76 13-May-0213-May-02 12.0012.00 SG5SG5

CR74CR74 13-May-0213-May-02 12.0012.00 SG37SG37

CR56CR56 1-Jul-021-Jul-02 10.3010.30 SG5SG5

staffNostaffNo interviewDateinterviewDate roomNoroomNoSG5SG5 13-May-0213-May-02 G101G101

SG37SG37 13-May-0213-May-02 G102G102

SG5SG5 1-Jul-021-Jul-02 G102G102

Interview

StaffRoom

BCNF Interview and StaffRoom relations

Page 24: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Another BCNF ExampleAnother BCNF Example

Example taken from Dr. Lee’s 2004 lecture notes

Page 25: The Normal Forms 3NF and BCNF BY Jasbir Jassu. Preview Normalization Normalization Solution: Normal Forms Solution: Normal Forms Introducing 3NF and BCNF

Sources:Sources:

http://www.troubleshooters.com/littstip/ltnorm.hthttp://www.troubleshooters.com/littstip/ltnorm.htmlml

http://www.cs.jcu.edu.au/Subjects/cp1500/1998/Lhttp://www.cs.jcu.edu.au/Subjects/cp1500/1998/Lecture_Notes/normalisation/3nf.htmlecture_Notes/normalisation/3nf.html

Dr. Lee’s Fall 2004 lecture notesDr. Lee’s Fall 2004 lecture notes