Download presentation
Presentation is loading. Please wait.
Published byLoraine Rosalyn Freeman Modified over 10 years ago
1
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2
2
Database Management Systems, R. Ramakrishnan and J. Gehrke2 ER Model Basics v Entity: Real-world object distinguishable from other objects. An entity is described (in DB) using a set of attributes. v Entity Set : A collection of similar entities. E.g., all employees. – All entities in an entity set have the same set of attributes. – Each entity set has a key. – Each attribute has a domain. Employees ssn name lot
3
Database Management Systems, R. Ramakrishnan and J. Gehrke3 ER Model Basics (Contd.) v Relationship : Association among two or more entities. E.g., Attishoo works in Pharmacy department. v Relationship Set : Collection of similar relationships. – An n-ary relationship set R relates n entity sets E1... En; each relationship in R involves entities e1 E1,..., en En u Same entity set could participate in different relationship sets, or in different “roles” in same set. lot dname budget did since name Works_In DepartmentsEmployees ssn Reports_To lot name Employees subor- dinate super- visor ssn
4
Database Management Systems, R. Ramakrishnan and J. Gehrke4 Key Constraints v Consider Works_In: An employee can work in many departments; a dept can have many employees. v In contrast, each dept has at most one manager, according to the key constraint on Manages. Many-to-Many 1-to-11-to ManyMany-to-1 dname budgetdid since lot name ssn Manages Employees Departments
5
Database Management Systems, R. Ramakrishnan and J. Gehrke5 Participation Constraints v Does every department have a manager? – If so, this is a participation constraint : the participation of Departments in Manages is said to be total (vs. partial ). u 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 since Departments Employees ssn Works_In
6
Database Management Systems, R. Ramakrishnan and J. Gehrke6 Weak Entities v 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 (one 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
7
Database Management Systems, R. Ramakrishnan and J. Gehrke7 ISA (`is a’) Hierarchies Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked 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. 1. Overlap constraints : Can Joe be an Hourly_Emps as well as a Contract_Emps entity? ( Allowed/disallowed ) 2. Covering constraints : Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no) 3. Reasons for using ISA : – To add descriptive attributes specific to a subclass. – To identify entitities that participate in a relationship.
8
Database Management Systems, R. Ramakrishnan and J. Gehrke8 Aggregation v Used when we have to model a relationship involving (entitity sets and) a relationship set. – Aggregation allows us to treat a relationship set as an entity set for purposes of participation in (other) relationships. * Aggregation vs. ternary relationship : 1. Monitors is a distinct relationship, with a descriptive attribute. 2. Also, can say that each sponsorship is monitored by at most one employee. budget did pid started_on pbudget dname until Departments Projects Sponsors Employees Monitors lot name ssn since
9
Database Management Systems, R. Ramakrishnan and J. Gehrke9 Conceptual Design Using the ER Model v Design choices: – Should a concept be modeled as an entity or an attribute? – Should a concept be modeled as an entity or a relationship? – Identifying relationships: Binary or ternary? Aggregation? v Constraints in the ER Model: – A lot of data semantics can (and should) be captured. – But some constraints cannot be captured in ER diagrams.
10
Database Management Systems, R. Ramakrishnan and J. Gehrke10 Entity vs. Attribute Should address be an attribute of Employees or an entity (connected to Employees by a relationship)? Depends upon the use we want to make of address information, and the semantics of the data: 1. If we have several addresses per employee, address must be an entity (since attributes cannot be set-valued). 2. If the structure (city, street, etc.) is important, e.g., we want to retrieve employees in a given city, address must be modeled as an entity (since attribute values are atomic).
11
Database Management Systems, R. Ramakrishnan and J. Gehrke11 Entity vs. Attribute (Contd.) v Works_In2 does not allow an employee to work in a department for two or more periods. v Similar to the problem of wanting to record several addresses for an employee: we want to record several values of the descriptive attributes for each instance of this relationship. name Employees ssn lot Works_In2 from to dname budget did Departments dname budget did name Departments ssn lot Employees Works_In3 Duration from to
12
Database Management Systems, R. Ramakrishnan and J. Gehrke12 Entity vs. Relationship v First ER diagram OK if a manager gets a separate discretionary budget for each dept. v What if a manager gets a discretionary budget that covers all managed depts? – Redundancy of dbudget, which is stored for each dept managed by the manager. Manages2 name dname budget did Employees Departments ssn lot dbudget since Employees since name dname budget did Departments ssn lot Mgr_Appts Manages3 dbudget apptnum Misleading: suggests dbudget tied to managed dept.
13
Database Management Systems, R. Ramakrishnan and J. Gehrke13 Logical DB Design: ER to Relational chapter 3 1. Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY KEY (ssn)) Employees ssn name lot
14
Database Management Systems, R. Ramakrishnan and J. Gehrke14 Relationship Sets 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). o This set of attributes forms a superkey for the relation. – All descriptive attributes. CREATE TABLE Works_In( ssn CHAR (1), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)
15
Database Management Systems, R. Ramakrishnan and J. Gehrke15 Review: Key Constraints Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1
16
Database Management Systems, R. Ramakrishnan and J. Gehrke16 Translating ER Diagrams with Key Constraints CREATE TABLE Manages( ssn CHAR(11), 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, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) dname budget did since lot name ssn Manages Employees Departments
17
Database Management Systems, R. Ramakrishnan and J. Gehrke17 Review: Participation Constraints lot name dname budgetdid since name dname budgetdid since Manages Departments Employees ssn 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 )
18
Database Management Systems, R. Ramakrishnan and J. Gehrke18 Translating Weak Entity Sets 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, ON DELETE CASCADE ) lot name age pname Dependents Employees ssn Policy cost
19
Database Management Systems, R. Ramakrishnan and J. Gehrke19 Review: ISA Hierarchies Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked
20
Database Management Systems, R. Ramakrishnan and J. Gehrke20 Translating ISA Hierarchies to Relations v General approach: – 3 relations: Employees, Hourly_Emps and Contract_Emps. u 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). u Queries involving all employees easy, those involving just Hourly_Emps require a join to get some attributes. v 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.