You are on page 1of 7

HW1 Solution

January 26, 2016

CS 5530 Database Systems;

Spring 2016

Instructor: Feifei Li, University of Utah

1 (a) A good design is as follows:


height

result

birthday

ssn

weight

PERSON

TEST-Event

Take-Test

testid

date

TestType
Father-Of

Mother-Of

ISA

TEST
MALE

FEMALE
tname

muscle-mass

cost

T-level

Note that a bold line indicates a total participation constraint.


(b) Tables (Schemas) and Keys
Person (ssn, mother-ssn, father-ssn, birthday, height, weight)
Female (ssn, t-level)
Male (ssn, muscle-mass)
TestEvent (testid, result, date, ssn, tname)
Test (tname, cost)
Note that cost is an optional attribute; just to illustrate the fact that you may have other attributes
of interest for a type of test.
SQL statement:
CREATE TABLE Male (
ssn INTEGER, musclemass REAL,
Primary Key (ssn),
Foreign Key (ssn) References Person );

[Foreign key is optional]

CREATE TABLE Female (


ssn INTEGER, tlevel INTEGER,
Primary Key(ssn)
Foreign Key (ssn) References Person ); [Foreign key is optional]
CREATE TABLE Person (
ssn INTEGER, birthday DATE, height REAL, weight REAL,
mother-ssn INTEGER, father-ssn INTEGER,

Primary Key (ssn),


Foreign Key (father-ssn) References (Male),
Foreign Key (mother-ssn) References (Female));
CREATE TABLE TestEvent (
testid INTEGER, aid INTEGER NOT NULL, tname CHAR[40] NOT NULL,
date DATE, result REAL,
Primary Key (testid),
Foreign Key (SSN) References (Person),
Foreign Key (tname) References (Test));
CREATE TABLE Test(
tname CHAR[40], cost INTEGER,

Primary Key (tname));

2 (a) Here is an ER diagram for this database:

name
ssn

model-tag

healthpolicy-no

salary

make

Employees

address

type
tel-no
Model

skillrating

Expertise

ISA

Accountant

Technician

ModelType
training-date

Car

Test-info
IsType

TestType

vin
color

name

maxscore
test-no

score

Test

hours

date
tid

(b) The relational schemas are as follows:


Model (model-tag, make, type)
Car (vin, color, model-tag)
Expertise(model-tag, ssn)
Employee (ssn, name, healthpolicy-no, address, tel-no, salary)
Technician (ssn, skillrating)
Accountant (ssn, training-date)
Test (tid, score, hours, date, test-no)
TestInfo (vin, ssn, tid)
TestType (test-no, name, maxscore)
The SQL statements for creating these tables are as follows:
CREATE TABLE Model(
model-tag CHAR(10), make CHAR(20), type CHAR(20),
PRIMARY KEY (model-tag))
CREATE TABLE Car(
vin CHAR(20), color CHAR(20), model-tag CHAR(20) NOT NULL,
PRIMARY KEY (vin),
FOREIGN KEY (model-tag) REFERENCES Model)
CREATE TABLE Expertise(
ssn CHAR(20), model-tag CHAR(20),
PRIMARY KEY (ssn, model-tag),
FOREIGN KEY (ssn) REFERENCES Technician,
FOREIGN KEY (model-tag) REFERENCES Model)
CREATE TABLE Employees(
ssn CHAR(20), name CHAR(30), address CHAR(50),
tel-no INTEGER, slalary INTEGER, healthpolicy-no INTEGER,
PRIMARY KEY (ssn))
CREATE TABLE Technician(
ssn CHAR(20), skillrating INTEGER,
PRIMARY KEY (ssn),
FOREIGN KEY (ssn) REFERENCES Employees) [Foreign key is optional]
CREATE TABLE Accountant(
ssn CHAR(20), training-date DATE,
PRIMARY KEY (ssn),
FOREIGN KEY (ssn) REFERENCES Employees) [Foreign key is optional]
CREATE TABLE Test(
tid INTEGER, hours INTEGER, date DATE, score INTEGER,
test-no INTEGER NOT NULL,
PRIMARY KEY (tid),
FOREIGN KEY (test-no) REFERENCES TestType)
CREATE TABLE TestInfo(
tid INTEGER, ssn CHAR(20), vin INTEGER,
PRIMARY KEY (tid, ssn, vin),
FOREIGN KEY (vin) REFERENCES Car,
FOREIGN KEY (ssn) REFERENCES Technicians,
FOREIGN KEY (tid) REFERENCES Test)

CREATE TABLE TestType(


test-no INTEGER, name CHAR(20),
PRIMARY KEY (test-no))

maxscore INTEGER,

3
a (10 points) Draw a (simple) E-R diagram that results in a primary key/foreign key constraint to be
created between tables. Show the SQL statements that create the tables including the foreign key and
primary key indications.

CREATE TABLE Customers(


cid CHAR(10),
primary key(cid))
CREATE TABLE Products(
pid CHAR(10),
primary key(pid))
CREATE TABLE Orders(
cid CHAR(10),
pid CHAR(10),
PRIMARY KEY (cid, pid),
FOREIGN KEY (cid) REFERENCES Customers,
FOREIGN KEY (pid) REFERENCES Products)

CREATE TABLE Salespersons(


sid CHAR(10),
primary key (sid))
CREATE TABLE Customers(
cid CHAR(10),
sid CHAR(10),
PRIMARY KEY(cid),
FOREIGN KEY(sid) REFERENCES Salespersons)

CREATE TABLE Customers(


cid CHAR(10),
primary key(cid))
CREATE TABLE Orders(
cid CHAR(10),
pid CHAR(10),
PRIMARY KEY (pid),
UNIQUE (cid),
FOREIGN KEY(cid) REFERENCES Customers)

b (5 points) For the relational tables you generated in question 3(a), describe which insert and delete
operations in this database must be checked to ensure that referential integrity is not violated for that
foreign key. Please state specifically which operations on which relations can cause problems.
Many to Many:
On insert(Order) exists(Customers) and exist(Products);
On delete(Customers) delete(Orders) or not allowed or set affected cid value in Orders to some
default value;
On delete(Products) delete(Orders) or not allowed set affected cid value in Orders to some default
value;
One to Many:
On insert(Customers) exists(Salespersons);
On delete(Salespersons) delete(Customers) or not allowed or set affected pid value in Cutomers to
some default value.
One to One:
On insert(Order) exists(Customers);
On delete(Customers) delete(Orders) or not allowed or set affected cid value in Orders to some
default value.
c (5 points) Consider a database of employees in which we need to record information about employees
addresses. Name one condition which would cause you to make Address an entity set of its own
rather than an attribute of the employee entity set.
There are several conditions.
An employee may have more than one address and all of them are supposed to be stored in the
database.
Attribute address is composed of street, city, state, etc. Stuctures of an address may also be of
interest to some queries.
One address may be shared by multiple employees.