Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev

Slides:



Advertisements
Similar presentations
Designing tables from a data model (Chapter 6) One base table for each entity.
Advertisements

Database Lecture Notes Mapping ER Diagrams to Tables 2 Dr. Meg Murray
1 Class Number – CS 304 Class Name - DBMS Instructor – Sanjay Madria Instructor – Sanjay Madria Lesson Title – EER Model –21th June.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke Database Processing Chapter 6 Transforming Data.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 COS 346 Day 10.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming ER Models into Database.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming ER Models into Database.
Transforming Data Models into Database Designs
Entity Relationships. Relationships Relationships exist between entities The type of relationship is entirely dependent on the business rules The business.
Mapping ERM to relational database
Chapter Six Professor Adams’ Slides. Note that entities are shadowed, tables are not. Note that entities have no physical existence (blueprint) Note.
University of Toronto Department of Computer Science © Steve Easterbrook. This presentation is available free for non-commercial use with attribution.
3 & 4 1 Chapters 3 and 4 Drawing ERDs October 16, 2006 Week 3.
1 A Demo of Logical Database Design. 2 Aim of the demo To develop an understanding of the logical view of data and the importance of the relational model.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
3 & 4 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Keys Consists of one or more attributes that determine other.
Quiz questions. 1 A data structure that is made up of fields and records? Table.
1 ER Modeling BUAD/American University Mapping ER modeling to Relationships.
Entity Relationship Diagram (ERD). Objectives Define terms related to entity relationship modeling, including entity, entity instance, attribute, relationship.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 6-1 David M. Kroenke’s Chapter Six: Transforming Data Models into Database.
Database Designsemester Slide 1 Database Design Lecture 7 Entity-relationship modeling Text , 7.1.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
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, Winter 2011 Data Modeling and Database Design Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
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
INF1343, Winter 2012 Data Modeling and Database Design Yuri Takhteyev Faculty of Information University of Toronto This presentation is licensed under.
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.
CompSci 280 S Introduction to Software Development
CSIS 115 Database Design and Applications for Business
Logical Database Design and the Rational Model
Database Design and Implementation
Translating ER into Relations; Normalization
Review Databases and Objects
Chapter 4 Logical Database Design and the Relational Model
Chapter 4: Logical Database Design and the Relational Model
Chapter 4: Part B Logical Database Design and the Relational Model
Special Topics in CCIT: Databases
Databases Chapter 16.
Database Keys and Constraints
Chapter 5: Logical Database Design and the Relational Model
Entity-Relationship Model
CSIS 115 Database Design and Applications for Business
Tables and Their Characteristics
Database Design – Lecture 4
CSIS 115 Database Design and Applications for Business
Data Modeling and Database Design
Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev
Database Design and Implementation
Developing Data Models – Conversion Rules
Database Processing: David M. Kroenke’s Chapter Six:
Session 2 Welcome: The seventh learning sequence
CHAPTER 4: LOGICAL DATABASE DESIGN AND THE RELATIONAL MODEL
Database Design Chapters 17 and 18.
Chapter 7: Entity-Relationship Model
DBMS ER-Relational Mapping
Mapping an ERD to a Relational Database
Database Processing: David M. Kroenke’s Chapter Six:
MIS2502: Data Analytics Relational Data Modeling 2
Database Processing: David M. Kroenke’s Chapter Six:
Presentation transcript:

Data Modeling and Database Design INF1343, Winter 2012 Yuri Takhteyev Faculty of Information University of Toronto 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/.

Week 4 Converting an ER Design into a Relational Form

Drawing Software Options for software: OpenOffice Draw Free / open source Available in the lab You can get “Crow’s Foot” templates at http://takhteyev.org/courses/12W/inf1343/Crows_Feet_Arrows.odg Alternatively, do UML notation (“n..m”) by hand Microsoft Visio Your favorite software

Hierarchical (1:M) vs. Not (Quite) Hierarchical (M:M) Common Patterns Hierarchical (1:M) vs. Not (Quite) Hierarchical (M:M)

A contains B 1:M building ⤙ room cd ⤙ track, book ⤙ chapter car ⤙ part province ⤙ riding neighborhood ⤙ restaurant session ⤙ prediction invoice ⤙ billable item

M:M course ⤚⤙ student list ⤙ restaurant dish ⤚⤙ ingredient A contains B M:M course ⤚⤙ student list ⤙ restaurant dish ⤚⤙ ingredient

A “owns” B 1:M mother ⤙ child user ⤙ comment restaurant ⤙ rating comment ⤙ rating comment ⤙ reply customer ⤙ payment customer ⤙ invoice customer ⤙ session

M:M investor ⤚⤙ company instructor ⤚⤙ course A “owns” B M:M investor ⤚⤙ company instructor ⤚⤙ course

Belonging to Different Entities 1:M user ⤙ comment ⤚ restaurant customer ⤙ session ⤚ f. teller

B “instantiates” A 1:M species ⤙ pet model ⤙ vehicle book ⤙ edition course ⤙ course_instance M:M employee ⤚⤙ role (“CCT395” vs “CCT395 in Fall 2011”)

