Documente Academic
Documente Profesional
Documente Cultură
Chapter 2
What information about these entities and relationships should we store in the database?
A database `schema in the ER Model can be represented pictorially (ER diagrams). Can map an ER diagram into a relational schema.
2
Entity: Real-world object distinguishable from other objects. An entity is described (in database) using a set of attributes.
All entities in an entity set have the same set of attributes (until we consider ISA hierarchies, anyway!)
Each attribute has a Key domain. Each entity set has a key ssn uniquely identifies an entity in the set Entity set
Attribute
name
lot Employees
3
Relationship
since name dname lot Employees
Works_In
ssn
did
budget Departments
Relationship: Association among two or more entities, e.g., Jessica works in Pharmacy department.
Relationship Set
since name ssn Employees lot
Works_In
Relationship set
EMPLOYEES
WORKS_IN
DEPARTMENTS
An n-ary relationship set R relates n entity sets E1 ... En; each relationship in R involves entities e1 in E1, ..., en in En
ssn
Employees
lot
Works_In
dname did
budget
Departments
Each department has offices in several locations We want to record the locations at which each employee works
Departments
Locations
Relationship Participation
Same entity set could participate in different relationship sets, or in different roles in the same set.
name
ssn lot
Employees
supervisor
subordinate
Reports_To
Mapping to Tables
since name dname lot
Works_In
ssn
did
budget Departments
Employees
Employees
ssn name lot
Works_In
ssn 1234 did 8
Departments
did 8 dname DB budget 1,000,000
1234
Jenny
A456
Consider Works_In: An employee can work in many departments; a dept can have many employees (many-tomamy relationship). In contrast, each dept has at most one manager, according to the key constraint on Manages (1-tomany relationship).
lot
Employees
Works_In
manyto-many
Departments
Employees
Manages
1-tomany
Departments
Key Constraints
since name ssn lot did dname budget
Employees
Manages
Departments
EMPLOYEES
MANAGES
DEPARTMENTS
1-to Many
Given a Departments entity, we can uniquely determine the Manages relationship in which it appears
10
Many-to-Many
1-to-1
1-to Many
Many-to-1
11
Participation Constraints
Does every department have a manager?
the participation of Departments in Manages is said to be total (vs. partial). Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value! - more details later in this course)
name ssn Employees since dname
lot Manages
did
budget Departments
Works_In
Partial participation
since
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke
Total participation
(a participation constraint)
12
Employees
Manages
Departments
Employees Employees
Manages Manages
Departments Departments
Partial Participation
Has no manager
Total Participation
13
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 oneto-many relationship set (one owner, many weak entities). Weak entity set must have total participation in this identifying relationship set.
Partial key name ssn lot cost dpname age
Employees
Policy
Dependents
Employees
As in C++, or other PLs, hourly_wages attributes are inherited. If we declare A ISA B, every A entity is also considered to be a B entity.
Hourly_Emps
Contract_Emps
Covering constraints: Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)
15
ssn
name lot
Employees
hours_worked ISA
Manages
Hourly_Emps Contract_Emps
contractid
16
Aggregation
Used when we have to model a relationship involving (entity sets and) a relationship set.
started_on pid Projects
Monitors
until
Aggregation allows us to treat a relationship set as an entity set for purpose of participation in (other) relationships
since did
dname budget
pbudget
Sponsors
Departments
Aggregation vs. ternary relationship: Monitors is a distinct relationship, with a descriptive attribute. Also, can say that each sponsorship is monitored by at most one employee.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17
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?
But some constraints cannot be captured in ER diagrams, e.g., functional dependencies (discussed later in this course)
18
lot
name ssn Employees lot Street City State Address
Employees
live_at
Option 2
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19
Depends upon the use we want to make of address information, and the semantics of the data:
If we have several addresses per employee, address must be an entity (since attributes cannot be set-valued).
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).
name address lot name
Employees can have multiple addresses Can retrieve employees in a given city
ssn
ssn
Employees Employees
dname
did budget Departments
Employees
Works_In4 does
did 9 9
to 2002 2009
same instance
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21
Works_In4 does not allow an employee to work in a department for two or more periods. Similar to the problem of wanting to record several addresses for an employee:
name
dname
ssn
Employees
lot
Works_In5
did
budget Departments
from
Duration
to
Works_In5
ssn 1234 1234 did 9 9 from 1998 2006 to 2002 2009
We want to record several values of the descriptive attributes for each instance of this relationship. Accomplished by introducing new entity set, Duration.
22
First ER diagram OK if a manager gets a separate discretionary budget for each dept. What if a manager gets a discretionary budget that covers all managed depts?
budget
Departments
Manages2
ssn Employees
Redundancy: dbudget stored for each dept managed by manager. Misleading: Suggests dbudget associated with department-mgr combination.
ISA
Manages2
Managers
dbudget
If each policy is owned by just one employee, and each dependent is tied to the covering policy, we need some constraints Owns 2
policies
Employees
Covers
Policy
ssn
Key constraint A policy cannot be owned jointly by two or more employees Total participation Every policy must be owned by some employee
Weak entity set Each dependent is uniquely identified by a policy (i.e., policyid and pname)
25
Parts
No combination of binary relationships is an adequate substitute for the above ternary relationship:
The binary relationships S can-supply P, D needs P, and D deals-with S do not imply that D has agreed to buy P from S. How do we record qty in the binary relationships ?
26
pid
Professor
Project
budget
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
ssn age name deg_prog 27
Each project is managed by one professor (principal investigator) Professors can manage multiple projects
rank age ssn Specialty
pid
Professor
Manages
Project
budget
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
grad
ssn
age
name
deg_prog 28
Each project is worked on by one or more professors (co-investigators) Professors can work on multiple projects
rank age ssn Specialty Work_in
pid
Professor
Manages
Project
budget
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
ssn
age
name
deg_prog 29
Each project is worked on by one or more graduate students (research assistants) Graduate students can work on multiple projects
rank age ssn Specialty Work_in
pid
Professor
Manages
Project
budget
Work_proj
hours
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
ssn
age
name
deg_prog 30
When graduate students work on multiple projects, they must have a supervisor (professor) for each one.
rank age ssn Specialty Work_in
pid
Professor
Manages
Project
budget
Supervises
Work_proj
hours
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
ssn
age
name
deg_prog 31
pid
Professor
Manages
Project
budget
Runs
Supervises
Work_proj
hours
Dept
dno dname Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke office
Graduate
ssn
age
name
deg_prog 32
Professors work in one or more departments For each department a professor works in, a time percentage is associated with their job
rank age ssn Specialty Work_in
pid
Professor
Manages
Project
budget
Work_dept
Runs
Supervises
Work_proj
hours
pc_time
Dept
dno dname office
Graduate
ssn
age
name
deg_prog 33
pid
Professor
Manages
Project
budget
Work_dept
Runs
Supervises
Work_proj
hours
pc_time
Dept
dno dname office
Major
Graduate
ssn
age
name
deg_prog 34
pid
Professor
Manages
Project
budget
Work_dept
Runs
Supervises
Work_proj
hours
pc_time
senior
Dept
dno dname office
Major
Graduate
grad
Advisor
ssn
age
name
deg_prog 35
pid
Professor
Manages
Project
budget
Work_dept
Runs
Supervises
Work_proj
hours
pc_time
senior
Dept
dno dname office
Major
Graduate
grad
Advisor
ssn
age
name
deg_prog 36
Yields a high-level description of data to be stored Constructs are expressive, close to the way people think about their applications.
Basic constructs: entities, relationships, and attributes (of entities and relationships).
Some additional constructs: weak entities, ISA hierarchies, and aggregation.
Summary of ER (Contd.)
38
Summary of ER (Contd.)
There are often many ways to model a given scenario! Analyzing alternative ER designs can be tricky. Common choices include:
Entity vs. attribute, entity vs. relationship, binary or n-ary relationship, whether or not to use ISA hierarchies, and whether or not to use aggregation.
Ensuring good database design: resulting relational schema should be analyzed and refined further.
Functional dependency information and normalization techniques are especially useful (discussed later).
39
Homework #1
Do Exercises 2.4 and 2.8 in the textbook
Due: 01/26/2012
40