Mapping E/R Diagrams to Relational Database Schemas

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

R ELATIONAL M ODEL TO SQL Data Model. 22 C ONCEPTUAL D ESIGN : ER TO R ELATIONAL TO SQL How to represent Entity sets, Relationship sets, Attributes, Key.
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
The Entity-Relationship Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Book Chapter 3 (part 2 ) From ER to Relational Model.
1 541: Database Systems S. Muthu Muthukrishnan. 2 Overview of Database Design  Conceptual design: (ER Model is used at this stage.)  What are the entities.
The Entity-Relationship (ER) Model
The Entity-Relationship Model
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Conceptual Design and The Entity-Relationship Model
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Relational Algebra, R. Ramakrishnan and J. Gehrke Ungraded Homework P1 Reminder : Reserves(sid,bid,day) a. Create a table Together(sid1,sid2,day)
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 176 Database Systems I The Entity-Relationship Model.
1 The Entity-Relationship Model Chapter 2. 2 Overview of Database Design  Conceptual design: (ER Model is used at this stage.) –What are the entities.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Modeling Your Data Chapter 2. Overview of Database Design Conceptual design: –What are the entities and relationships in the enterprise? – What information.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
Conceptual Design Using the Entity-Relationship (ER) Model
The Entity-Relationship (ER) Model CS541 Computer Science Department Rutgers University.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
ER continued, and ER to Relational Mappings R&G Chapters 2, 3 Lecture 22.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Christoph F. Eick: Designing E/R Diagrams 1 Designing E/R Diagrams Updated: January
NFL E/R Design Problem Design an Entity-Relationship Diagram that models the following objects and relationships in the world of football (NFL): teams,
1 The Entity-Relationship Model Chapter 2. 2 Overview of Database Design  Conceptual design : (ER Model is used at this stage.)  What are the entities.
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 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 The Entity-Relationship Model Chapter 6. 2 Database Design Process  Requirement collection and analysis  DB requirements and functional requirements.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Christoph F. Eick: Designing E/R Diagrams 1 The Entity-Relationship Model Chapter 3+4.
The Relational Model1 ER-to-Relational Mapping and Views.
ICS 321 Fall 2009 The Relational Model (ii) Asst. Prof. Lipyeow Lim Information and Computer Science Department University of Hawaii at Manoa 9/10/20091Lipyeow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems,1 Conceptual Design Using the Entity-Relationship (ER) Model.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Entity-Relationship (ER) Model. Overview of db design Requirement analysis – Data to be stored – Applications to be built – Operations (most frequent)
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L2_ER Model 1 The Entity-Relationship Model (Chapter 2)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter.
Dr. Eick’s: Homework Solutions COSC Ungraded Homework P1 Reminder : Reserves(sid,bid,day) a. Create a table Together(sid1,sid2,day) that contains.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Logical DB Design: ER to Relational
The Entity-Relationship Model
ER-to-Relational Mapping
The Entity-Relationship Model
Instructor: Elke Rundensteiner
From ER to Relational Model
Solution Problem1 Exam0 ssn Sal empl. name name Person Team city contr
The Entity-Relationship Model
Presentation transcript:

Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter 3 The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter 2: The Entity-Relationship Model Chapter 3: The Relational Model Chapter 4 (Part A): Relational Algebra Chapter 4 (Part B): Relational Calculus Chapter 5: SQL: Queries, Programming, Triggers Chapter 6: Query-by-Example (QBE) Chapter 7: Storing Data: Disks and Files Chapter 8: File Organizations and Indexing Chapter 9: Tree-Structured Indexing Chapter 10: Hash-Based Indexing Chapter 11: External Sorting Chapter 12 (Part A): Evaluation of Relational Operators Chapter 12 (Part B): Evaluation of Relational Operators: Other Techniques Chapter 13: Introduction to Query Optimization Chapter 14: A Typical Relational Optimizer Chapter 15: Schema Refinement and Normal Forms Chapter 16 (Part A): Physical Database Design Chapter 16 (Part B): Database Tuning Chapter 17: Security Chapter 18: Transaction Management Overview Chapter 19: Concurrency Control Chapter 20: Crash Recovery Chapter 21: Parallel and Distributed Databases Chapter 22: Internet Databases Chapter 23: Decision Support Chapter 24: Data Mining Chapter 25: Object-Database Systems Chapter 26: Spatial Data Management Chapter 27: Deductive Databases Chapter 28: Additional Topics 1

E/R  Relation Model (Example) since since name name dname dname ssn lot did did budget budget (0,*) (1,1) Employees Manages Departments (0,*) hours_worked (0,*) Works_In   hourly_wages contractid since Hourly_Emps Contract_Emps

Logical DB Design: ER to Relational 1. Entity Types to Tables. Employees ssn name lot The slides for this text are organized into several modules. Each lecture contains about enough material for a 1.25 hour class period. (The time estimate is very approximate--it will vary with the instructor, and lectures also differ in length; so use this as a rough guideline.) This covers Lectures 1 and 2 (of 6) in Module (5). Module (1): Introduction (DBMS, Relational Model) Module (2): Storage and File Organizations (Disks, Buffering, Indexes) Module (3): Database Concepts (Relational Queries, DDL/ICs, Views and Security) Module (4): Relational Implementation (Query Evaluation, Optimization) Module (5): Database Design (ER Model, Normalization, Physical Design, Tuning) Module (6): Transaction Processing (Concurrency Control, Recovery) Module (7): Advanced Topics CREATE TABLE Employees (ssn CHAR(9), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) 3

