PPT UEU Basis Data Pertemuan 11

  

Normalisation to 3NF

Database Systems Lecture 11

Munawar, PhD

  In This Lecture

  • Normalisation to 3NF
  • Data redundancy
  • Functional dependencies
  • Normal forms
  • • First, Second, and Third Normal Forms

  • For more information
  • Connolly and Begg chapter 13

  nd

  • Ullman and Widom ch.3.6.6 (2 edition),

  rd 3.5 (3 edition)

  

Redundancy and Normalisation

  • Redundant data • Normalisa>Can be determined • Aims to reduce data from other data in the redundancy database
  • Redundanc
  • Leads to various expressed in terms of problems

  dependencies

  • INSERT anoma>Normal forms
  • UPDATE anomalies

  defined that do not have certain types of

  • DELETE anomalies

  dependency

  

First Normal Form

  • In most definitions of • A relation is said to the relational model be in first no
  • All data values should form (1NF) if all data be atomic

  values are atomic

  • This means that table entries should be single values, not sets or composite objects

  Normalisation to 1NF

To convert to a 1NF relation, split up any non-atomic values

  1NF Module Dept Lecturer Text

  Unnormalised M1 D1 L1 T1

  Module Dept Lecturer Texts M1 D1 L1 T2

  M1 D1 L1 T1, T2 M2 D1 L1 T1

  M2 D1 L1 T1, T3 M2 D1 L1 T3 M3 D1 L2 T4 M3 D1 L2 T4

  M4 D2 L3 T1, T5 M4 D2 L3 T1 M5 D2 L4 T6 M4 D2 L3 T5

  Problems in 1NF

  • INSERT anomalies

  1NF

  • Can't add a module Module Dept Lecturer Text with no texts

  M1 D1 L1 T1

  • UPDATE anomalies

  M1 D1 L1 T2

  • To change lecturer for M2 D1 L1 T1

  M1, we have to M2 D1 L1 T3 change two rows M3 D1 L2 T4

  • DELETE anomalies

  M4 D2 L3 T1

  • If we remove M3, we M4 D2 L3 T5 remove L2 as well

  M5 D2 L4 T6

  

Functional Dependencies

  • Redundancy is often • A set of attributes, A, caused by a functional functionally determines dependency another set, B, or: there exists a functional
  • A functional dependency

  dependency between A

  (FD) is a link between and B ( ), if

A  B

  two sets of attributes in whenever two rows of a relation the relation have the

  • We can normalise a same values for all the relation by removing attributes in A, then they undesirable FDs also have the same values for all the attributes in B.

  ID First modCode modName Last

  Example

  • {ID, modCode}  {First, Last, modName}
  • {modCode}  {modName}
  • {ID}  {First, Last}

  111 G51PRG Programming Joe Bloggs

  222 G51DBS Databases Anne Smith

  

FDs and Normalisation

  • We define a set of • Not all FDs cause a 'normal forms' pro>Each normal form has • We identify various fewer FDs than the sorts of FD that do last
  • Each normal >Since FDs represent removes a type of FD redundancy, each that is a problem normal form has less
  • We will also need a redundancy than the way to remove FDs last

  

Properties of FDs

  • In any rela>Rules for
  • The primary key FDs any set of attributes in that relation
  • Reflexivity: If B is a subset of A then

  KX

  • Augmentation: If
  • K is the primary key,

  X is a set of attributes

  • Transitivity: If A B and B C then
  • Same for candidate keys
  • Any set of attributes is FD on itself

  XX

  A B

  A B then A U C B U C

  A C FD Example

  • The primary key is

  1NF

  {Module, Text} so

  Module Dept Lecturer Text {Module, Text}  {Dept,

  Lecturer} M1 D1 L1 T1 M1 D1 L1 T2

  • 'Trivial' FDs, eg:

  M2 D1 L1 T1 {Text, Dept}  {Text}

  M2 D1 L1 T3 {Module}  {Module}

  M3 D1 L2 T4 M4 D2 L3 T1 {Dept, Lecturer}  { } M4 D2 L3 T5 M5 D2 L4 T6

  FD Example

  • Other FDs are

  1NF

  • {Module}  Module Dept Lecturer Text {Lecturer}
  • {Module}  {Dept} M1 D1 L1 T1 M1 D1 L1 T2
  • {Lecturer}  {Dept} M2 D1 L1 T1
  • These are non-trivial M2 D1 L1 T3 and determinants (left

  M3 D1 L2 T4 hand side of the M4 D2 L3 T1 dependency) are not M4 D2 L3 T5 keys. M5 D2 L4 T6

  

