Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter 3 Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 E/R Relation Model (Example) since name dname ssn Employees Hourly_Emps (0,*) (1,1) Manages (0,*) hours_worked hourly_wages did lot Works_In budget Departments (0,*) contractid Contract_Emps Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions since 2 Logical DB Design: ER to Relational 1. Entity Types to Tables. ssn name Employees lot CREATE TABLE Employees (ssn CHAR(9), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 3 2. Relationship Types to Tables • 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. 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) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 4 Translating ER Diagrams with Key Constraints • 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 Manages( ssn CHAR(9), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES 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) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 5 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 ssn lot Employees (0,*) Parent cost Policy Policy Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions dname (1,1) age Dependents Dependents 6 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) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 7 name ssn lot Review: ISA Hierarchies Employees hours_worked •As hourly_wages 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 contractid 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) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 8 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. Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 9 Dr. Eick’s Default Mapping E/R Relational Data Model 1. 2. 3. 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. 10 Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions ssn name occurred (0,*) husband Male wife (0,*) Person Female (1,1) Wedding (0,*) (0,*) Is-insured Company location Con# amount to from E/R Diagram to be mapped name Mapping of the Multi-Wedding E/R Diagram to a Relational Schema Person(ssn,name) Male_Person(ssn) Company(name,location) 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) Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 12 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! Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 13 Sal name city NFL E/R Problem empl. Manager isa (1,3) Team isa (24,99) Home Visit (0,*) (0,*) contr (0,1) play Player (0,*) weight Sal played-in. score (1,1) Scoring: 1. Play relationship a Set: 3 2. Person/Player/Manager: 3 Game 3. Weak Game Entity: 3 4. Played-in: 2 5. Can Only Play once on a day: 1 6. Contract: 3 7. Salary, score, min attribute: 3 ssn name (0,4) min (22,*) Date Person birthd height pos Using Dr. Eick’s E/RRel.-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) Sal name city empl. (0,4) Manager isa (1,3) Team (24,99) Home Visit (0,*) (0,*) contr (0,1) play (0,*) Date isa Player (0,*) weight Sal Day ssn name Person birthd height pos played-in. score (1,1) (22,*) Game Another different Solution! min Scoring: 1. Play relationship a Set: 3 2. Person/Player/Manager: 3 3. Weak Game Entity: 3 4. Played-in: 2 5. Can Only Play once on a day: 1 6. Contract: 3 7. Salary, score, min attribute: 3
© Copyright 2024 Paperzz