Top Banner

of 8

Lecture 8 Normalization

Aug 07, 2018

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
  • 8/20/2019 Lecture 8 Normalization

    1/17

  • 8/20/2019 Lecture 8 Normalization

    2/17

    Lecture 7

    Systems Analysis & Design 2

    3

    Basic Conversion Rules For 1:1 cardinality: all attributes are

    merged into a single table.

    For 1:N cardinality: post the “identifier”from the “one” side as an attribute tothe “many” side.

    For N:M cardinality: create a new table

    and post the primary keys from eachentity as attributes in the new table.

    4

    Null Values

    Should be avoided.

    Make it difficult to construct & interpret

    relational data base queries.

  • 8/20/2019 Lecture 8 Normalization

    3/17

    Lecture 7

    Systems Analysis & Design 3

    5

    Optional-Max Conversion Rules

    There are 9 rules

    On completion of these 9 rules a relationaldatabase would be created.

    6

    Optional-Max Conversion Rules:

    Rule 1

    Construct an ER

    diagram and label theupper and lower boundsfor relationship degree.

     All multi-valuedattributes should berepresented as entities.

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_NameL_name

    Home Address

    H_StreetH_CityH_State

    Locker

    LID [PK]Building

    Teach

    Enroll

    Stud_Maj

    Has   Assigned

    Major

    Maj_ID [PK]Description

  • 8/20/2019 Lecture 8 Normalization

    4/17

    Lecture 7

    Systems Analysis & Design 4

    7

    Optional-Max Conversion Rules:

    Rule 2

    In every instance where the lower

    bound for relationship degree is 0

    and the upper bound is 1,

    temporarily label the upper bound

    as N.

    If the upper bound is greater than 1

    (for example, 3, or 10 or N), leave it

    as is.

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_Name

    L_name

    Home AddressH_StreetH_CityH_State

    LockerLID [PK]Building

    Teach

    Enroll

    Stud_Maj

    Has   Assigned

    MajorMaj_ID [PK]Description

    N

    N

    N

    8

    Optional-Max Conversion Rules:

    Rule 3

    Faculty

    Course

    Students

    Home_Address

    Major 

    Locker 

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_NameL_name

    Home AddressH_StreetH_CityH_State

    LockerLID [PK]Building

    Teach

    Enroll

    Stud_Maj

    Has   Assigned

    MajorMaj_ID [PK]

    Description

    N

    N

    N

    Use the entity namefor each entity as thetable name

  • 8/20/2019 Lecture 8 Normalization

    5/17

    Lecture 7

    Systems Analysis & Design 5

    9

    Optional-Max Conversion Rules:

    Rule 4

    If an identifier or primary key has

    not been selected then select an

    identifier for each table.Faculty (FID)

    Course (CID)

    Students (SID)

    Home_Address (H_Street, H_City, H_State)

    Major (Maj_ID)

    Locker (LID)

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_NameL_name

    Home AddressH_StreetH_CityH_State

    LockerLID [PK]Building

    Teach

    Enroll

    Stud_Maj

    Has   Assigned

    MajorMaj_ID [PK]Description

    N

    N

    N

    10

    Optional-Max Conversion Rules:

    Rule 5

    Take all the attributes that

    describe an entity & post themas column names in the

    relational tables.

    Faculty (FID, F_Name, L_Name)

    Course (CID, C_Name, Credits)

    Students (SID, F_Name, L_Name)

    Home_Address (H_Street, H_City,

    H_State)

    Major (Maj_ID, Description)

    Locker (LID, Building)

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_NameL_name

    Home AddressH_StreetH_CityH_State

    LockerLID [PK]Building

    Teach

    Enroll

    Stud_Maj

    Has   Assigned

    MajorMaj_ID [PK]

    Description

    N

    N

    N

  • 8/20/2019 Lecture 8 Normalization

    6/17

  • 8/20/2019 Lecture 8 Normalization

    7/17

    Lecture 7

    Systems Analysis & Design 7

    13

    Optional-Max Conversion Rules:

    Rule 8

    For N:M relationships, create a newtable using the relationship name

    as the name for the table.

    Faculty (FID, F_Name, L_Name)

    Teach (FID, CID)

    Course (CID, C_Name, Credits)

    Enroll (CID, SID,)

    Students (SID, Maj_ID F_Name,L_Name, H_Street, H_City, H_State)

    Major (Maj_ID, Description)

     Assigned (SID, LID)

    Locker (LID, Building)

    concatenated

    key

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    Enroll

    StudentsSID [PK]

    F_NameL_name

    Home AddressH_StreetH_CityH_State

    MajorMaj_ID [PK]Description

    LockerLID [PK]Building

    Teach

    C_Enroll

    S_Enroll

    Stud_Maj

    Has   Assigned

    N

    N

    N

    intersection

    table

    14

    Optional-Max Conversion Rules: Rule 9Take any additional attributes and post them as column names in the appropriate

    relational table. If there are any composite attributes in the relational table

    definition, decompose them into simple attributes.

    Faculty (FID, F_Name, L_Name)

    Teach (FID, CID)

    Course (CID, C_Name, Credits)

    Enroll (CID, SID, Grade)

    Students (SID, Maj_ID F_Name,L_Name, H_Street, H_City, H_State)

    Major (Maj _ID, Description)

     Assigned (SID, LID, Building)

    Locker (LID, Building)

    FacultyFID [PK]F_NameL_name

    CourseCID [PK]C_NameCredits

    StudentsSID [PK]F_NameL_name

    Home AddressH_StreetH_CityH_State

    MajorMaj_ID [PK]Description

    LockerLID [PK]Building

    Teach

    Stud_Maj

    Has   Assigned

    N

    N

    N

    Enroll

    C_Enroll

    S_Enroll

  • 8/20/2019 Lecture 8 Normalization

    8/17

    Lecture 7

    Systems Analysis & Design 8

    15

    Optional-Max Conversion Rules

    SummaryOptional-Max Conversion Rules

    Rule 1 Construct ERD

    Rule 2 Change 0:1 to 0:N

    Rule 3 Use entity name as Table name

    Rule 4 Select and write an identifier for each table

    Rule 5 Write down attributes as column names

    Rule 6 Merge 1:1 relationships

    Rule 7 For 1:N relationships place key from “one”side to the “many” side

    Rule 8 For N:M create a new table usingrelationship name

    Rule 9 If additional attribute exist, place inappropriate table

    16

    Normalization

    Normalization is a technique for producing a set of

    relations with desirable properties, given the datarequirements of an enterprise.

    The process of normalization is a formal method that

    identifies relations based on their primary or candidate

    keys and the functional dependencies among their

    attributes.

  • 8/20/2019 Lecture 8 Normalization

    9/17

    Lecture 7

    Systems Analysis & Design 9

    17

    Normal forms The 4 most commonly used normal

    forms are:

    First normal form (1NF)

    Second normal form (2NF)

    Third normal form (3NF)

    Boyce-Codd normal form (BCNF)

    18

    Normal forms

    Unnormalized relation

    Remove repeating

    groups

    Remove partial

    dependencies

    Remove transitive

    dependencies

    1NF

    2NF

    3NF

  • 8/20/2019 Lecture 8 Normalization

    10/17

    Lecture 7

    Systems Analysis & Design 10

    19

    WHNo WHLoc ProductNo ProductDesc Price PCode CDesc Qty

    10 Ilford 1001 Hammer 10.99 H Hardware 06

    1002 Screwdriver 03.99 H Hardware 08

    1010 Wood Drill 30.49 ET Electrical 10

    30 Exeter 1001 Hammer 10.99 H Hardware 01

    4001 Stepladder 49.99 H Hardware 02

    Unnormalized form

    20

    First normal form ( 1NF )

    First normal form requires that repeating groups must beremoved

    (WHNo, WHLoc, (ProductNo, PDesc, Price, PCode,CDesc, Qty)).

    WHNo WHLoc ProductNo ProductDesc Price PCode CDesc Qty

    10 Ilford 1001 Hammer 10.99 H Hardware 06

    1002 Screwdriver 03.99 H Hardware 08

    1010 Wood Drill 30.49 ET Electrical 10

    30 Exeter 1001 Hammer 10.99 H Hardware 01

    4001 Stepladder 49.99 H Hardware 02

    Repeating Group

  • 8/20/2019 Lecture 8 Normalization

    11/17

    Lecture 7

    Systems Analysis & Design 11

    21

    First normal form ( 1NF ) WAREHOUSE(WHNo, WHLoc).

    STOCK(WHNo, ProductNo, ProductDesc, Price, PCode,CDesc, Qty).

    The duplication of WHNo in both serves to establish therelationship, or link, between the two entity types.

    Neither WHNo nor ProductNo alone would be sufficient touniquely identify occurrences of STOCK.

    It would be possible to reconstitute the original i.e. identifyall the occurrences of STOCK that are related to any givenoccurrence of WAREHOUSE

    22

    First normal form ( 1NF )

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo ProductDesc Price PCode CDesc Qty

    10 1001 Hammer 10.99 H Hardware 06

    10 1002 Screwdriver 03.99 H Hardware 08

    10 1010 Wood Drill 30.49 ET Electrical 10

    30 1001 Hammer 10.99 H Hardware 01

    30 4001 Stepladder 49.99 H Hardware 02

    Stock

  • 8/20/2019 Lecture 8 Normalization

    12/17

    Lecture 7

    Systems Analysis & Design 12

    23

    First normal form ( 1NF )

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo ProductDesc Price PCode CDesc Qty

    10 1001 Hammer 10.99 H Hardware 06

    10 1002 Screwdriver 03.99 H Hardware 08

    10 1010 Wood Drill 30.49 ET Electrical 10

    30 1001 Hammer 10.99 H Hardware 01

    30 4001 Stepladder 49.99 H Hardware 02

    Stock

    Insertion

    To insert a new product intothe entity STOCK,Need to know what warehouseit is supplied from.

    24

    First normal form ( 1NF )

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo ProductDesc Price PCode CDesc Qty

    10 1001 Hammer 10.99 H Hardware 06

    10 1002 Screwdriver 03.99 H Hardware 08

    10 1010 Wood Drill 30.49 ET Electrical 10

    30 1001 Hammer 10.99 H Hardware 01

    30 4001 Stepladder 49.99 H Hardware 02

    Stock

    Deletion

    If warehouse 30 is deleted,all the details of Product 4001will also be deleted

  • 8/20/2019 Lecture 8 Normalization

    13/17

    Lecture 7

    Systems Analysis & Design 13

    25

    First normal form ( 1NF )

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo ProductDesc Price PCode CDesc Qty

    10 1001 Hammer 10.99 H Hardware 06

    10 1002 Screwdriver 03.99 H Hardware 08

    10 1010 Wood Drill 30.49 ET Electrical 10

    30 1001 Hammer 10.99 H Hardware 01

    30 4001 Stepladder 49.99 H Hardware 02

    Stock

    Editing

    same price will be charged fora product regardless of whereit is supplied from.If price of hammers atWHNo10 = 11.99, the price atWHNo30 will still be = 10.99.This will make the datainconsistent.

    26

    Clearly, WAREHOUSE is already in 2NF

    (Why? Because all attributes fully depend on theentire key, WHNo; none of the attributes candepend on part of a WHNo).

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

  • 8/20/2019 Lecture 8 Normalization

    14/17

    Lecture 7

    Systems Analysis & Design 14

    27

    However, there are partial dependencies in STOCK.

    ProductNo ProductDesc, Price, PCode,

    CDesc.Therefore, these attributes depend on only part ofthe key, i.e. they depend on ProductNo and NOTWHNo ProductNo together.

    WHNo ProductNo   ProductDesc Price PCode CDesc   Qty

    10 1001   Hammer 10.99 H Hardware   06

    10 1002   Screwdriver 03.99 H Hardware   08

    10 1010   Wood Drill 30.49 ET Electrical   10

    30 1001   Hammer 10.99 H Hardware   01

    30 4001   Stepladder 49.99 H Hardware   02

    Stock

    28

    Second normal form ( 2NF )"all attributes, which are not part of the primary key, must be fullyfunctionally dependent on the whole of the key".

    ProductNo ProductDesc Price PCode CDesc

    1001 Hammer 10.99 H Hardware

    1002 Screwdriver 03.99 H Hardware

    1010 Wood Drill 30.49 ET Electrical

    4001 Stepladder 49.99 H Hardware

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo Qty

    10 1001 06

    10 1002 08

    10 1010 10

    30 1001 01

    30 4001 02

    Stock Product

  • 8/20/2019 Lecture 8 Normalization

    15/17

    Lecture 7

    Systems Analysis & Design 15

    29

    Second normal form ( 2NF )

    ProductNo ProductDesc Price PCode CDesc

    1001 Hammer 10.99 H Hardware

    1002 Screwdriver 03.99 H Hardware

    1010 Wood Drill 30.49 ET Electrical

    4001 Stepladder 49.99 H Hardware

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo Qty

    10 1001 06

    10 1002 08

    10 1010 10

    30 1001 01

    30 4001 02

    Stock Product

    Insertion

    In PRODUCT,To insert a potentially newproduct code you will need toinsert details of a product,because the key ProductNowould be NULL or empty.This is unacceptable.

    30

    Second normal form ( 2NF )

    ProductNo ProductDesc Price PCode CDesc

    1001 Hammer 10.99 H Hardware

    1002 Screwdriver 03.99 H Hardware

    1010 Wood Drill 30.49 ET Electrical

    4001 Stepladder 49.99 H Hardware

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo Qty

    10 1001 06

    10 1002 08

    10 1010 10

    30 1001 01

    30 4001 02

    Stock Product

    Deletion

    In PRODUCT, if you want tostop selling wood drills,

    you need to delete the rowfor wood drill i.e. whereProductNo = 1010.

    When you do this, the factthat the product code 'ET'and its description

     ‘Electrical’ exist, is now lost.

  • 8/20/2019 Lecture 8 Normalization

    16/17

    Lecture 7

    Systems Analysis & Design 16

    31

    Second normal form ( 2NF )

    ProductNo ProductDesc Price PCode CDesc

    1001 Hammer 10.99 H Hardware

    1002 Screwdriver 03.99 H Hardware

    1010 Wood Drill 30.49 ET Electrical

    4001 Stepladder 49.99 H Hardware

    WHNo WHLoc

    10 Ilford

    30 Exeter

    Warehouse

    WHNo ProductNo Qty

    10 1001 06

    10 1002 08

    10 1010 10

    30 1001 01

    30 4001 02

    Stock Product

    Editing

    In PRODUCT, change thecode description forstepladders from ‘Hardware’to ‘Paint’.

    The product code ‘H’ willhave a description of ‘Paint’for this row, and adescription of ‘Hardware’everywhere else. This leadsto inconsistency in the data.

    32

    ProductNo PCode CDesc

    CDesc is not dependent on ProductNo. Its havingtransitive dependecies with ProductNo

    Normalisation requires that more new entitytypes be formed to remove the transitivedependencies, but that their key (PCode in thiscase) must also be retained in the original entitytype

     Transitive dependencies

  • 8/20/2019 Lecture 8 Normalization

    17/17

    Lecture 7

    33

     Third normal form ( 3NF )"Entity types are in 3NF when all attributes which are not part of the key are

    functionally dependent on the key, the whole key and nothing but thekey".

    PCode CDesc

    H Hardware

    ET Electrical

    WHNo WHLoc

    10 Ilford

    30 Exeter

    WarehouseWHNo ProductNo Qty

    10 1001 06

    10 1002 08

    10 1010 10

    30 1001 01

    30 4001 02

    Stock

    Product

    Product_Code

    ProductNo PDesc Price PCode

    1001 Hammer 10.99 H

    1002 Screwdriver 03.99 H

    1010 Wood Drill 30.49 ET

    4001 Stepladder 49.99 H

    34

    Summary NormalisationUnnormalized relation

    1NF

    2NF

    3NF

    Remove repeatinggroups

    Remove partialdependencies

    Remove transitivedependencies