Translating ER into Relations; Normalization INF1343, Week 5 Translating ER into Relations; Normalization Yuri Takhteyev University of Toronto January 31, 2011 This presentation is licensed under Creative Commons Attribution License, v. 3.0. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/. This presentation incorporates images from the Crystal Clear icon collection by Everaldo Coelho, available under LGPL from http://everaldo.com/crystal/.
building room probably redundant student course might be redundant ------------------------------------------------ code map url room ------------------------------------------------ number capacity probably redundant student ------------------------------------------------ last name first name course ------------------------------------------------ code title might be redundant session ------------------------------------------------ code start date
building room student course session code map url number capacity ------------------------------------------------ code map url room ------------------------------------------------ number capacity room is in building course meets in room student ------------------------------------------------ last name first name course ------------------------------------------------ code title student enrolls in course course is offered in session session ------------------------------------------------ code start date
building room student course session code map url number capacity ------------------------------------------------ code map url room ------------------------------------------------ number capacity student ------------------------------------------------ last name first name course ------------------------------------------------ code title session ------------------------------------------------ code start date
building room student course session code map url number capacity ------------------------------------------------ code map url room ------------------------------------------------ number capacity student ------------------------------------------------ last name first name course ------------------------------------------------ code title session ------------------------------------------------ code start date
building room student course session enrollment code map url number ------------------------------------------------ code map url room ------------------------------------------------ number capacity student ------------------------------------------------ last name first name enrollment course ------------------------------------------------ code title session ------------------------------------------------ code start date
building room course student session enrollment code map url number ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session course enrollment instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course ------------------------------------------------ code title course instance student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session course enrollment instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course ------------------------------------------------ code title course instance student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session course enrollment instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course ------------------------------------------------ code title course instance student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session course enrollment instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course instance course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session course enrollment instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment course instance course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
ER for M
Entities: → tables 1:M relationships: → primary and foreign keys ER to Relations Entities: → tables 1:M relationships: → primary and foreign keys
“Create one table for each entity” (after breaking up M:M) Step 1 “Create one table for each entity” (after breaking up M:M)
student first_name last_name student last name first name ------------------------------------------------ last name first name student first_name last_name
student name domain last_name first_name student last name first name ------------------------------------------------ last name first name student name domain last_name first_name
student name domain last_name varchar(100) first_name student ------------------------------------------------ last name first name student name domain last_name varchar(100) first_name
student name domain NULL last_name varchar(100) YES first_name NO ------------------------------------------------ last name first name student name domain NULL last_name varchar(100) YES first_name NO
(do this for every entity)
Creating primary and foreign keys to represent relationships Steps 2-4 Creating primary and foreign keys to represent relationships
building name domain NULL code varchar(100) NO map_url YES room name ------------------------------------------------ code map url room ------------------------------------------------ number capacity building name domain NULL code varchar(100) NO map_url YES room name domain NULL number integer NO capacity YES
building name domain NULL code varchar(100) NO map_url YES room name ------------------------------------------------ code map url room ------------------------------------------------ number capacity building name domain NULL code varchar(100) NO map_url YES room name domain NULL number integer NO capacity YES building_???
Keys student name last_name first_name no key!
Keys student name last_name first_name student_no a candidate key
Keys another candidate key name last_name first_name student student_no utorid another candidate key
Keys candidate #1 candidate #2 name last_name first_name student student_no utorid candidate #1 candidate #2
Keys primary key candidate #1 candidate #1 candidate #2 name last_name student name last_name first_name student_no* utorid primary key candidate #1 candidate #1 candidate #2
Multi-Column Keys city name province population a key!
Multi-Column Keys a three column key name province city country population a three column key
Multi-Column Keys candidate #1 candidate #2 name province city country population city_id candidate #1 candidate #2
“Natural” Keys building name code map_url a “natural” key e.g., “BL”
“Natural” Keys a “natural” key a “surrogate” key name code map_url building name code map_url building_id a “natural” key e.g., “BL” a “surrogate” key e.g., 56
Step 2 “For each entity that is only on the ‘one’ end of one or more relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”
Step 2 “For each entity that is only on the ‘one’ end of one or more relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
Step 2 “For each entity that is only on the ‘one’ end of one or more relationships and not at the ‘many’ end of any relationship, create a single-column primary key... if no natural primary key is available.”
student name last_name first_name
student name last_name first_name student_id* let’s add it
student name type null key last_name varchar(100) YES first_name NO student_id int PRI
building room course student session enrollment course instance code ------------------------------------------------ code map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code title student ------------------------------------------------ last name first name session ------------------------------------------------ code start date
building room course student session enrollment course instance code* ------------------------------------------------ code* map url room ------------------------------------------------ number capacity enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? course ------------------------------------------------ code* title student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code* start date
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
Step 3 “For each entity that is at the ‘many’ end of one or more relationships, include the primary key of each parent entity in the table as foreign keys.”
Step 3 “For each entity that is at the ‘many’ end of one or more relationships, include the primary key of each parent entity in the table as foreign keys.”
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id
Step 3 “For each entity that is at the ‘many’ end of one or more relationships, include the primary key of each parent entity in the table as foreign keys.”
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id
building room course student session enrollment course instance code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ course instance ------------------------------------------------ final exam? student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity building_id course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity building_id course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
Step 4a “If an entity at the ‘many’ end of one or more relationships, has a natural primary key, use that single key as a the primary key.”
? building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity building_id ? course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
Step 4b “Otherwise, concatenate the primary key of its parent with any other column or columns needed for uniqueness to form the table’s primary key.”
building_id + number building room course student session course ------------------------------------------------ code map url building_id* room ------------------------------------------------ number capacity building_id building_id + number course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
If necessary, add a field! Step 4c If necessary, add a field!
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
Step 4d Go back to step 3!
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id room_id course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id* session_id* room_id* course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id room_id instance_id* course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id* instance_id* student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id room_id instance_id* course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id* instance_id* student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
course_instance name type null key instance_id int NO PRI course_id session_id room_id final_exam bool
building room course student session course instance enrollment code ------------------------------------------------ code map url building_id* room ------------------------------------------------ code capacity building_id room_id* course instance ------------------------------------------------ final exam? course_id session_id room_id instance_id* course ------------------------------------------------ code title course_id* enrollment ------------------------------------------------ student_id* instance_id* student ------------------------------------------------ last name first name student_id* session ------------------------------------------------ code start date session_id*
course_instance name type null key instance_id int NO PRI course_id session_id room_id YES final_exam bool
Step 5 Normalize!
Checking that the table does not have any well-known problems Normalization Checking that the table does not have any well-known problems
What Problems? problem type cause solution update redundancy decomposition insertion deletion
Normal Forms 5th Normal Form 4th Normal Form BC Normal Form fixing weird issues 4th Normal Form BC Normal Form 3rd Normal Form you can do it! 2nd Normal Form 1st Normal Form trivial!
No multi-valued attributes 1NF No multi-valued attributes
pet_id owner names 1 Bob “Slim”, “the Serpent”, “Ribbon” 2 Gwen “Fluffy”, “Big Dog”
The Wrong Solution pet_id owner name1 name2 name3 1 Bob “Slim” “the Serpent” “Ribbon” 2 Gwen “Fluffy” “Big Dog”
The Right Solution pet_id owner 1 Bob 2 Gwen pet_id names 1 Slim the Serpent Ribbon 2 Fluffy Big Dog
Getting rid of “functional dependencies” 2NF and 3NF Getting rid of “functional dependencies”
“Each attribute must describe the key, student id course id course title student name grade code grade points “Each attribute must describe the key, the whole key, and nothing but the key.” “So help me Codd!”
“Each attribute must describe the key, partial dependency student id course id course title student name grade code full dependency grade points transitive dependency “Each attribute must describe the key, the whole key, and nothing but the key.” “So help me Codd!”
2NF and 3NF 2NF: no partial dependencies (“the whole key”) 3NF: 2NF + no transitive dependencies (“nothing but the key”)
Decomposition (break up tables into several) Reaching 2NF and 3NF Decomposition (break up tables into several)
student id course id course title student name grade code grade points
student id course student id student name course id grade code grade points course id course title
student id course student id student name course id grade code course id course title grade code grade points
“all determinants must be candidate keys” BCNF “all determinants must be candidate keys”
overlapping candidate keys room id course time slot instructor id course code overlapping candidate keys
dependencies between keys room id course time slot instructor id dependencies between keys
dependencies between keys room id course time slot instructor id dependencies between keys
this is 3NF, but not BCNF (“so help me Codd”) instructor utorid course time slot instructor id this is 3NF, but not BCNF (“so help me Codd”)
decompose it instructor id time slot course instructor id instructor utorid decompose it
some relations cannot be decomposed into BCNF student id student, course → project project → course course id course project id some relations cannot be decomposed into BCNF (“so help me Codd”)
Mistaking binary relationships for ternary 4NF Mistaking binary relationships for ternary
room course session code capacity code title code start date ------------------------------------------------ code capacity course ------------------------------------------------ code title session ------------------------------------------------ code start date
room course session course instance code capacity code title code ------------------------------------------------ code capacity course ------------------------------------------------ code title course instance session ------------------------------------------------ code start date
name student_id course_id internship_id
“We could break it up yet more” 5NF “We could break it up yet more”
Questions?