Normalisation-Introduction to Database System-Lecture 13 Slides-Computer Science, Slides of Introduction to Database Management Systems

Normalisation, Functional Dependencies, FD Diagram, Normalisation to 2NF, Normalisation to 3NF, 1NF, 2NF, 3NF, Stream Relation, Anomalies in Stream, Boyce-codd Normal Form, Conversion to BCNF, Stream and BCNF, Decomposition Properties, Higher Normal Forms, Denormalisation

Typology: Slides

2011/2012

Uploaded on 02/12/2012

jeanette
jeanette 🇬🇧

3.7

(7)

237 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Normalisation so Far
First normal form
All data values are
atomic
Second normal form
As 1NF plus no non-
primary-key attribute
is partially dependant
on the primary key
Third normal form
As 2NF plus no non-
primary-key attribute
depends transitively on
the primary key
These are all based
on a relation with a
single (primary) key
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Normalisation Example
We have a table
representing orders
in an online store
Each entry in the
table represents an
item on a par ticular
order
Columns
•Order
Product
•Customer
Address
Quantity
•UnitPrice
Primary key is
{Order, Product}
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Functional Dependencies
Each order is for a
single customer
Each customer has a
single address
Each product has a
single pric e
FDs 1 and 2 and
transitivity
•{Order}
{Customer}
•{Customer}
{Address}
•{Product}
{UnitPrice}
•{Order}
{Address}
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Example – FD Diagram
Order Product Customer Address Quantity UnitPrice
R
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Normalisation to 2NF
Second normal form
means no p artial
dependencies on the
primary key
•{Order}
{Customer, Address}
•{Product}
{UnitPrice}
To remove the first
FD we project over
{Order, Customer,
Address}
•And
•{Order, Product,
Quantity and
UnitPrice}
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
Normalisation to 2NF
Order Product Customer Address Quantity UnitPrice
R
Order Product Quantity UnitPr ice
Order Customer Address
R1
R2
pf3
pf4
pf5

Partial preview of the text

Download Normalisation-Introduction to Database System-Lecture 13 Slides-Computer Science and more Slides Introduction to Database Management Systems in PDF only on Docsity!

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation so Far

  • First normal form
    • All data values are atomic
  • Second normal form
    • As 1NF plus no non- primary-key attribute is partially dependant on the primary key - Third normal form - As 2NF plus no non- primary-key attribute depends transitively on the primary key - These are all based

on a relation with a

single (primary) key

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation Example

  • We have a table

representing orders

in an online store

  • Each entry in the

table represents an

item on a particular

order

  • Columns
    • Order
    • Product
    • Customer
    • Address
    • Quantity
    • UnitPrice
  • Primary key is

{Order, Product}

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Functional Dependencies

  • Each order is for a

single customer

  • Each customer has a

single address

  • Each product has a

single price

  • FDs 1 and 2 and

transitivity

  • {Order} →

{Customer}

  • {Customer} →

{Address}

  • {Product} →

{UnitPrice}

  • {Order} →

{Address}

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Example – FD Diagram

Order Product Customer Address Quantity UnitPrice

R

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 2NF

  • Second normal form

means no partial

dependencies on the

primary key

  • {Order} → {Customer, Address}
  • {Product} → {UnitPrice} - To remove the first

FD we project over

  • {Order, Customer, Address}
  • And
  • {Order, Product, Quantity and UnitPrice}

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 2NF

Order Product Customer Address Quantity UnitPrice

R

Order Product Quantity UnitPrice

Order Customer Address

R

R

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 2NF

  • R1 is now in 2NF,

but there is still a

partial FD in R

  • {Product} → {UnitPrice} - To remove this we

project over

  • {Product, UnitPrice}
  • And
  • {Order, Product, Quantity}

Order Product Quantity UnitPrice

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 2NF

Order Product Quantity UnitPrice

R

Product UnitPrice

Order Product Quantity

R

R

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 3NF

  • R has now been split

into 3 relations - R1,

R3, and R

  • R3 and R4 are 3NF
  • R1 has a transitive FD on its key - To remove {Order} → {Customer} → {Address} - we project R

over

  • {Order, Customer}
  • {Customer, Address}