Eatr restaurant neighborhood comment list_membership user list ------------------------------------------------ restaurant_id* name price_range neighborhood ------------------------------------------------ neighborhood_id* name comment ------------------------------------------------ comment_id* date_posted comment_text list_membership ----------------------------------------------- user ------------------------------------------------ user_id* username real_name list ------------------------------------------------ list_id* name

Mapping ER to Rel. 0. Break up M:M entities 1. Each entity becomes a table (attributes become fields / columns) 2. What about the relationships?

Linking the Tables species Human humanoid 1.7 Hutt gastropod 3.5 species_type persona gastropod humanoid 2 Jabba Hutt Obiwan Kenobi Human

Keys A (Candidate) Key a set of fields that can uniquely identify a row in a table The Primary Key (PK) a candidate key that we decided to use as the primary method for identifying in a particular table

Examples of Keys student name? student id? Utorid email? date of birth? restaurant name? city? Address? comment text? time? user?

Natural vs Surrogate A “Natural” Key based on an existing attribute e.g.: email, existing codes ☺ easy to remember ☹ may have to change A “Surrogate” Key an arbitrary identifier ☹ hard to remember ☺ never have to change Usually a better option

Does Every Table Need a PK? Strictly speaking, no. But it often helps, and almost never hurts. So, as a rule of thumb: add a surrogate PK to each table, except those representing associative entities.

Choosing PKs restaurant: restaurant_id integer neighborhood: neighborhood_id integer comment: comment_id integer user: user_id integer

CREATE TABLE create table restaurant ( restaurant_id integer, name varchar(100), price_range integer );

NOT NULL create table restaurant ( restaurant_id integer not null, name varchar(100) not null, price_range integer );

PRIMARY KEY create table restaurant ( restaurant_id integer not null, name varchar(100) not null, price_range integer, primary key (restaurant_id) );

AUTO_INCREMENT create table restaurant ( restaurant_id integer not null auto_increment, name varchar(100) not null, price_range integer, primary key (restaurant_id) );

Foreign Key A PK of another table An attribute that contains a primary key of another table, with a constraint that the corresponding row exists in the other table. (A FK is normally not itself a “key”.)

Implementing 1:M Every table representing an entity on the “M” side of a relationship gets a FK pointing to the PK of the entity on the “1” side of that relationship.

Implementing 1:M restaurant neighborhood comment list_membership user ------------------------------------------------ restaurant_id* name price_range neighborhood ------------------------------------------------ neighborhood_id* name comment ------------------------------------------------ comment_id* date_posted comment_text list_membership ----------------------------------------------- user ------------------------------------------------ user_id* username real_name list ------------------------------------------------ list_id* name

Implementing 1:M restaurant neighborhood comment list_membership user ------------------------------------------------ restaurant_id* name price_range neighborhood_id neighborhood ------------------------------------------------ neighborhood_id* name comment ------------------------------------------------ comment_id* date_posted comment_text restaurant_id user_id parent_id list_membership ----------------------------------------------- restaurant_id list_id user ------------------------------------------------ user_id* username real_name list ------------------------------------------------ list_id* name user_id

Implementing a FK create table restaurant ( restaurant_id integer not null auto_increment, name varchar(100) not null, price_range integer, neighborhood_id integer, primary key (restaurant_id), foreign key (neighborhood_id) references neighborhood(neighborhood_id) );

ON DELETE alternatives: “set null”, “restrict”. create table restaurant ( restaurant_id integer ... neighborhood_id integer, primary key (restaurant_id), foreign key (neighborhood_id) references neighborhood(neighborhood_id) on delete cascade ); alternatives: “set null”, “restrict”.

Associative Entities create table list_membership ( list_id integer not null, restaurant_id integer not null, primary key (list_id, restaurant_id), foreign key (list_id) references list(list_id), foreign key (restaurant_id) references restaurant(restaurant_id), );

Recursion create table comment ( comment_id integer not null, ... parent_id integer, primary key (comment_id), foreign key (parent_id) references comment(comment_id) );

Questions?

Optional / Mandatory On the 1 side: Use “not null” on the FK. On the M side: Can’t be mandatory. (It will have to be optional.)

1:1 Relationships Option 1: Use the same table. Option 2: Use a single-attribute FK as the PK in one of the tables.

Multivalued Attributes customer: name phone number(s) email address(es) restaurant: name address tag(s)

Multivalued Attributes Problem: Multivalued attributes may be ok in ER, but definitely not in a relational database. Solution: Treat multivalued attributes as simple entities.

Why are those 1:M and not M:M? customer ------------------------------------------------ name customer_email ------------------------------------------------ email customer_tel ------------------------------------------------ phone_number Why are those 1:M and not M:M?

customer customer_email customer_tel customer_id (PK) email name ------------------------------------------------ customer_id (PK) name customer_email ------------------------------------------------ email customer_id (FK) customer_tel ------------------------------------------------ phone_number customer_id (FK)

create table customer_email ( email varchar(100), customer_id integer not null, primary key (customer_id, email), foreign key (customer_id) references customer(list_id) ); Are we missing anything?

create table customer_email ( email varchar(100), customer_id integer not null, position integer, primary key (customer_id, email), foreign key (customer_id) references customer(list_id) );