Partial FDs and 2NF

  • Partial FDs: Second normal f>A FD, A B is a par
  • A relation is in second

  FD, if some attribute of

  normal form (2NF) if it is

  A can be removed and

  in 1NF and no non-key

  the FD still holds

  attribute is partially

  • Formally, there is some

  dependent on a

  proper subset of A,

  candidate key

  C A, such that C B

  • In other words, no C B • Let us call attributes where C is a strict subset which are part of some of a candidate key and B candidate key, key is a non-key attribute.

  attributes, and the rest

  Second Normal Form

  1NF

  • 1NF is not in 2NF

  Module Dept Lecturer Text

  • We have the FD M1 D1 L1 T1

  {Module, Text} 

  M1 D1 L1 T2

  {Lecturer, Dept}

  M2 D1 L1 T1

  • But also M2 D1 L1 T3

  {Module}  {Lecturer, Dept}

  M3 D1 L2 T4

  • And so Lecturer and M4 D2 L3 T1

  Dept are partially M4 D2 L3 T5 dependent on the M5 D2 L4 T6 primary key

  

Removing FDs

  • Suppose we have a • It turns out that we can relation R with scheme S split R into two parts: and the FD A  B where
  • R1, with scheme C U A A B = { } ∩
  • R2, with scheme A U B • Let C = S – (A U B)
  • The original relation>In other words: be recovered as the natural join of R1>A – attributes on the left hand side of the FD R2:
  • B – attributes on>R = R1 NATURAL JOIN R2 right hand side of the FD
  • C – all other attributes

  

1NF to 2NF – Example

  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

  2NFa Module Dept Lecturer M1 D1 L1 M2 D1 L1 M3 D1 L2 M4 D2 L3 M5 D2 L4

  2NFb Module Text M1 T1 M1 T2 M2 T1 M2 T3 M3 T4 M4 T1 M4 T5 M1 T6

  2NFa Module Dept Lecturer M1 D1 L1 M2 D1 L1 M3 D1 L2 M4 D2 L3 M5 D2 L4 Problems Remaining in 2NF

  

Problems Resolved in 2NF

  • Problems in
  • In 2NF the first two are resolved, but not the third one
  • >INSERT – Can't add a module with no texts
  • UPDATE – To change lecturer for M1, we have to change two rows
  • DELETE – If we remove M3, we remove L2 as well
  •   2NFa

    • INSERT anomalies

      Module Dept Lecturer

    • Can't add lecturers

      M1 D1 L1 who teach no modules M2 D1 L1

    • UPDATE anomalies

      M3 D1 L2

    • To change the

      M4 D2 L3 department for L1 we M5 D2 L4 must alter two rows

    • DELETE anoma
    • If we delete M3 we delete L2 as well

      ABC Third Normal Form

      

    Transitive FDs and 3NF

    • Transitive >Third normal
    • A relation is in third normal form (3NF) if it is in 2NF and no non-key attribute is transitively dependent on a candidate key
    • A FD, A C is a transitive FD, if there is some set B such that AB and B C are non-trivial FDs
    • AB non-trivial means: B is not a subset of A • We have

      2NFa

    • 2NFa is not in 3NF

      Module Dept Lecturer

    • We have the FDs M1 D1 L1

      {Module}  {Lecturer} M2 D1 L1

      {Lecturer}  {Dept} M3 D1 L2

    • So there is a M4 D2 L3 transitive FD from the

      M5 D2 L4 primary key {Module} to {Dept}

      2NF to 3NF – Example

      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

      3NFb Module Lecturer M1 L1 M2 L1 M3 L2 M4 L3 M5 L4 Problems Resolved in 3NF

    • In 3NF all of these are resolved (for this relation – but 3NF can still have anomalies!)
    • >Problems in
    • INSERT – Can't add lecturers who teach no modules
    • UPDATE – To change the department for L1 we must alter two rows
    • DELETE – If we delete

      M3 we delete L2 as well

      3NFa Lecturer Dept L1 D1 L2 D1 L3 D2 L4 D2

      3NFb Module Lecturer M1 L1 M2 L1 M3 L2 M4 L3 M5 L4

      

    Normalisation and Design

    • Normalisation is • When you find you

      related to DB design have a non-3NF DB>A database should • Identify the FDs that normally be in 3NF at are causing a problem least

    • Think if they will >If your design leads to to any insert, update, a non-3NF DB, then or delete anomalies you might want to
    • Try to remove them revise it

      Next Lecture