Converting ER-Diagrams to Table Definitions

Slides:



Advertisements
Similar presentations
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Advertisements

Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Designing Tables for an Oracle Database System Database Course, Fall 2003.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Final Exam Revision 5 Prof. Sin-Min Lee Department of Computer Science.
Methodology Logical Database Design for the Relational Model
1 Designing Tables for an Oracle Database System Database Course, Fall 2005.
1 Database Course. 2 General Information TAs: –Gideon Rothschild, office hours: Sun 16:00- 17:00 at Ross 109 –Aron Matskin, office hours: TBA Course Homepage:
Mapping of N:M Relationships PERSON-ID TITLE PERSON-NAME DATE-OF-BIRTH PERSON-ID PROJECT-ID HOURS-SPENT PROJECT-ID END-DATE START-DATE E-R Diagram PERSON.
1 Database Course. 2 General Information TAs: –Jonathan Mamou –Gideon Rothschild Course Moderated Newsgroup: local.course.db.ta.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
1 Designing Tables for an Oracle Database System Database Course, Fall 2004.
LOGICAL DATABASE DESIGN
Using ER/Studio.
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
1 Translating E/R Diagrams into Relational Schemas.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
1 Chapter 17 Methodology - Local Logical Database Design.
1 CS 430 Database Theory Winter 2005 Lecture 15: How to Convert an ER Model to Relations.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
1 Database Course. 2 General Information TAs: –Sara Cohen –Jonathan Mamou Course Moderated Newsgroup: local.course.db.ta Students.
COP-5725 Practice Exercises
ER & Relational: Digging Deeper R &G - Chapters 2 & 3.
CS3431: C-Term Translating ER Schema to Relational Model Instructor: Mohamed Eltabakh
Modeling: Entity-Relationship Diagrams
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Chapter 71 The Relational Data Model, Relational Constraints & The Relational Algebra.
1 What is a Database Management System? Lecture slides by Dr. Sara Cohen Spring 2008.
CCT396, Fall 2011 Database Design and Implementation Yuri Takhteyev University of Toronto This presentation is licensed under Creative Commons Attribution.
COP Introduction to Database Structures
Translating ER Schema to Relational Model
Data Modeling Using the ERD
CSIS 115 Database Design and Applications for Business
Logical DB Design: ER to Relational
Database Constraints Ashima Wadhwa.
Chapter 5 Database Design
Methodology Logical Database Design for the Relational Model
EER Model – Chapter
Database Keys and Constraints
Entity-Relationship Model
Chapter 2: Entity-Relationship Model
Chapter Design Methodology Pearson Education © 2009.
Entity-Relationship Model
Designing Tables for a Database System
From ER to Relational Model
Relational Database.
Databases: An Introduction
CS4222 Principles of Database System
Module 8 – Database Design Using the E-R Model
376a. Database Design Dept. of Computer Science Vassar College
Designing Tables for a postgreSQL Database System
SQL data definition using Oracle
Translation of ER-diagram into Relational Schema
Data Modeling with Entity Relationship Diagrams (Cont.)
Session 2 Welcome: The seventh learning sequence
Chapter 5 Advanced Data Modeling
Database Design: Relational Model
Day 2 - Basic Database Backbone
Mapping an ERD to a Relational Database
Database EER.
SQL – Constraints & Triggers
Mapping an ERD to a Relational Database
Including Constraints
Presentation transcript:

Converting ER-Diagrams to Table Definitions

General Principals When converting ER diagrams to Relations, we should try to: Reduce duplicate information Constrain as tightly as possible Note: Some scenarios can be represented in different ways. Sometimes we will not be able to fully represent constraints, or will be forced to represent information more than once.

Relations vs. Tables We show how to translate ER-Diagrams to table definitions Sometimes, people translate ER-Diagrams to relation definition, which is more abstract than table definitions. e.g., Employee(SSN, Fname, Lname, Gender, Salary, Dept); table definitions contain, in addition, constraints and datatypes

Simple entity translation birthday id Actor name address General Rule: Create a table with the name of the Entity. There is a column for each attribute The key in the diagram is the primary key of the table

Student id name address name address student id

Simple entity translation birthday id Actor name address Relation: Actor (id, name, birthday, address) create table Actor(id varchar2(20) primary key, name varchar2(40), birthday date, address varchar2(100));

