Presentation is loading. Please wait.

Presentation is loading. Please wait.

ER-to-Relational Mapping

Similar presentations


Presentation on theme: "ER-to-Relational Mapping"— Presentation transcript:

1 ER-to-Relational Mapping
Database Management Systems, 3rd ed., Ramakrishnan and Gehrke, Chapter 3

2 Regular Entity Set LName FName SSN MidInit Sex Employees Address
Create relation for the entity set; include all attributes. Choose one of the key attributes as the PK. Employees Address Salary Employee (SSN, FName, MidInit, LName, Sex, Address, Salary)

3 Relationship Set – No Key or Participation Constraints
Name Since DName Create relation for relationship set. Include as FKs the PKs of each participating entity set. Include descriptive attributes of relationship set. Relation’s PK is collection of PK attributes from participating entity sets. SSN Lot Budget DID WorksIn2 Employees Departments Address Locations Create relations for the entity sets: Employees (SSN, Name, Lot) Departments (DID, DName, Budget) Locations (Address, Capacity) and a relation for the relationship set: WorksIn2 (SSN, DID, Address, Since) SSN is FK that references Employees. DID is FK that references Departments. Address is FK that references Locations. Capacity Figure 3.10

4 Roles supervisor Employees ReportsTo SSN Name Lot To represent the ReportsTo relationship set, create meaningful attribute names for SSN to represent the two different roles played by Employees. subordinate Create relation for the entity set: Employees (SSN, Name, Lot) and relation for the relationship set: ReportsTo (SupervisorSSN, SubordinateSSN) SupervisorSSN is FK that references SSN in Employees. SubordinateSSN is FK that references SSN in Employees. Figure 3.11

5 Relationship Set – Key Constraint Approach 1
Suppose relationship set has n participating entity sets of which m are linked via arrows. The key for any one of those m entity sets is a candidate key for relation representing relationship set. Name Since DName SSN Lot Budget DID Manages Employees Departments Figure 3.12 Create relations for the entity sets: Employees (SSN, Name, Lot) Departments (DID, DName, DBudget) and a relation for the relationship set: Manages (SSN, DID, Since) SSN is FK that references Employees. DID is FK that references Departments. Why not have (SSN, DID) as the PK for Manages?

6 Relationship Set – Key Constraint Approach 2
Do not define separate relation for relationship set. Relationship is represented in relation corresponding to entity set with the key. Name Since DName SSN Lot Budget DID Manages Employees Departments Figure 3.12 Create relations for the entity sets. Add SSN to Departments to represent relationship set; also include the relationship’s descriptive attribute. Employees (SSN, Name, Lot) Departments (DID, DName, DBudget, SSN, Since) SSN is FK that references Employees. Note: Book renamed the Departments relation DeptMgr.

7 Relationship Set – Participation Constraint
Name Since DName SSN Lot Budget DID Manages Employees Departments Each department has exactly one manager: Participation constraint: Each department must have at least 1 manager. Key constraint: Each department may have at most 1 manager. WorksIn Figure 3.13 Since

8 Participation Constraint (continued)
Note: We’re using the 2nd approach for representing the key constraint. Why would the 1st approach not work in this case? Employees Manages SSN Name Lot DID DName Budget Since Departments WorksIn Employees (SSN, Name, Lot) DeptMgr (DID, DName, Budget, SSN, Since) SSN is FK that references Employees; cannot be null. WorksIn (SSN, DID, Since) SSN is FK that references Employees; every SSN in Employees must appear in WorksIn. DID is FK that references DeptMgr; every DID in DeptMgr must appear in WorksIn. Figure 3.13

9 Weak Entity Set Name Create relation for weak entity set. Include as FKs the PKs of the owner entity set. PK for new relation is owner’s PK and weak entity set’s partial key. Use 2nd approach given earlier to represent 1:N relationship set between owner and weak entity set. Specify “on delete cascade” to indicate existence dependence. Cost SSN Lot PName Age Employees Policy Dependents Figure 3.14 Define relation for Employees: Employees (SSN, Name, Lot) and relation for Dependents entity set plus Policy relationship set: DepPolicy (SSN, PName, Age, Cost) SSN is FK that references Employees; on delete cascade.

10 Class Hierarchy Approach 1
Name Lot SSN Employee Map each entity set to a relation. For each subclass entity set, include the PK of the superclass. The subclass’s PK is the same as the superclass’s PK and is a FK; specify “on delete cascade”. ISA HoursWorked HourlyEmps ContractEmps Employee (SSN, Name, Lot) HourlyEmps (SSN, HourlyWages,HoursWorked) SSN is FK that references Employee; on delete cascade. ContractEmps (SSN, ContractID) HourlyWages ContractID Figure 3.15

11 Class Hierarchy Approach 2
Name Lot SSN Employee Map only the subclass entity sets to relations. For each subclass entity set, include the PK of the superclass. ISA HoursWorked HourlyEmps ContractEmps HourlyWages ContractID HourlyEmps (SSN, Name, Lot, HourlyWages, HoursWorked) ContractEmps (SSN, Name, Lot, ContractID) Figure 3.15

12 Aggregation Name Lot SSN Employees Employees (SSN, Name, Lot) Until
Projects SSN Name Lot Monitors Until Sponsors Departments Since PID StartedOn PBudget DID DName Budget Employees (SSN, Name, Lot) Projects (PID. StartedOn, PBudget) Departments (DID, DName, Budget) Sponsors (PID, DID, Since) Every PID in Projects must appear in Sponsors. Monitors (SSN, DID, PID, Until) Note that the relation representing the Monitors relationship set includes the key attributes of the Sponsors relation and the descriptive attributes of the Monitors relationship set. Figure 3.16


Download ppt "ER-to-Relational Mapping"

Similar presentations


Ads by Google