Book Chapter 3 (part 2 ) From ER to Relational Model.

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.
The Entity-Relationship Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
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.
1 Key Constraints Consider Works_In: An employee can work in many departments; a dept can have many employees. In contrast, each dept has at most one manager,
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.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
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 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
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.
Conceptual Design and The Entity- Relationship Model CS 186 Spring 2006 Lectures 19 & 20 R &G - Chapter 2 A relationship, I think, is like a shark, you.
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.
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,
The Relational Model Chapter 3
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.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
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.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
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.
Mapping E/R Diagrams to Relational Database Schemas
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.
ER & Relational: Digging Deeper R &G - Chapters 2 & 3.
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.
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.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
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
From ER to Relational Model
The Relational Model Chapter 3
The Entity-Relationship Model
The Entity-Relationship Model
Presentation transcript:

Book Chapter 3 (part 2 ) From ER to Relational Model

2  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Logical DB Design: ER to Relational  Translate Entity sets to tables: CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Employees ssn name lot

3  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Relationship Sets to Tables lot dname budget did since name Works_In DepartmentsEmployees ssn

4  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Relationship Sets to Tables  Attributes of relation include:  Keys for each participating entity set  All descriptive attributes. CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE) lot dname budget did since name Works_In DepartmentsEmployees ssn

5  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Relationship Sets to Tables  Foreign Keys: Keys for each participating entity set (?)  Keys: This set of attributes forms superkey for relation (?) CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) lot dname budget did since name Works_In DepartmentsEmployees ssn

6  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Key Constraints Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1

7  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Key Constraint  Each dept has at most one manager, according to key constraint on Manages.  Each department appears only once in relationship Translation to relational model? 1-to Many dname budget did since lot name ssn Manages Employees Departments

8  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraint : Approach I.  Separate tables for Employees and Departments.  Note that did is key now! TABLE Dept(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)

9  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraint: Approach II.  Combine Manages and Departments into one relation.  Each department has a unique manager TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, ………………. ) dname budget did since lot name ssn Manages Employees Departments

10  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraint: Approach II.  Combine Manages and Departments into one relation.  Each department has a unique manager, if any. TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) dname budget did since lot name ssn Manages Employees Departments

11  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraints  Approach I  Separate tables for Employees and Departments.  Note that did is the key now!  Approach II  Combine Manages and Departments.  Each department has a unique manager TABLE Dept(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

12  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Other Cases of Key Constraints Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1

13  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Participation Constraints  The “must have” constraint (not-null value)? lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In

14  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation + Key Constraint.  Every department must have a manager ! Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!) lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn

15  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach I. every did value in Department appears in a tuple of Managers corresponding tuple must have a non-null ssn values Does this capture with “not-null” work, or not? TABLE Dept(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments,

16  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach I. every did value in Department appears in a tuple of Works_In the corresponding tuple must have a non-null ssn values TABLE Dept_mgr(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments, CHECK ( (SELECT Count(*) FROM Manages) = (SELECT Count(*) FROM Dept) )  Must utilize check constraints !

17  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach II. - capture participation constraints involving one entity set in a binary relationship using combined table. TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, SEMANTICS ??? ) Does this “non-null” approach now work ?

18  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach II. - capture participation constraints involving one entity set in a binary relationship using combined table. TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, SEMANTICS ??? ) What should happen if manager-employee is deleted?

19  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach II. - use the “on action” propagation constraint ! CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION)

20  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational More on Participation Constraints  What about other cases of “must have” constraint ? lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In  What if we want to capture participation for many- to-many relationships?  Anwer: We need to use CHECK constraints.

21  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  What if we want to capture participation for many-to- many relationships?  Anwer: We need to use CHECK constraints. lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In

22  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  What if we want to capture participation for three-way relationships?  Anwer: We need to use CHECK constraints.  Observation : Little else can be enforced without resorting to the use of check constraints !

23  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Weak Entities  A weak entity can be identified uniquely only by considering 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. lot name age pname Dependents Employees ssn Policy cost

24  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating Weak Entity Sets  Weak entity set and identifying relationship set are translated into a single table. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, --- (?), PRIMARY KEY (pname, ssn), --- (?) FOREIGN KEY (ssn) REFERENCES Employees, WHAT SEMANTICS HERE ??? )  What when the owner entity is deleted?  Then all owned weak entities must also be deleted !

25  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating Weak Entity Sets  When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11), PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

26  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: ISA Hierarchies Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked  Attributes are inherited

27  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating ISA Hierarchies  What are possible alternatives of mapping IS-A to the relational model ?

28  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating ISA Hierarchies  General approach:  3 relations: Employees, Hourly_Emps and Contract_Emps. Employees ( ssn, name, lot) Hourly_Emps (ssn, hourly_wages, hours_worked) ; Contract_Emps (ssn, contractid); Keys? Foreign keys? Delete semantics? - Delete Hourly_Emps tuple if referenced Employees tuple is deleted - how? - Put CASCADE semantics on foreign key.

29  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating ISA Hierarchies  Alternative: Two tables  Namely: Hourly_Emps and Contract_Emps Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Contract_Emps ( ssn, name, lot, contractid) Keys? Foreign keys? Delete semantics?

30  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Pros/Cons : Translating ISA Hierarchies  Alternative : Three Relations Employees ( ssn, name, lot) Hourly_Emps (ssn, hourly_wages, hours_worked) ; Contract_Emps (ssn, contractid);  Alternative: Two tables Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Contract_Emps ( ssn, name, lot, contractid)  Pros/cons for three relations: + Queries involving all employees easy - Queries involving just Hourly_Emps may require a Join.  Pros/Cons for two relations: - Each employee must be in one of these two subclasses. - Avoid joins for subtable queries

31  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational ISA Hierarchy Translation? Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked  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)

32  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating ISA Hierarchies  General approach:  3 relations: Employees, Hourly_Emps and Contract_Emps. Employees ( ssn, name, lot) Hourly_Emps (ssn, hourly_wages, hours_worked) ; Contract_Emps (ssn, contractid); delete Hourly_Emps tuple if referenced Employees tuple is deleted (how?) + Queries involving all employees easy - Queries involving just Hourly_Emps require a Join.  Alternative: Just Hourly_Emps and Contract_Emps. Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Hourly_Emps ( ssn, name, lot, contractid)  Each employee must be in one of these two subclasses.

33  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Another Example Requirements:  1. A policy cannot be owned by two or more employees. (Key Constraints)  2. Every policy must be owned by some employee (Total participation)  3. Dependents is a weak entity (Weak Entity) Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot

34  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Example of Mapping to Tables  Key constraints allow us to combine (Purchaser with Policies) and (Beneficiary with Dependents.)  Participation constraints lead to NOT NULL constraints.  Policies is a weak entity set: ON DELETE CASCADE CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees) CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)

35  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Summary  ER Modeling : graphical design view  Relational Model : A tabular representation of data.  Rules to translate ER to relational model exist  Later : Yet more design optimizations can and should be applied, after initial relational design has been derived.