Translating Entities with Relationships (without constraints) birthday title id Actor Film Acted In year name salary address type Create tables for the entities as before Create a table with the name of the relationship Relationship table attributes: its own attributes (salary) + all keys of the relating entities (title, id). Q: What is the primary key of the table? A: A composite of both entity keys Q: What foreign keys are needed? A: From the relationship table to the entities

How would we represent this diagram in tables? תאריך שם קורס שם מס' זהות סטודנט נבחן קורס מס' קורס בחינה מס' בחינה

Answer קורס בחינה סטודנט נבחן מס' קורס שם קורס מס' בחינה מס' זהות שם תאריך

Translating Recursive Relationships (without constraints) manager id Employee Manages worker name address Relation: Manages (Wid, Mid) What would be the table definition? create table Manages( Eid varchar2(20), Mid varchar2(20), Foreign key Eid references Employee(id), Foreign key Mid references Employee(id), Primary key(Eid, Mid)); If we want to make sure an employee is not his own manager we can express it with Check

Translating relationships (key constraints): Option 1 id Director Film Directed title name salary year Option 1: Same as without key constraints (3 tables), except that the relationship primary key is…? title.

Translating relationships (key constraints): Option 1 id Director Film Directed title name salary year create table Directed( id varchar2(20), title varchar2(40), salary integer, primary key (title), foreign key id references Director, foreign key title references Film);

Translating relationships (key constraints): Option 2 id Director Film Directed title name salary year Option 2: Do not create a table for the relationship Add information columns that would have been in the relationship's table to the table of the entity with the key constraint

Translating relationships (key constraints): Option 2 id Director Film Directed title name salary year create table Film( title varchar2(40), year integer, primary key (title), id varchar2(20), salary integer, foreign key(id) references Director); Why couldn’t we do this when there were no constraints?

Translating relationships (participation constraints) id Director Film Directed title name salary year General Rule: If both participation and key constraint exist, use Option 2 from before (only 2 tables), and: Add the not null constraint on the referncing attribute to ensure that there will always be values for the key of the other entity

Translating relationships (participation constraints) id Director Film Directed title name salary year create table Film( title varchar2(40), year integer, id varchar2(20), salary integer, foreign key (id) references Director, primary key (title)); Where should we add NOT NULL?

Translating relationships (participation constraints) id Actor Film Acted In title name salary year How would we translate this?

Translating Weak Entity Sets A regular table for Organization, and.. phone number name create table award( name varchar2(40), year integer, money number(6,2), o_name varchar2(40), primary key(name, year, o_name), foreign key (o_name) references Organization(name) on delete cascade ) Organization Gives Award money year name

Translating ISA: Option 1 address Translating ISA: Option 1 id Movie Person name ISA picture Actor Director create table MoviePerson( ... ) create table Actor(id varchar2(20), picture bfile, primary key(id), foreign key (id) references MoviePerson)) create table Director(...)

Translating ISA: Option 2 address Translating ISA: Option 2 id Movie Person name ISA picture Actor Director No table for MoviePerson! create table Actor(id varchar2(20), address varchar2(100), name varchar2(20), picture blob, primary key(id)); create table Director(...)

Which Option To Choose? We use option 2 only if the lower-level entity sets (Actor and Director): Cover the high-level entity set Are disjoint

Translating Aggregation phone number Oname Translating Aggregation Organization picture Director ID Gives salary Acted In Won year Award Film Broad- casted year name title type Won(title, year, name, Oname, Broadcasted);

Summary Tables Primary key Remarks Simple Entity Single table The entity key a column for each attr. Simple Relationship 3 (2 entities +relationship) For the relation: Both entity keys Foreign keys from rel. Table Key constraint 3 as before or 2 (one for each entity) Key of constrained ent. Foreign key from constr. Entity Key and Participation constr. 2 Regular Constrained entity has a non-null f. key

Tables Primary key Remarks Weak Entity 2: parent and weak entities Weak: its own and parent keys Foreign keys from weak ent. ISA: covers and disjoint 2: only child entities Parent key ISA: otherwise 3: parent and child entities Foreign keys from child ent. Aggregation 3: 2 aggregates and relationship For relationship: keys of both aggregates Foreign keys from relationship table