You are on page 1of 14

Reducing ER Schema to

Relational Schema

Excerpt from
Chapter 3, “Database Management Systems” 3ed, R. Ramakrishnan and J. Gehrke

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 1


Logical DB Design: ER to Relational

™ Entity sets to tables:

CREATE TABLE Employees


name (ssn CHAR(11),
ssn lot name CHAR(20),
lot INTEGER,
Employees PRIMARY KEY (ssn))

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 2


Relationship Sets to Tables

™ In translating a relationship CREATE TABLE Works_In(


set to a relation, attributes of ssn CHAR(1),
the relation must include: did INTEGER,
ƒ Keys for each since DATE,
participating entity set PRIMARY KEY (ssn, did),
(as foreign keys). FOREIGN KEY (ssn)
• This set of attributes REFERENCES Employees,
forms a superkey for FOREIGN KEY (did)
the relation. REFERENCES Departments)
ƒ All descriptive attributes.

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 3


Review: Key Constraints
since
™ Each dept has at name dname

most one manager, ssn lot did budget

according to the
key constraint on Employees Manages Departments
Manages.

Translation to
relational model?

1-to-1 1-to Many Many-to-1 Many-to-Many

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 4


Translating ER Diagrams with Key Constraints
™ Map relationship to a
table: CREATE TABLE Manages(
ssn CHAR(11),
ƒ Note that did is did INTEGER,
the key now! since DATE,
PRIMARY KEY (did),
ƒ Separate tables for
FOREIGN KEY (ssn) REFERENCES Employees,
Employees and FOREIGN KEY (did) REFERENCES Departments)
Departments.
CREATE TABLE Dept_Mgr(
™ Since each did INTEGER,
department has a dname CHAR(20),
unique manager, we budget REAL,
could instead ssn CHAR(11),
since DATE,
combine Manages PRIMARY KEY (did),
and Departments. FOREIGN KEY (ssn) REFERENCES Employees)

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 5


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 table (with a non-null ssn value!)
since
name dname
ssn lot did budget

Employees Manages Departments

Works_In

since
1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 6
Participation Constraints in SQL
™ We can capture participation constraints involving
one entity set in a binary relationship

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)

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 7


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 relationship set.

name
cost pname age
ssn lot

Employees Policy Dependents

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 8


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)

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 9


Review: Binary vs. Ternary
Relationships ssn name lot pname age

™ Constraints: Employees Covers Dependents

ƒ each policy is Bad design


owned by just 1 Policies
employee
policyid cost
ƒ every policy
must be owned name pname age
by some ssn lot
employee Dependents
Employees
ƒ each dependent
is tied to the
covering policy Purchaser
Beneficiary

Better design
Policies

policyid cost
1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 10
Binary vs. Ternary Relationships (Contd.)
CREATE TABLE Policies (
™ The key policyid INTEGER,
constraints allow cost REAL,
us to combine ssn CHAR(11) NOT NULL,
Purchaser with PRIMARY KEY (policyid).
Policies and FOREIGN KEY (ssn) REFERENCES Employees,
Beneficiary with ON DELETE CASCADE)
Dependents.
CREATE TABLE Dependents (
™ Participation pname CHAR(20),
constraints lead to age INTEGER,
NOT NULL
policyid INTEGER,
constraints. PRIMARY KEY (pname, policyid).
FOREIGN KEY (policyid) REFERENCES Policies,
ON DELETE CASCADE)
1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 11
Extended ER (EER)

Excerpt from
Chapters 2 & 3, “Database Management Systems” 3ed, R. Ramakrishnan and J. Gehrke

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 12


name
ssn lot

Specialization/Generalization Employees

¾As in C++, or other PLs, hourly_wages hours_worked

attributes are inherited. contractid

¾If we declare A ISA B, every A


Contract_Emps
entity is also considered to be a B Hourly_Emps

entity.
™ Overlap constraints: Can Joe be an Hourly_Emps as well as a
Contract_Emps entity? (Disjoint/Overlapping)
™ Covering constraints: Does every Employees entity also have to be an
Hourly_Emps or a Contract_Emps entity? (Total/Partial)
™ Reasons for using ISA:
ƒ To add descriptive attributes specific to a subclass.
ƒ To identify entities that participate in a relationship.

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 13


Translating Class 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.

1/30/2008 USC - CSCI585 - Spring 2008 - Farnoush Banaei-Kashani 14

You might also like