ER to Relational
Mapping
Logical DB Design: ER to Relational
Entity sets to tables.
name
ssn lot
35
Employees
CREATE TABLE
Employees
(ssn CHAR(11),
name CHAR(20),
lot INTEGER,
PRIMARY KEY
Relationship Sets to Tables
CREATE TABLE Works_In(
In translating a many-to- ssn CHAR(1),
many relationship set to a did INTEGER,
relation, attributes of the since DATE,
relation must include:
PRIMARY KEY (ssn, did),
Keys for each
FOREIGN KEY (ssn)
participating entity set
(as foreign keys). REFERENCES Employees,
This set of attributes FOREIGN KEY (did)
forms a superkey for REFERENCES Departments
the relation.
All descriptive
attributes.
3/3/93
2/2/92
Review: Key Constraints
since
Each dept has at name dname
most one manager, ssn lot did budget
according to the
key constraint on
Manages. Employees Manages Departments
Translation to
relational model?
1-to-1 1-to Many Many-to-1 Many-to-Many
Translating ER Diagrams with Key
Constraints
CREATE TABLE Manages(
ssn CHAR(11),
Map relationship did INTEGER,
set to a table: since DATE,
Note that did is PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES
the key now!
Employees,
Separate tables FOREIGN KEY (did) REFERENCES
for Employees Departments)
and CREATE TABLE Dept_Mgr(
did INTEGER,
Departments. dname CHAR(20),
Since each budget REAL,
department has a ssn CHAR(11),
unique manager, we since DATE,
could instead PRIMARY KEY (did),
combine Manages FOREIGN KEY (ssn) REFERENCES Employees)
and Departments.
Review: Participation Constraints
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).
Every did value in Departments table must
appear in a row of the Manages
since table (with a non-
name dname
null ssn
ssn
value!)
lot did budget
Employees Manages Departments
Works_In
since
Participation Constraints in SQL
We can capture participation constraints
involving one entity set in a binary relationship,
but little else (without resorting to CHECK
constraints).
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)
Review: 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 (1
owner, many weak entities).
Weak entity set must have total participation in this
identifying
namerelationship set.
cost pname age
ssn lot
Employees Policy Dependents
Translating Weak Entity Sets
Weak entity set and identifying
relationship set are translated into a
single table.
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) NOT NULL,
PRIMARY KEY (pname, ssn),
FOREIGN KEY (ssn) REFERENCES Employees,
ON DELETE CASCADE)
name
ssn lot
Review: ISA Hierarchies Employees
hourly_wages hours_worked
ISA
As in C++, or other PLs, contractid
attributes are inherited.
Hourly_Emps Contract_Emps
If we declare A ISA B, every
A entity is also considered to
be a B entity.
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)
Translating ISA Hierarchies to
Relations
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.
ER Model Summary
Usually easier to understand than
Relational
Expresses relationships clearly
Rules to convert ER-diagrams to Relational
Schema
Some systems use ER-model for schema
design
Some people use ER-model as step before
creating relational tables