Normalisation 3NF

Embed Size (px)

Citation preview

  • 8/21/2019 Normalisation 3NF

    1/24

    Normalisation to 3NF

    Database Systems Lecture 11

    Natasha Alechina

  • 8/21/2019 Normalisation 3NF

    2/24

     

    In This Lecture

    • Normalisation to 3NF

    • Data redundancy

    • Functional dependencies• Normal forms

    • First, Second, and Third Normal Forms

    • For more information

    • onnolly and !e"" chapter 13

    • #llman and $idom ch%3%&%& '(nd edition),3%* '3rd edition)

  • 8/21/2019 Normalisation 3NF

    3/24

     

    +edundancy and Normalisation

    • +edundant data

    • an be determinedfrom other data in the

    database• Leads to arious

    problems

    • INS-+T anomalies

    • #.DAT- anomalies

    • D-L-T- anomalies

    • Normalisation

    • Aims to reduce dataredundancy

    • +edundancy ise/pressed in terms ofdependencies

    • Normal forms aredefined that do not

    hae certain types ofdependency

  • 8/21/2019 Normalisation 3NF

    4/24

     

    First Normal Form

    • In most definitions ofthe relational model

    • All data alues should

    be atomic

    • This means that tableentries should besin"le alues, not setsor composite ob0ects

    • A relation is said tobe in first normalform '1NF) if all data

    alues are atomic

  • 8/21/2019 Normalisation 3NF

    5/24

     

    Normalisation to 1NF

    Unnormalised

    Module Dept Lecturer Texts

      M1 D1 L1 T1, T2

      M2 D1 L1 T1, T3

      M3 D1 L2 T4  M4 D2 L3 T1, T5

      M5 D2 L4 T6

    1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1

      M2 D1 L1 T3  M3 D1 L2 T4

      M4 D2 L3 T1

      M4 D2 L3 T5

      M5 D2 L4 T6

    To convert to a 1NF relation, split up an

    non!atomic values

  • 8/21/2019 Normalisation 3NF

    6/24

     

    .roblems in 1NF

    • INS-+T anomalies

    • ant add a module2ith no te/ts

    • #.DAT- anomalies

    • To chan"e lecturer for1, 2e hae tochan"e t2o ro2s

    • D-L-T- anomalies• If 2e remoe 3, 2e

    remoe L( as 2ell

    1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1

      M2 D1 L1 T3

      M3 D1 L2 T4

      M4 D2 L3 T1  M4 D2 L3 T5

      M5 D2 L4 T6

  • 8/21/2019 Normalisation 3NF

    7/24 

    Functional Dependencies

    • +edundancy is oftencaused by a functionaldependency

    • A functional dependency'FD) is a lin4 bet2eent2o sets of attributes ina relation

    • $e can normalise a

    relation by remoin"undesirable FDs

    • A set of attributes, A,functionally determines another set, !, or5 there

    e/ists a functionaldependency bet2een Aand ! 'A → !), if2heneer t2o ro2s ofthe relation hae thesame alues for all theattributes in A, then theyalso hae the samealues for all theattributes in !%

  • 8/21/2019 Normalisation 3NF

    8/24 

    -/ample

    • 6ID, modode7 → 6First, Last, modName7

    • 6modode7 → 6modName7

    • 6ID7 → 6First, Last7

    ID modode modNameFirst Last

    111 8*1.+8 .ro"rammin"9oe !lo""s

    ((( 8*1D!S DatabasesAnne Smith

  • 8/21/2019 Normalisation 3NF

    9/24 

    FDs and Normalisation

    • $e define a set ofnormal forms

    • -ach normal form has

    fe2er FDs than thelast

    • Since FDs representredundancy, eachnormal form has less

    redundancy than thelast

    • Not all FDs cause aproblem

    • $e identify arious

    sorts of FD that do

    • -ach normal formremoes a type of FDthat is a problem

    • $e 2ill also need a2ay to remoe FDs

  • 8/21/2019 Normalisation 3NF

    10/24 

    .roperties of FDs

    • In any relation• The primary 4ey FDs

    any set of attributes

    in that relationK  →  X 

    • K  is the primary 4ey, X  is a set ofattributes

    • Same for candidate

    4eys• Any set of attributes

    is FD on itself 

     X  →  X 

    • +ules for FDs• +efle/iity5 If B is a

    subset of A then

     A → B• Au"mentation5 If A → B then

     A # C → B # C 

    • Transitiity5

    If A → B and B → C  then A → C 

  • 8/21/2019 Normalisation 3NF

    11/24 

    FD -/ample

    • The primary 4ey is6odule, Te/t7 so

    6odule, Te/t7 → 

    6Dept, Lecturer7

    • Triial FDs, e"5

    6Te/t, Dept7 → 6Te/t7

    6odule7 → 6odule7

    6Dept, Lecturer7 → 6 7

    1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1

      M2 D1 L1 T3

      M3 D1 L2 T4

      M4 D2 L3 T1  M4 D2 L3 T5

      M5 D2 L4 T6

  • 8/21/2019 Normalisation 3NF

    12/24 

    FD -/ample

    • :ther FDs are

    • 6odule7 → 6Lecturer7

    • 6odule7 → 6Dept7

    • 6Lecturer7 → 6Dept7

    • These are non;triialand determinants 'lefthand side of thedependency) are not4eys%

    1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1

      M2 D1 L1 T3

      M3 D1 L2 T4

      M4 D2 L3 T1  M4 D2 L3 T5

      M5 D2 L4 T6

  • 8/21/2019 Normalisation 3NF

    13/24 

    .artial FDs and (NF

    • .artial FDs5

    • A FD, A → B is a partialFD, if some attribute of A can be remoed andthe FD still holds

    • Formally, there is someproper subset of A,

    C ⊂  A, such that C → B 

    • Let us call attributes2hich are part of somecandidate 4ey, 4eyattributes, and the restnon;4ey attributes%

    Second normal form5 

    • A relation is in secondnormal form '(NF) if it is

    in 1NF and no non;4eyattribute is partiallydependent on acandidate 4ey

    • In other 2ords, no C → B 

    2here is a strict subsetof a candidate 4ey and !is a non;4ey attribute%

  • 8/21/2019 Normalisation 3NF

    14/24 

    Second Normal Form

    • 1NF is not in (NF

    • $e hae the FD

    6odule, Te/t7 → 

    6Lecturer, Dept7• !ut also

    6odule7 → 6Lecturer, Dept7

    • And so Lecturer andDept are partially

    dependent on theprimary 4ey

    1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1

      M2 D1 L1 T3

      M3 D1 L2 T4

      M4 D2 L3 T1

      M4 D2 L3 T5

      M5 D2 L4 T6

  • 8/21/2019 Normalisation 3NF

    15/24 

    +emoin" FDs

    • Suppose 2e hae arelation + 2ith scheme Sand the FD A → ! 2here

    A ! < 6 7∩• Let < S = 'A # !)

    • In other 2ords5

    • A = attributes on the lefthand side of the FD

    • ! = attributes on theri"ht hand side of the FD

    • = all other attributes

    • It turns out that 2e cansplit + into t2o parts5

    • +1, 2ith scheme # A

    • +(, 2ith scheme A # !• The ori"inal relation can

    be recoered as thenatural 0oin of +1 and+(5

    • + < +1 NAT#+AL 9:IN +( 

  • 8/21/2019 Normalisation 3NF

    16/24 

    1NF to (NF = -/ample1NF

    Module Dept Lecturer Text

      M1 D1 L1 T1

      M1 D1 L1 T2

      M2 D1 L1 T1  M2 D1 L1 T3

      M3 D1 L2 T4

      M4 D2 L3 T1

      M4 D2 L3 T5

      M5 D2 L4 T6

    2NFa

    Module Dept Lecturer 

      M1 D1 L1

      M2 D1 L1

      M3 D1 L2  M4 D2 L3

      M5 D2 L4

    2NF"

    Module Text

      M1 T1

      M1 T2

      M2 T1  M2 T3

      M3 T4

      M4 T1

      M4 T5

      M1 T6

  • 8/21/2019 Normalisation 3NF

    17/24 

    .roblems +esoled in (NF

    • .roblems in 1NF

    • INS-+T = ant add amodule 2ith no te/ts

    • #.DAT- = To chan"electurer for 1, 2ehae to chan"e t2oro2s

    • D-L-T- = If 2e

    remoe 3, 2eremoe L( as 2ell

    • In (NF the first t2oare resoled, but notthe third one

    2NFa

    Module Dept Lecturer 

      M1 D1 L1

      M2 D1 L1

      M3 D1 L2  M4 D2 L3

      M5 D2 L4

  • 8/21/2019 Normalisation 3NF

    18/24 

    .roblems +emainin" in (NF

    • INS-+T anomalies

    • ant add lecturers2ho teach no modules

    • #.DAT- anomalies• To chan"e the

    department for L1 2emust alter t2o ro2s

    • D-L-T- anomalies• If 2e delete 3 2e

    delete L( as 2ell

    2NFa

    Module Dept Lecturer 

      M1 D1 L1

      M2 D1 L1  M3 D1 L2

      M4 D2 L3

      M5 D2 L4

  • 8/21/2019 Normalisation 3NF

    19/24 

    Transitie FDs and 3NF

    • Transitie FDs5

    • A FD, A → C  is atransitie FD, if there

    is some set B suchthat A → B and B → C  are non;triial FDs

    •  A → B non;triialmeans5 B is not a

    subset of A• $e hae

     A → B → C 

    • Third normal form

    • A relation is in thirdnormal form '3NF) if

    it is in (NF and nonon;4ey attribute istransitiely dependenton a candidate 4ey

  • 8/21/2019 Normalisation 3NF

    20/24 

    Third Normal Form

    • (NFa is not in 3NF

    • $e hae the FDs

    6odule7 → 6Lecturer7

    6Lecturer7 → 6Dept7

    • So there is atransitie FD from theprimary 4ey 6odule7to 6Dept7

    2NFa

    Module Dept Lecturer 

      M1 D1 L1

      M2 D1 L1  M3 D1 L2

      M4 D2 L3

      M5 D2 L4

  • 8/21/2019 Normalisation 3NF

    21/24

     

    (NF to 3NF = -/ample

    2NFa

    Module Dept Lecturer 

      M1 D1 L1  M2 D1 L1

      M3 D1 L2

      M4 D2 L3

      M5 D2 L4

    3NFa

    Lecturer Dept

      L1 D1  L2 D1

      L3 D2

      L4 D2

    3NF"

    Module Lecturer 

      M1 L1  M2 L1

      M3 L2

      M4 L3

      M5 L4

  • 8/21/2019 Normalisation 3NF

    22/24

     

    .roblems +esoled in 3NF

    • .roblems in (NF

    • INS-+T = ant addlecturers 2ho teach

    no modules• #.DAT- = To chan"e

    the department for L12e must alter t2oro2s

    • D-L-T- = If 2e delete3 2e delete L( as2ell

    • In 3NF all of these areresoled 'for this relation =but 3NF can still haeanomalies>)

    3NFa

    Lecturer Dept

      L1 D1

      L2 D1  L3 D2

      L4 D2

    3NF"Module Lecturer 

      M1 L1

      M2 L1

      M3 L2

      M4 L3  M5 L4

  • 8/21/2019 Normalisation 3NF

    23/24

     

    Normalisation and Desi"n

    • Normalisation isrelated to D! desi"n

    • A database should

    normally be in 3NF atleast

    • If your desi"n leads toa non;3NF D!, thenyou mi"ht 2ant to

    reise it

    • $hen you find youhae a non;3NF D!

    • Identify the FDs that

    are causin" a problem

    • Thin4 if they 2ill leadto any insert, update,or delete anomalies

    • Try to remoe them

  • 8/21/2019 Normalisation 3NF

    24/24

    Ne/t Lecture

    • ore normalisation

    • Lossless decomposition? 2hy our reduction

    to (NF and 3NF is lossless• !oyce;odd normal form '!NF)

    • @i"her normal forms

    • Denormalisation

    • For more information• onnolly and !e"" chapter 1

    • #llman and $idom chapter 3%&