210 likes | 362 Views
Week 4: Normalisation: Redundant data becomes inconsistent data; therefore … “The key, the whole key, and nothing but the key,so help me, Codd”. BoyGirl Database, Version 0. NOTE: Not a good design!. Because one girl can have many boys …
E N D
Week 4: Normalisation:Redundant data becomes inconsistent data; therefore …“The key, the whole key, and nothing but the key,so help me, Codd”
BoyGirl Database, Version 0 • NOTE: Not a good design! • Because one girl can have many boys … • .. we are storing redundant mobile data about Bonnie. • “Redundant data becomes inconsistent data”
Foreign key A Better BoyGirl Database Primary key Primary key • Eliminated redundant data about Bonnie • Two tables with a One-to-Many relationship … • … linked by a Foreign Key • http://www-staff.it.uts.edu.au/~raymond/db/boygirl.sql
The Menace of Redundant Data • Redundant data becomes inconsistent data • Insert, modify, and delete more data than desired • Strive for one fact in one place • More technically, strive for 3NF or BCNF (more later)
Forgotten what these American University terms mean???? See next slide … Big University Table – What’s redundant here? StdSSN OfferNo CourseNo StdSSN, OfferNo StdCity, StdClass Solution is to split the single table into two or more Tables. Like we did with BoyGirl. OffTerm, OffYear, CourseNo CrsDesc EnrGrade
Big University Table – Forgotten these American terms ???? Offering Course Primary Key OfferNo CourseNo OffYear CrsDesc CourseNo Attributes
Functional Dependencies (FDs) y f(x) = 2x • X Y • “X (functionally) determines Y” • For each X value, there is at most one Y value • “Normalisation” is the process of splitting tables to remove redundancies x
FD’s in Data • Prove non-existence (but not existence) by looking at data • Two rows that have the same X value but a different Y value • Understand business rules (or common sense)
Toward First Normal Form (1NF) Normalisation step by step Step 1: Identification of superkeys. According to the previous FD diagram, we know that StdSSN, OfferNo and CourseNo form a “superkey”
First Normal Form (1NF) Step 2: Determining primary key (minimal superkey). If we analyze our superkey we can conclude that CourseNo is not contributing to the uniqueness of our superkey, therefore we can take it out of the key.
Second Normal Form (2NF) • Every non-key column depends on a whole key, not part of a key • Violations • Part of key non-key • Violations only for combined keys • “combined” “composite”
2NF Example (problem) • Violations of 2NF form in the 1NF big university database table • StdSSN StdCity, StdClass • OfferNo OffTerm, OffYear, CourseNo, CrsDesc
2NF Example (solution) • Splitting the table • UnivTable0 (StdSSN*, OfferNo*, EnrGrade) • UnivTable1 (StdSSN, StdCity, StdClass) • UnivTable2 (OfferNo, OffTerm, OffYear, CourseNo, CrsDesc) • Where … • Underlining means “primary key” • Asterisk means foreign key
To get to Third Normal Form (3NF) • We need to ensure that … • Every non-key column depends only on a key not on non-key columns • Violations: Non-key Non-key • OfferNo CourseNo, CourseNo CrsDesc then OfferNo CrsDesc
3NF Example • One violation in UnivTable2 • CourseNo CrsDesc • Splitting the table • UnivTable2-1 (OfferNo, OffTerm, OffYear, CourseNo*) • UnivTable2-2 (CourseNo, CrsDesc)
BCNF • A special case not covered by 3NF • Where two things can be used as substitute primary keys for each other • E.g. staff number, tax file number, email address
BCNF Example UnivTable4 (Mannino’s example page 236-238) Convert from 3NF to BCNF by placing the redundant keys in a table by themselves and only using one of them in other tables: UnivTable4-1 (StdSSN*, OfferNo, EnrGrade) UnivTable4-2 (StdSSN,Email)
Role of Normalisation • Normalisation and drawing ERDS are complimentary ways of designing databases. • Strive to reach at least 3NF, hopefully BCNF. • There are even higher normal forms, 4NF, 5NF, etc, but we don’t talk about them in this course. • They are almost never an issue in real work databases. May reverse engineer an ERD
Today’s Lab Exercise(this slide not part of today’s lecture handout) Familiarisation with next week’s lab exam database. Download the lab exam database into your PostgreSQL account. See URL on page 2 of handout. Attempt the sample questions on page 5 of handout. Answers on page 6.