2. Relationship Types to Tables CREATE TABLE Works_In( ssn CHAR(9), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes. 5

Translating ER Diagrams with Key Constraints CREATE TABLE Manages( ssn CHAR(9), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) Map relationship to a table: Note that did is the key now! Separate tables for Employees and Departments. Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, manager CHAR(9), since DATE, PRIMARY KEY (did), FOREIGN KEY (manager) REFERENCES Employees) 7

Review: Weak Entities A weak entity can be identified uniquely only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. name cost ssn dname lot age (0,*) (1,1) Policy Dependents Employees Policy Dependents Parent 10

Translating Weak Entity Types Weak entity set and identifying relationship set are translated into a single table --- it has a (1,1) cardinality constraint. CREATE TABLE Dep_Policy ( dname CHAR(20), age INTEGER, cost REAL, parent CHAR(9) NOT NULL, PRIMARY KEY (parent, dname), FOREIGN KEY (parent) REFERENCES Employees, ON DELETE CASCADE) 11

Review: ISA Hierarchies name name ssn lot Review: ISA Hierarchies Employees hours_worked   hourly_wages contractid As in C++, or other PLs, attributes are inherited. If we declare A ISA B, every A entity is also considered to be a B entity. Hourly_Emps Contract_Emps Overlap constraints: Can Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed) Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) 12

3. Translating ISA Hierarchies to Tables General approach: 3 relations: Employees, Hourly_Emps and Contract_Emps. Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must delete Hourly_Emps tuple if referenced Employees tuple is deleted). Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes. Alternative: Just Hourly_Emps and Contract_Emps. Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked. Each employee must be in one of these two subclasses. 13

Dr. Eick’s Default Mapping E/R  Relational Data Model For each entity type create a relation with the attributes associated with the entity type. Choose a primary key for the defined relation; if the entity type is weak, delay choosing primary keys until all identifying relationships are mapped. For each relationship type create a relation that contains the roles as well as the attributes of the relationship type. Define referential integrity constraints with respect to the mapped roles. Exception: If there is a (1,1) cardinality constraint do not generate a separate relation, but rather associate the relationship information with the relation of this participating entity type. For each sub-type create a relation that contains the attributes of the entity type as well as the primary key of the most general super class of this entity type (which also will be the primary key of the generated relation). Define referential integrity constraints with respect to the direct super class of the mapped entity type.

E/R Diagram to be mapped ssn name (0,*) Male  occurred husband Person  wife  Female (0,*) (1,1) Wedding Is-insured (0,*) (0,*) Company location Con# name amount to from E/R Diagram to be mapped

Mapping of the Multi-Wedding E/R Diagram to a Relational Schema Person(ssn,name) Company(name,location) Male_Person(ssn) Female_Person(ssn) Wedding(husband,wife,from,to) Is-Insured(hssn,wssn,from,company, amount, Con#) Correct Syntax: FOREIGN KEY (hssn,wssn,from) REFERENCES Wedding(husband,wife,from)

NFL E/R Design Problem Ungraded Homework Spring 2005 Design an Entity-Relationship Diagram that models the following objects and relationships in the world of football (NFL): teams, players, games, managers and contracts. Each (NFL-) team has a unique team name, and a city it plays in. Each person being part of the NFL-world has a unique ssn and a name. Additionally, for players their weight, height, position and birth dates are of importance. Players have a contract with at most one team and receive a salary for their services, and teams have at least 24 and at most 99 players under contract. Each team has one to three managers; managers can work for at most 4 teams and receive a salary for each of their employments. Players cannot be managers. A game involves a home-team and visiting-team; additionally, the day of the game, and the score of the game are of importance; teams play each other several times in a season (not on the same day!). Moreover, for each game played we like to know which players participated in the game and how many minutes they played.   Indicate the cardinalities for each relationship type; assign roles (role names) to each relationship if there are ambiguities! Use sub-types, if helpful to express constraints!

NFL E/R Problem ssn Sal empl. name name Person Team city contr Player (0,4) name name Manager isa (1,3) Person Team isa city (24,99) contr (0,1) Player birthd Home Visit NFL E/R Problem (0,*) (0,*) weight height pos Sal (0,*) play played-in. score min (1,1) (22,*) Scoring: Play relationship a Set: 3 Person/Player/Manager: 3 Weak Game Entity: 3 Played-in: 2 Can Only Play once on a day: 1 Contract: 3 Salary, score, min attribute: 3 Game Date

Using Dr. Eick’s E/RRel.-Default Mapping Employs(team, manager, salary) Manger(ssn) Team(name, city) Player(ssn, birthd, pos,…) Person(ssn, name) Game(home, visit, day, score) Played_in(home,visit, day, ssn, min)

Another different Solution! Sal empl. (0,4) ssn name name Manager isa (1,3) Person Team isa city (24,99) contr (0,1) Player birthd Home Visit (0,*) (0,*) weight height pos Sal (0,*) play played-in. Day (0,*) score min Date (1,1) (22,*) Game Scoring: Play relationship a Set: 3 Person/Player/Manager: 3 Weak Game Entity: 3 Played-in: 2 Can Only Play once on a day: 1 Contract: 3 Salary, score, min attribute: 3 Another different Solution!