Docsity

Normalisation to 3NF, Exercises of Database Management Systems (DBMS)

Partial preview of the text

Download Normalisation to 3NF and more Exercises Database Management Systems (DBMS) in PDF only on Docsity!

Normalisation to 3NFDatabase Systems Lecture 11Natasha Alechina

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• Ullman and Widom ch.3.6.6 (

nd^ edition),

rd^ 3.5 (3edition)

First Normal Form

  • In most definitionsof the relationalmodel• All data values shouldbe atomic• This means that tableentries should besingle values, not setsor composite objects
  • A relation is said tobe in first normalform (1NF) if all datavalues are atomic

Normalisation to 1NF UnnormalisedModule Dept^ Lecturer

Functional Dependencies• Redundancy is oftencaused by a functionaldependency• A functional dependency(FD) is a link betweentwo sets of attributes in arelation• We can normalise arelation by removingundesirable FDs

-^ A set of attributes, A,functionally determinesanother set, B, or: thereexists a functionaldependency between Aand B (A

^ B), ifwhenever two rows ofthe relation have thesame values for all theattributes in A, then theyalso have the samevalues for all theattributes in B.

Example

-^ {ID, modCode}

^ {First, Last, modName}

-^ {modCode}

^ {modName}

-^ {ID}^ ^ {First, Last}ID^

modCode^

modName First^ Last 111

G51PRG^

Programming Joe^ Bloggs 222

G51DBS^

Databases Anne^ Smith

Properties of FDs

  • In any relation• The primary key FDsany set of attributesin that relation

K^ ^ X • K is the primary key, X is a set ofattributes• Same for candidatekeys• Any set of attributesis FD on itself X^ ^ X

  • Rules for FDs• Reflexivity: If

B^ is a subset of^ A then AB

  • Augmentation: If^ A^ ^ B^ then^ A^ U^ C^

^ B^ U^ C

  • Transitivity:If^ A^ ^ B^ and

B^ ^ C^ then AC

FD Example

  • The primary key is{Module, Text} so{Module, Text}

{Dept, Lecturer}• 'Trivial' FDs, eg:{Text, Dept}^

^ {Text} {Module}^ 

{Module} {Dept, Lecturer}

^ { }

1NFModule^ Dept

Lecturer^ TextM1 D1 L^

T

M1^ D^

L1^ T

M2^ D^

L1^ T

M2^ D^

L1^ T

M3^ D^

L2^ T

M4^ D^

L3^ T

M4^ D^

L3^ T

M5^ D^

L4^ T

Partial FDs and 2NF• Partial FDs:• A FD,^ A^ ^ B^ is a partialFD, if some attribute of A can be removed andthe FD still holds• Formally, there is someproper subset of^

A , C^ ^ A , such that

C^ ^ B

-^ Let us call attributeswhich are part of somecandidate key, keyattributes, and the restnon-key attributes.

Second normal form:•^ A relation is in secondnormal form (2NF) if it isin 1NF and no non-keyattribute is partiallydependent on acandidate key•^ In other words, no

C^ ^ B

where C is a strict subsetof a candidate key and Bis a non-key attribute.

Second Normal Form

  • 1NF is not in 2NF• We have the FD{Module, Text}

 {Lecturer, Dept}

  • But also{Module}^  {Lecturer, Dept} - And so Lecturer andDept are partiallydependent on theprimary key 1NFModule^ Dept

Lecturer^ TextM1 D1 L^

T

M1^ D^

L1^ T

M2^ D^

L1^ T

M2^ D^

L1^ T

M3^ D^

L2^ T

M4^ D^

L3^ T

M4^ D^

L3^ T

M5^ D^

L4^ T

1NF to 2NF – Example

 - M1 D Texts - L1 T1, T - M2 D - L1 T1, T - M3 D - L2 T - M4 D - L3 T1, T - M5 D - L4 T 
  • Lecturer TextM1 D1 L 1NFModule Dept
    • T
      • M1 D
      • L1 T - M2 D - L1 T - M2 D - L1 T - M3 D - L2 T - M4 D - L3 T - M4 D - L3 T - M5 D - L4 T
  • Lecturer TextM1 D1 L 1NFModule Dept
    • T
      • M1 D
      • L1 T
        • M2 D
        • L1 T
          • M2 D
          • L1 T
            • M3 D
            • L2 T
              • M4 D
              • L3 T
                • M4 D
                • L3 T
                  • M5 D
                  • L4 T
    • Lecturer TextM1 D1 L 1NFModule Dept
      • T
        • M1 D
        • L1 T
          • M2 D
          • L1 T
            • M2 D
            • L1 T
              • M3 D
              • L2 T
                • M4 D
                • L3 T
                  • M4 D
                  • L3 T
                    • M5 D - L4 T - LecturerM1 D1 L1M2 D1 L1M3 D1 L2M4 D2 L3M5 D2 L 2NFaModule Dept - 2NFbModule TextM1 T1M1 T2M2 T1M2 T3M3 T4M4 T1M4 T5M1 T

Problems Resolved in 2NF• Problems in 1NF• INSERT – Can't add amodule with no texts• UPDATE – To changelecturer for M1, wehave to change tworows• DELETE – If weremove M3, weremove L2 as well

  • In 2NF the first twoare resolved, but notthe third one^ 2NFaModule^ Dept

LecturerM1 D1 L1M2 D1 L1M3 D1 L2M4 D2 L3M5 D2 L

Transitive FDs and 3NF• Transitive FDs:• A FD,^ A^ ^ C

is a transitive FD, if thereis some set

B^ such that^ A^ ^ B^

and^ B^ ^ C are non-trivial FDs• A^ ^ B^ non-trivialmeans:^ B^ is not asubset of^ A • We have^ A^ ^ B^

^ C

  • Third normal form• A relation is in thirdnormal form (3NF) ifit is in 2NF and nonon-key attribute istransitively dependenton a candidate key

Third Normal Form

  • 2NFa is not in 3NF• We have the FDs{Module}

^ {Lecturer}{Lecturer}^ ^ {Dept}• So there is atransitive FD from theprimary key {Module}to {Dept}

2NFaModule^ Dept

LecturerM1 D1 L1M2 D1 L1M3 D1 L2M4 D2 L3M5 D2 L