Order Customer Address

R

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation to 3NF

Order Customer Address

R

Order Customer

Customer Address

R

R

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation

  • 1NF:
    • {Order, Product, Customer, Address, Quantity, UnitPrice}
  • 2NF:
    • {Order, Customer, Address}, {Product, UnitPrice}, and {Order, Product, Quantity}
  • 3NF:
    • {Product, UnitPrice}, {Order, Product, Quantity}, {Order, Customer}, and {Customer, Address}

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

2NF Revisited

  • We consider

relations with several

candidate keys

  • An attribute of a

relation is called

prime if it is part of a

candidate key, and

non-prime otherwise

  • Second normal form
    • A relation is in second normal form if it is in first normal form and no non-prime attribute is partially dependent on any candidate key
    • If there is only one key, this is the same as before

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Anomalies in Stream

  • INSERT anomolies
    • You can’t add an empty stream
  • UPDATE anomolies
    • Moving the 12: class to 9:00 means changing two rows
  • DELETE anomolies
    • Deleting Rebecca removes a stream

Student Course Time John Databases 12: Mary Databases 12: Richard Databases 15: Richard Programming 10: Mary Programming 10: Rebecca Programming 13:

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Boyce-Codd Normal Form

  • A relation is in

Boyce-Codd normal

form (BCNF) if for

every FD A → B

either

  • B is contained in A (the FD is trivial), or
  • A contains a candidate key of the relation - The same as 3NF

except we don’t

allow B to be prime

  • If there is only one

candidate key then

3NF and BCNF are

the same

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Stream and BCNF

  • Stream is not in

BCNF as the FD

{Time} → {Course}

is non-trivial and

{Time} does not

contain a candidate

key

Student Course Time

Student Course Time John Databases 12: Mary Databases 12: Richard Databases 15: Richard Programming 10: Mary Programming 10: Rebecca Programming 13:

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Conversion to BCNF

Student Course Time

Student Course Course Time

Stream has been put into BCNF but we have lost the FD

{Student, Course} → {Time}

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Decomposition Properties

  • Lossless: Data should

not be lost or created

when splitting

relations up

  • Dependency

preservation: It is

desirable that FDs are

preserved when

splitting relations up

  • Normalisation to 3NF

is always lossless and

dependency

preserving

  • Normalisation to

BCNF is lossless, but

may not preserve all

dependencies

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Higher Normal Forms

  • BCNF is as far as we

can go with FDs

  • Higher normal forms are based on other sorts of dependency
  • Fourth normal form removes multi-valued dependencies
  • Fifth normal form removes join dependencies

1NF Relations

2NF Relations

3NF Relations

BCNF Relations

4NF Relations

5NF Relations

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Denormalisation

  • Normalisation
    • Removes data redundancy
    • Solves INSERT, UPDATE, and DELETE anomalies
    • This makes it easier to maintain the information in the database in a consistent state - However - It leads to more tables in the database - Often these need to be joined back together, which is expensive to do - So sometimes (not often) it is worth ‘denormalising’

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Denormalisation

  • You might want to

denormalise if

  • Database speeds are unacceptable (not just a bit slow)
  • There are going to be very few INSERTs, UPDATEs, or DELETEs
  • There are going to be lots of SELECTs that involve the joining of tables

Number Street City Postcode

Address

Not normalised since

{Postcode} → {City}

Number Street Postcode

City

Address

Postcode

Address

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation in Exams

Given a relation with scheme {ID, Name, Address, Postcode, CardType, CardNumber}, the candidate key {ID}, and the following functional dependencies:

  • {ID} → {Name, Address, Postcode, CardType, CardNumber}
  • {Address} → {Postcode}
  • {CardNumber} → {CardType}

(i) Explain why this relation is in second normal form, but not in third normal form.

(3 marks)

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Normalisation in Exams

(ii) Show how this relation can be converted to third normal form. You should show what functional dependencies are being removed, explain why they need to be removed, and give the relation(s) that result. (4 marks)

(iii) Give an example of a relation that is in third normal form, but that is not in Boyce-Codd normal form, and explain why it is in third, but not Boyce-Codd, normal form. (4 marks)