Translating ER into Relations; Normalization

Slides:



Advertisements
Similar presentations
FEN Introduction to the database field: Quality checking table design: Design Guidelines Normalisation Seminar: Introduction to relational.
Advertisements

Boyce-Codd NF Takahiko Saito Spring 2005 CS 157A.
Normalization What is it?
Normalization Lite Pepper. Golden Rule Every attribute must depend upon the key, --- > 1NF the whole key, --- > 2NF and nothing but the key. -  3NF and.
Normalization Dr. Mario Guimaraes. Data Normalization Primarily a tool to validate and improve a logical design so that it satisfies certain constraints.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Class Agenda (04/03 and 04/08)  Review and discuss HW #8 answers  Present normalization process Enhance conceptual knowledge of database design. Improve.
Logical Data Modeling Review Lecture for University of Agder, Grimstad DAT202 Databaser (5.5.11) Judith Molka-Danielsen
Design Guidelines Normalisation Table Design. Informal Design Guidelines Table Semantics A table should hold information about one and only one entity/concept.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Database Design Conceptual –identify important entities and relationships –determine attribute domains and candidate keys –draw the E-R diagram Logical.
Project and Data Management Software
Why Normalization? To Reduce Redundancy to 1.avoid modification, insertion, deletion anomolies 2.save space Goal: One Fact in One Place.
Database Design & ER Diagrams
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
COURSE REGISTRATION SYSTEM Case study IST2101. Case Study: Course Registration (1) IST2102 You are helping Penn State create a course registration system.
SQL Normalization Database Design Lecture 5. Copyright 2006Page 2 SQL Normalization Database Design 1 st Normal Form 1 st Normal Form 2 nd Normal Form.
1 Class Agenda (11/07 and 11/12)  Review HW #8 answers  Present normalization process Enhance conceptual knowledge of database design. Improve practical.
Functional Dependence An attribute A is functionally dependent on attribute(s) B if: given a value b for B there is one and only one corresponding value.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Schema Refinement and Normal Forms 20131CS3754 Class Notes #7, John Shieh.
FEN Quality checking table design: Design Guidelines Normalisation Table Design Is this OK?
Customer Order Order Number Date Cust ID Last Name First Name State Amount Tax Rate Product 1 ID Product 1 Description Product 1 Quantity Product 2 ID.
IE 423 – Design of Decision Support Systems Data modeling and database development.
NORMALIZATION. What is Normalization  The process of effectively organizing data in a database  Two goals  To eliminate redundant data  Ensure data.
Functional dependencies and normalization 1 Normalization Functional dependencies NF + BCNF.
IE 423 – Design of Decision Support Systems Data modeling and database development.
CS 3630 Database Design and Implementation. 2 Mathematical Relation A mathematical relation is a subset of a Cartesian Product. A1  A2  A3  …  An.
FEN Introduction to the database field: The development process Seminar: Introduction to relational databases Development process: Analyse.
Normalization ACSC 425 Database Management Systems.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Standards and Conventions
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT395, Week 4 Database Design and ER Modeling This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
INF1343, Week 6 Implementing a Database with SQL This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
CCT395, Week 11 Review, Permissions Physical Storage and Performance This presentation is licensed under Creative Commons Attribution License, v. 3.0.
CCT395, Week 6 Implementing a Database with SQL and a Case Study Exercise This presentation is licensed under Creative Commons Attribution License, v.
Database Design and Implementation
CCT395, Week 2 Introduction to SQL Yuri Takhteyev September 15, 2010
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Week 4 Database Design and ER Modeling This presentation is licensed under Creative Commons Attribution License, v To view a copy of this.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
CCT395, Week 5 Translating ER into Relations; Normalization This presentation is licensed under Creative Commons Attribution License, v To view a.
INF1343, Winter 2011 Data Modeling and Database Design Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
Logical Design & the Relational Model
Database Design and Implementation
Database, tables and normal forms
University of Milano Bicocca Carlo Batini Course on Data Base Design
Special Topics in CCIT: Databases
CCT1343, Week 2 Single-Table SQL Yuri Takhteyev University of Toronto
A brief summary of database normalization
Database Management Systems (CS 564)
Chapter 5: Logical Database Design and the Relational Model
COMP 430 Intro. to Database Systems
Lecture 4 Database design IV Normal Forms: Summary.
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Database Design and Implementation
Example Question–Is this relation Well Structured? Student
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Translation of ER-diagram into Relational Schema
Entity Relationship Diagram
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Relational Database.
Normalization Murali Mani.
SQL data definition using Oracle
Database solutions The process of normalization Marzena Nowakowska Faculty of Management and Computer Modelling Kielce University of Technology rooms:
Sampath Jayarathna Cal Poly Pomona
Database Normalization.
Functional Dependencies and Normalization
Functional Dependencies and Normalization
Presentation transcript:

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?