Académique Documents
Professionnel Documents
Culture Documents
The Entity-Relationship
Model
Employees
Employees
since
dname
did budget super- subor-
visor dinate
Reports_To
Departments Works_In
Key Constraints
Employees Manages Departments
An employee can
work in many Works_In
departments; a
dept can have since
many employees.
Key Constraints
Employees Manages Departments
Approach 1 Approach 2
Works_In
Works_In
CREATE TABLE Dept_Mgr(
did INTEGER,
dname CHAR(20), since
budget REAL, How to enforce total
participation of Employees
mgr_ssn CHAR(11),NOT NULL & Departments in Works_in?
since DATE, •Every did value appearing
in Departments appears in a
PRIMARY KEY (did), tuple of Works_in.
FOREIGN KEY (ssn) •FK from
Departments/Employees to
REFERENCES Employees Works_in?
ON DELETE RESRTICT) •ASSERTIONS!!
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 one-to-many relationship set (one
owner, many weak entities).
– Weak entity set must have total participation in
this identifying relationship set.
name
cost pname age
ssn lot
Purchaser
• Think through all Beneficiary
the constraints in
the 2nd diagram! Better design Policies
policyid cost
Binary vs. Ternary Relationships (Contd.)
• Previous example illustrated a case when two binary
relationships were better than one ternary.
Suppliers
Binary vs. Ternary Relationships (Contd.)
quantity
VS.
Suppliers
can-supply
Suppliers deals-with
• General approach:
– 3 relations: Employees, Hourly_Emps and Contract_Emps.
• 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).
• Queries involving all employees easy, those involving
just Hourly_Emps require a join to get some attributes.
• 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.
Translating ISA Hierarchies to Relations
• General approach:
– Always applicable
– A query that examine all employees & do not care about
attributes specific to subclasses are answered using just
one relation
– Full information about employees require joins
• Alternative: Just Hourly_Emps and Contract_Emps.
– Not applicable if we have employees that are neither
Hourly_emps nor Contract_emps
– If an employee is both, his name & lot are stored twice
– A query that needs to examine all employees must join the
two relations
name
Aggregation ssn lot
Employees
Used to model a
relationship
involving a
Monitors until
relationship set.
Allows us to treat a started_on since
dname
relationship set
pid pbudget did budget
for purposes of
participation in Aggregation vs. ternary relationship?
(other) Monitors is a distinct relationship,
relationships. with a descriptive attribute.
Also, can say that each sponsorship
is monitored by at most one employee.
Review - Our Basic ER Model
What if manager’s
dbudget covers all ssn
name
lot
managed depts? dname
(can repeat value, but Employees
did budget
such redundancy is
problematic)
Departments
apptnum Mgr_Appts
dbudget
These things get pretty hairy!
Translation to
relational model?
name
cost pname age
ssn lot
hourly_wages hours_worked
ISA
As in C++, or other PLs, contractid