Lecture On Database Normalisation
Lecture On Database Normalisation
Lecture On Database Normalisation
In This Lecture
More normalisation
Brief review of relational algebra Lossless decomposition Boyce-Codd normal form (BCNF) Higher normal forms Denormalisation
Normalisation so Far
First normal form
All data values are atomic
Lossless decomposition
To normalise a relation, we used projections If R(A,B,C) satisfies AB then we can project it on A,B and A,C without losing information Lossless decomposition: R = AB(R) AC(R) where AB(R) is projection of R on AB and is natural join. Reminder of projection:
R
A B C
AB(R) A B
A,B
(R1R2R3)
Module,LecturerR
Text
CB UW UW B
Module,TextR
Module Text
DBS DBS RDB APS CB UW UW B
Module Lecturer
DBS RDB APS nza nza rcb
First,LastS
Last
Smith Brown Smith Brown
First,AgeS
First
John John Mary Tom
Age
20 30 20 10
First
John John Mary Tom
Last
Smith Brown Smith Brown
Age
20 30 20 10
First,LastS
First
John John Mary Tom
First,AgeS
First
John John Mary Tom
Last
Smith Smith Brown Brown Smith Brown
Age
20 30 20 30 20 10
Last
Smith Brown Smith Brown
Age
20 30 20 10
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
Functional Dependencies
Each order is for a single customer Each customer has a single address Each product has a single price From FDs 1 and 2 and transitivity {Order} {Customer}
{Customer}{Address} {Product} {UnitPrice} {Order} {Address}
Normalisation to 2NF
Second normal form means no partial dependencies on candidate keys
{Order} {Customer, Address} {Product} {UnitPrice}
and
{Order, Product, Quantity, UnitPrice} (R2)
Normalisation to 2NF
R1 is now in 2NF, but there is still a partial FD in R2
{Product} {UnitPrice}
To remove this we project over {Product, UnitPrice} (R3) and {Order, Product, Quantity} (R4)
Normalisation to 3NF
R has now been split into 3 relations - R1, R3, and R4
R3 and R4 are in 3NF R1 has a transitive FD on its key
To remove
{Order} {Customer} {Address}
we project R1 over
{Order, Customer} {Customer, Address}
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}
Anomalies in Stream
INSERT anomalies
You cant add an empty stream Student John Mary Richard Richard Mary Rebecca Course Databases Databases Databases Programming Programming Programming Time 12:00 12:00 15:00 10:00 10:00 13:00
UPDATE anomalies
Moving the 12:00 class to 9:00 means changing two rows
DELETE anomalies
Deleting Rebecca removes a stream
The same as 3NF except in 3NF we only worry about non-key Bs If there is only one candidate key then 3NF and BCNF are the same
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}
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
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
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 Address Number Street City Postcode
Address2 Postcode
City
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)
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)
Next Lecture
Physical DB Issues
RAID arrays for recovery and speed Indexes and query efficiency
Query optimisation
Query trees