Tutorial 6: normalize the following relation to 1NF, 2NF, and 3NF TransIDRentDateCustomerIDLastNamePhoneAddressVideoIDCopy#TitleRent 14/18/043Washington Easy Street122001: A Space Odyssey$ /18/04 3Washington Easy Street63Clockwork Orange$ /30/04 7Lasater S. Ray Drive81Hopscotch$ /30/04 7Lasater S. Ray Drive21Apocalypse Now$ /30/04 7Lasater S. Ray Drive61Clockwork Orange$ /18/048Jones Lakeside Drive91Luggage Of The Gods$ /18/04 8Jones Lakeside Drive151Fabulous Baker Boys$ /18/04 8Jones Lakeside Drive41Boy And His Dog$ /18/043Washington Easy Street31Blues Brothers$ /18/04 3Washington Easy Street81Hopscotch$ /18/04 3Washington Easy Street131Surf Nazis Must Die$ /18/043Washington Easy Street171Witches of Eastwick$2.00 RentalForm(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode, Video(VideoID, Copy#, Title, Rent ) )
First Normal Form RentalForm(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode, (VideoID, Copy#, Title, Rent ) ) RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode) RentalLine(TransID, VideoID, Copy#, Title, Rent ) u Remove repeating sections –Split into two tables –Bring key from main and repeating section u RentalLine(TransID, VideoID, Copy#,...) –Each transaction can have many videos (key VideoID) –Each video can be rented on many transactions (key TransID) –For each TransID and VideoID, only one Copy# (no key on Copy#)
1NF: TransID RentDateCustIDPhoneLastNameFirstNameAddressCityStateZipCode 14/18/ WashingtonElroy95 Easy StreetSmith's GroveKY /30/ LasaterLes67 S. Ray DrivePortlandTN /18/ JonesCharlie867 Lakeside DriveCastalian SpringsTN /18/ WashingtonElroy95 Easy StreetSmith's GroveKY42171 TransIDVideoIDCopy#TitleRent : A Space Odyssey$ Clockwork Orange$ Hopscotch$ Apocalypse Now$ Clockwork Orange$ Luggage Of The Gods$ Fabulous Baker Boys$ Boy And His Dog$ Blues Brothers$ Hopscotch$ Surf Nazis Must Die$ Witches of Eastwick$2.00
Second Normal Form Definition u Each non-key column must depend on the entire key. –Only applies to concatenated keys –Some columns only depend on part of the key –Split those into a new table. RentalLine(TransID, VideoID, Copy#, Title, Rent) Depend only on VideoID Depends on both TransID and VideoID
Second Normal Form Example u Title depends only on VideoID –Each VideoID can have only one title u Rent depends on VideoID –This statement is actually a business rule. –It might be different at different stores. –Some stores might charge a different rent for each video depending on the day (or time). u Each non-key column depends on the whole key. RentalLine(TransID, VideoID, Copy#, Title, Rent) VideosRented(TransID, VideoID, Copy#) Videos(VideoID, Title, Rent)
Second Normal Form Example (Data) TransIDVideoIDCopy# VideoIDTitleRent 12001: A Space Odyssey$1.50 2Apocalypse Now$2.00 3Blues Brothers$2.00 4Boy And His Dog$2.50 5Brother From Another Planet$2.00 6Clockwork Orange$1.50 7Gods Must Be Crazy$2.00 8Hopscotch$1.50 VideosRented(TransID, VideoID, Copy#) Videos(VideoID, Title, Rent) RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode) (Unchanged)
Second Normal Form Problems (Data) TransIDRentDateCustIDPhoneLastNameFirstNameAddressCityStateZipCode 14/18/ WashingtonElroy95 Easy StreetSmith's GroveKY /30/ LasaterLes67 S. Ray DrivePortlandTN /18/ JonesCharlie867 Lakeside DriveCastalian SpringsTN /18/ WashingtonElroy95 Easy StreetSmith's GroveKY42171 RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode) u Even in 2NF, problems remain –Redundancy (Replication) –Hidden dependency –If a customer has not rented a video yet, where do we store their personal data? u Solution: split table.
Third Normal Form Definition RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode) u Each non-key column must depend on nothing but the key. –Some columns depend on columns that are not part of the key. –Split those into a new table. –Example: Customers name does not change for every transaction. u Dependence (definition) –If given a value for the key you always know the value of the property in question, then that property is said to depend on the key. –If you change the key and the questionable property does not change, then the table is not in 3NF. Depend only on CustomerID Depend on TransID
Third Normal Form Example u Customer attributes depend only on Customer ID –Split them into new table (Customer) –Remember to leave CustomerID in Rentals table. –We need to be able to reconnect tables. u 3NF is sometimes easier to see if you identify primary objects at the start--then you would recognize that Customer was a separate object. RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode) Rentals(TransID, RentDate, CustomerID ) Customers(CustomerID, Phone, Name, Address, City, State, ZipCode )
Third Normal Form Example Data TransIDRentDateCustomerID 14/18/ /30/ /18/048 44/18/043 CustomerIDPhoneLastNameFirstNameAddressCityStateZipCode JohnsonMartha125 Main StreetAlvatonKY SmithJack873 Elm StreetBowling GreenKY WashingtonElroy95 Easy StreetSmith's GroveKY AdamsSamuel746 Brown DriveAlvatonKY RabitzVictor645 White AvenueBowling GreenKY SteinmetzSusan15 Speedway DrivePortlandTN LasaterLes67 S. Ray DrivePortlandTN JonesCharlie867 Lakeside DriveCastalian TN ChavezJuan673 Industry Blvd.CaneyvilleKY RojoMaria88 Main StreetCave CityKY42127 Rentals(TransID, RentDate, CustomerID ) Customers(CustomerID, Phone, Name, Address, City, State, ZipCode ) VideosRented(TransID, VideoID, Copy#) Videos(VideoID, Title, Rent) RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode
Exercise : Put the following on the 1NF, 2NF and 3NF