From ER to Relational Model Book Chapter 3
From ER to Relational Model Book Chapter 3 (part 2 )
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Logical DB Design: ER to Relational v Translate Entity sets to tables: ssn name lot CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Employees 2
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translate Relationship Sets to Tables since name ssn lot Employees dname did Works_In budget Departments 3
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translate Relationship Sets to Tables v Attributes of relation include: § Keys for each participating entity set § All descriptive attributes. since name ssn CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE) lot Employees dname did Works_In budget Departments 4
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translate Relationship Sets to Tables § Foreign Keys: • § Keys for each participating entity set (? ) 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) Keys: • This set of attributes forms superkey for relation (? ) since name ssn lot Employees dname did Works_In budget Departments 5
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Key Constraints Translation to relational model? 1 -to-1 1 -to Many-to-1 Many-to-Many 6
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Review: Key Constraint v v Each dept has at most one manager, according to key constraint on Manages. Each department appears only once in relationship since name ssn dname lot Employees did Manages budget Departments Translation to relational model? 1 -to Many 7
Translate Key Constraint : Approach I. § § Separate tables for Employees and Departments. Note that did is key now! Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational 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) 8
Translate Key Constraint: Approach II. Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational TABLE Employee (…) § Combine Manages and Departments into one relation. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, ………………. ) § Each department has a unique manager since name ssn dname lot Employees did Manages budget Departments 9
Translate Key Constraint: Approach II. Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational TABLE Employee (…) § Combine Manages and Departments into one relation. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) § Each department has a unique manager, if any. since name ssn dname lot Employees did Manages budget Departments 10
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Other Cases of Key Constraints 1 -to-1 1 -to Many-to-1 Many-to-Many Translation to relational model? 12
Review: Participation Constraints v Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational The “must have” constraint (not-null value)? since name ssn did lot Employees dname Manages budget Departments Works_In since 13
Participation + Key Constraint. v Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational 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!) since name ssn did lot Employees dname Manages budget Departments 14
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v Approach I. • every did value in Department appears in a tuple of Managers • corresponding tuple must have a non-null ssn values 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, • Does this capture with “not-null” work, or not? 15
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v 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 ! 16
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v 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 ? 17
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v 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? 18
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v 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) 19
More on Participation Constraints v Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational What about other cases of “must have” constraint ? since name ssn did lot Employees dname Manages budget Departments Works_In since What if we want to capture participation for many-tomany relationships? v v Anwer: We need to use CHECK constraints. 20
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Participation Constraints in SQL v What if we want to capture participation for three-way relationships? v Anwer: We need to use CHECK constraints. v Observation : Little else can be enforced without resorting to the use of check constraints ! 22
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Review: Weak Entities v 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. name ssn lot Employees cost Policy pname age Dependents 23
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translating Weak Entity Sets v 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? v Then all owned weak entities must also be deleted ! v 24
Ø 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) 25
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Review: ISA Hierarchies v Attributes are inherited name ssn lot Employees hourly_wages hours_worked ISA Hourly_Emps contractid Contract_Emps 26
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translating ISA Hierarchies v What are possible alternatives of mapping IS-A to the relational model ? 27
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translating ISA Hierarchies v 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. 28
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Translating ISA Hierarchies Alternative: Two tables v Namely: Hourly_Emps and Contract_Emps v Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Contract_Emps (ssn, name, lot, contractid) Keys? Foreign keys? Delete semantics? 29
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Pros/Cons : Translating ISA Hierarchies v Alternative : Three Relations Employees ( ssn, name, lot) Hourly_Emps (ssn, hourly_wages, hours_worked); Contract_Emps (ssn, contractid); v Alternative: Two tables Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Contract_Emps (ssn, name, lot, contractid) v Pros/cons for three relations: + Queries involving all employees easy - Queries involving just Hourly_Emps may require a Join. v Pros/Cons for two relations: - Each employee must be in one of these two subclasses. Avoid joins for subtable queries 30
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational ISA Hierarchy Translation? name ssn lot Employees hourly_wages hours_worked ISA Hourly_Emps v v 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) 31
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Another Example Requirements: v v 1. A policy cannot be owned by two or more employees. (Key Constraints) 2. Every policy must be owned by some employee (Total participation) ssn name pname lot Dependents Employees Purchaser Beneficiary Policies policyid v age cost 3. Dependents is a weak entity (Weak Entity) 33
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Example of Mapping to Tables v Key constraints allow us to combine (Purchaser with Policies) and (Beneficiary with Dependents. ) v Participation constraints lead to NOT NULL constraints. v 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) 34
Ø Relational Data Model Ø Relational Query Language Ø Integrity Constraints Ø ER to Relational Summary ER Modeling : graphical design view v Relational Model: A tabular representation of data. v Rules to translate ER to relational model exist v v Later : Yet more design optimizations can and should be applied, after initial relational design has been derived. 35
- Slides: 32