The Normal Forms2

download The Normal Forms2

of 25

Transcript of The Normal Forms2

  • 8/8/2019 The Normal Forms2

    1/25

    The Normal FormsThe Normal Forms

    3NF and BCNF3NF and BCNFBY BY

    Jasbir JassuJasbir Jassu

  • 8/8/2019 The Normal Forms2

    2/25

    P reviewP review

    NormalizationNormalizationSolution: Normal FormsSolution: Normal Forms

    Introducing 3NF and BCNFIntroducing 3NF and BCNF3NF3NFExamplesExamples

    BCNFBCNF

  • 8/8/2019 The Normal Forms2

    3/25

    NormalizationNormalizationNormalization is the process of efficientlyNormalization is the process of efficientlyorganizing data in a database with twoorganizing data in a database with twogoals in mindgoals in mind

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

    than one tablethan one table

    Second Goal:Second Goal: ensure data dependenciesensure data dependenciesmake sensemake sense for example, only storing related data in afor example, only storing related data in a

    tabletable

  • 8/8/2019 The Normal Forms2

    4/25

    Benefits of NormalizationBenefits of Normalization

    Less storage spaceLess storage spaceQuicker updatesQuicker updates

    Less data inconsistencyLess data inconsistencyClearer data relationshipsClearer data relationshipsEasier to add dataEasier to add data

    Flexible StructureFlexible Structure

  • 8/8/2019 The Normal Forms2

    5/25

    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, difficulties inanomalies: data inconsistency, difficulties inmaintenancemaintenance

    1NF, 2NF, 3NF, BCNF are some of the1NF, 2NF, 3NF, BCNF are some of theearly forms in the list that address thisearly forms in the list that address thisproblemproblem

  • 8/8/2019 The Normal Forms2

    6/25

    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 theMeet all the requirements of the 2NF2NF3)3) Remove columns that are not dependent Remove columns that are not dependent upon the primary key.upon the primary key.

  • 8/8/2019 The Normal Forms2

    7/25

    1) First normal form1) First normal form - -1NF1NF

    The following table is not in 1NFThe following table is not in 1NF

    DPT_NODPT_NO MG_NOMG_NO EMP_NOEMP_NO EMP_NMEMP_NMD 101D 101 1234512345 2000020000

    20001200012000220002

    Carl SaganCarl SaganMag JamesMag JamesLarry BirdLarry Bird

    D 102D 102 1345613456 30000300003000130001

    Jim CarterJim CarterP aul SimonP aul Simon

    1NF : if all attribute values are1NF : if all attribute values areatomic: no repeating group, noatomic: no repeating group, no

    composite attributes.composite attributes.

  • 8/8/2019 The Normal Forms2

    8/25

    Table in 1NFTable in 1NF

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

    DPT_NODPT_NO MG_NOMG_NO EMP_NOEMP_NO EMP_NMEMP_NM

    D 101D 101 1234512345 2000020000 Carl SaganCarl Sagan

    D 101D 101 1234512345 2000120001 Mag JamesMag James

    D 101D 101 1234512345 2000220002 Larry BirdLarry Bird

    D 102D 102 1345613456 3000030000 Jim CarterJim Carter

    D102

    D102 1345613456 3000130001

    Paul Simon

    Paul Simon

  • 8/8/2019 The Normal Forms2

    9/25

    2) Second Normal Form2) Second Normal Form

    Second normal form (2NF) further addresses theSecond normal form (2NF) further addresses theconcept of removing duplicative data:concept of removing duplicative data:

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

    (a) R is 1NF , and(a) R is 1NF , and (b) all non(b) all non- -prime attributes are fully dependent prime attributes are fully dependent on the candidate keys. Which is creatingon the candidate keys. Which is creatingrelationships between these new tables andrelationships between these new tables andtheir predecessors through the use of foreigntheir predecessors through the use of foreignkeys.keys.

    A prime attribute appears in a candidate key. A prime attribute appears in a candidate key.There is no partial dependency in 2NF.There is no partial dependency in 2NF.

    Example is nextExample is next

  • 8/8/2019 The Normal Forms2

    10/25

    No dependencies on nonNo dependencies on non- -key attributeskey 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, becausewe have more than one supplier for the same product.

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

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

  • 8/8/2019 The Normal Forms2

    11/25

    CONTINUEDCONTINUED

    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 uponthe ENTIRE PK (Description-Supplier)for its existence.

    Inventory

    Description Supplier Cost Supplier Address

  • 8/8/2019 The Normal Forms2

    12/25

  • 8/8/2019 The Normal Forms2

    13/25

    3 ) Remove columns that are not3 ) Remove columns that are not

    dependent upon the primary key.dependent upon the primary key.So for every nontrivial functional dependency XSo for every nontrivial functional dependency X -- --> A,> 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.

  • 8/8/2019 The Normal Forms2

    14/25

    B ooks

    Name Author's Name Author's Non-dePlume # of Pages

    B ooks

    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'sName, not the PK for its existence. It has to go.

    Author

    Name Non-de Plume

    Example of 3NFExample of 3NF

  • 8/8/2019 The Normal Forms2

    15/25

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

    S(SUPP# , P ART# , SNAME, QUANTITY) with the followingS(SUPP# , P ART# , SNAME, QUANTITY) with the followingassumptions:assumptions:

    (1) SU PP# is unique for every supplier.(1) SU PP# 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 bya 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) SU PP# (1) SU PP# ----> SNAME> SNAME(2) SNAME(2) SNAME ----> SU PP#> SU PP#(3) SU PP# P ART# (3) SU PP# P ART# ----> QUANTITY > QUANTITY (4) SNAME P ART# (4) SNAME P ART# ----> QUANTITY > QUANTITY

    The candidate keys are:The candidate keys are:(1) SU PP# P ART#(1) SU PP# P ART#(2) SNAME P ART#(2) SNAME P ART#

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

  • 8/8/2019 The Normal Forms2

    16/25

    The table in 3NFThe table in 3NF

    SUPP#SUPP# SNAMESNAME PART#PART# QTYQTY

    S1S1 Yues Yues

    P 1P 1

    100100

    S1S1 Yues Yues P 2P 2 200200

    S2S2 Yues Yues P 3P 3 250250

    S2S2 JonesJones P 1P 1 300300

  • 8/8/2019 The Normal Forms2

    17/25

    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 groups.No repeating 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, withit's own key, in this case the combination of invoice

    number and line number

  • 8/8/2019 The Normal Forms2

    18/25

  • 8/8/2019 The Normal Forms2

    19/25

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

  • 8/8/2019 The Normal Forms2

    20/25

    Third Normal Form:Third Normal Form:

    Each column must depend on *directly* on the primary key.Each column must depend on *directly* on the primary key.

  • 8/8/2019 The Normal Forms2

    21/25

  • 8/8/2019 The Normal Forms2

    22/25

    FD 1 clientNo, interview D ate interviewTime, staffNo, roomNo (Primary Key)

    FD 2 staffNo, interview D ate, interviewTime clientNo (Candidate key)

    FD 3 roomNo, interview D ate, interviewTime clientNo, staffNo (Candidate key)

    FD 4 staffNo, interview D ate 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 forstaffNo SG5 on the 13-May-02.

    ClientInterview

    ClientNoClientNo interviewDateinterviewDate interviewTimeinterviewTime staffNostaffNo roomNoroomNo

    CR76CR76 1313--MayMay--0202 10.3010.30 SG5SG5 G101G101

    CR76CR76 1313--MayMay--0202 12.0012.00 SG5SG5 G101G101CR74CR74 1313--MayMay--0202 12.0012.00 SG37SG37 G102G102

    CR56CR56 11--JulJul--0202 10.3010.30 SG5SG5 G102G102

  • 8/8/2019 The Normal Forms2

    23/25

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

    Interview (clientNo, interviewDate, interviewTime, staffNo)

    StaffRoom(staffNo, interviewDate, roomNo)

    ClientNoClientNo interviewDateinterviewDate interviewTimeinterviewTime staffNostaffNoCR76CR76 1313--MayMay--0202 10.3010.30 SG5SG5CR76CR76 1313--MayMay--0202 12.0012.00 SG5SG5CR74CR74 1313--MayMay--0202 12.0012.00 SG37SG37CR56CR56 11--JulJul--0202 10.3010.30 SG5SG5

    staffNostaffNo interviewDateinterviewDate roomNoroomNoSG5SG5 1313--MayMay--0202 G101G101SG37SG37 1313--MayMay--0202 G102G102SG5SG5 11--JulJul--0202 G102G102

    Interview

    StaffRoom

    BCNF Interview and StaffRoom relations

  • 8/8/2019 The Normal Forms2

    24/25

    Another BCNF Example Another BCNF Example

    Example taken from D r. Lee s 2004 lecture notes

  • 8/8/2019 The Normal Forms2

    25/25

    Sources:Sources:http://www.troubleshooters.com/littstip/ltnorm.htmlhttp://www.troubleshooters.com/littstip/ltnorm.htmlhttp://www.cs.jcu.edu.au/Subjects/cp1500/1998/Lecturehttp://www.cs.jcu.edu.au/Subjects/cp1500/1998/Lecture

    _Notes/normalisation/3nf.html _Notes/normalisation/3nf.html

    D r. Lee s Fall 2004 lecture notesD r. Lee s Fall 2004 lecture notes