ch2-3 Practice Exercises
ch2-3 Practice Exercises
ch2-3 Practice Exercises
PRACTICE EXERCISES
Chapter 2: Database Design
Chapter 3: Relational Model
M. Amanda Crick
Exercise 2.4
Problem
A company database needs to store
information about employees (identified
by ssn, with salary and phone as
attributes), departments (identified by
dno, with dname and budget as
attributes), and children of employees
(with name and age as attributes).
Exercise 2.4
Problem
Employees work in departments; each
department is managed by an employee;
a child must be identified uniquely by
name when the parent (who is an
employee; assume that only one parent
works for the company) is known. We are
not interested in information about a child
once the parent leaves the company.
Draw an ER diagram that captures this
information.
Exercise 2.4
Solution
First, we shall design the entities and
relationships.
Exercise 2.4
Solution
salary
ssn
dnam
e
phone
Employees
Manages
Dependent
Works_In
Child
name
dno
age
budge
t
Departmen
ts
Exercise 2.4
Solution
Now, we will design the constraints.
Exercise 2.4
Solution
salary
ssn
dnam
e
phone
Employees
Manages
Dependent
Works_In
Child
name
dno
age
budge
t
Departmen
ts
Exercise 2.8
Problem
Although you always wanted to be an artist,
you ended up being an expert on databases
because you love to cook data and you
somehow confused database with data
baste. Your old love is still there, however,
so you set up a database company, ArtBase,
that builds a product for art galleries. The
core of this product is a database with a
schema that captures all the information
that galleries need to maintain.
Exercise 2.8
Problem
Galleries keep information about artists, their
names (which are unique), birthplaces, age,and
style of art. For each piece of artwork, the
artist, the year it was made, its unique title, its
type of art (e.g., painting, lithograph, sculpture,
photograph), and its price must be stored.
Pieces of artwork are also classified into groups
of various kinds, for example, portraits, still
lifes, works by Picasso, or works of the 19th
century; a given piece may belong to more
than one group.
Exercise 2.8
Problem
Each group is identified by a name (like
those just given) that describes the group.
Finally, galleries keep information about
customers. For each customer, galleries
keep that persons unique name, address,
total amount of dollars spent in the gallery
(very important!), and the artists and
groups of art that the customer tends to
like.
Draw the ER diagram for the database.
Exercise 2.8
Solution
Like before, we begin with the entities
and relationships.
artists, their names (which are
unique), birthplaces, age, and style of
art.
For each piece of artwork, the artist, the
year it was made, its unique title, its type
of art and its price must be stored.
Exercise 2.8
Solution
Pieces of artwork are also classified into
groups of various kinds, Each group is
identified by a name (like those just given)
that describes the group.
For each customer, galleries keep that
persons unique name, address, total
amount of dollars spent in the gallery
(very important!), and the artists and
groups of art that the customer tends to
like.
Exercise 2.8
Solution
type
year
name
title
Like_Group
Group
Classify
Artwork
Customer
Like_Artist
Artist
Paints
addre
ss
cust_i
d
name
amou
nt
name
birthplace
style
age
price
Exercise 2.8
Solution
Now we look at constraints.
Exercise 2.8
Solution
type
year
name
title
Like_Group
Group
Classify
Artwork
Customer
Like_Artist
Artist
Paints
addre
ss
cust_i
d
name
amou
nt
name
birthplace
style
age
price
Exercise 2.8
Solution
Suppose we had several piece of artwork
with the same title, and we told them
apart by artist?
Example: What is Love? by Cheryl D,
What is Love? by Joe Brown, etc.
Exercise 2.8
Solution
type
year
name
title
Like_Group
Group
Classify
Artwork
Customer
Like_Artist
Artist
Paints
addre
ss
cust_i
d
name
amou
nt
name
birthplace
style
age
price
Exercise 3.14
Problem
Consider the scenario from Exercise 2.4,
where you designed an ER diagram for a
company database. Write SQL
statements to create the corresponding
relations and capture as many of the
constraints as possible. If you cannot
capture some constraints, explain why.
Exercise 3.14
ER Diagram from Exercise 2.4dnam
salary
ssn
e
phone
Employees
Manages
Dependent
Works_In
Child
name
dno
age
budge
t
Departmen
ts
Exercise 3.14
Solution
First we begin with the entities
Employees and Departments.
Translating these to SQL is
straightforward.
Exercise 3.14
Solution
salary
ssn
phone
Employees
dnam
e
dno
budge
t
Departmen
ts
CREATE TABLE
Departments (
dno INTEGER,
budget INTEGER,
dname CHAR(20),
PRIMARY KEY (dno)
)
Exercise 3.14
Solution
Next, we translate the relationships,
Manages and Dependents.
We translate each these to a table
mapping one entity to another.
We also use foreign constraints to make
sure every row in the relationship tables
refers only to rows that exist in the entity
tables.
Exercise 3.14
Solution
salary
ssn
Employees
dnam
e
phone
dno
Manages
budge
t
Departmen
ts
Works_In
Exercise 3.14
Solution
Why did we make dno the primary key
for Manages?
Since each department can have at most
one manager, each dno can appear at
most once in the Manages table, making
it a key for Manages.
Note that if we had made (ssn, dno) the
key for Manages, a department could
have more than one Manager.
Exercise 3.14
Solution
Finally, we translate the weak entity
Child and its corresponding relationship
Dependent
Exercise 3.14
Solution
salary
ssn
phone
Employees
Dependent
Child
name
age
Exercise 3.18
Problem
Write SQL statements to create the
corresponding relations to the ER
diagram you designed for Exercise 2.8. If
your translation cannot capture any
constraints in the ER diagram, explain
why.
Exercise 3.18
ER Diagram from Exercise 2.8
type
year
name
title
Like_Group
Group
Classify
Artwork
Customer
Like_Artist
Artist
Paints
addre
ss
cust_i
d
name
amou
nt
name
birthplace
style
age
price
Exercise 3.18
Solution
The entities are translated similarly to
Exercise 3.4. Since these are fairly
simple, we shall skip them.
Now, we shall translate the relationships.
Exercise 3.18
Solution
name
Like_Group
Group
Customer
addre
ss
cust_i
d
name
amou
nt
Exercise 3.18
Solution
Customer
Like_Artist
addre
ss
cust_i
d
name
amou
nt
Artist
name
style
birthplace
age
Exercise 3.18
Solution
type
year
title
Artwork
Artist
name
birthplace
Paints
style
age
price
Exercise 3.18
Solution
name
Group
type
year
title
Classify
Artwork
price
Exercise 3.8
Problem
Answer each of the following questions
briefly. The questions are based on the
following relational schema:
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary:
real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real,
managerid: integer
1.
Exercise 3.8
Solution for (1)
An example of a foreign constraint that
involves Dept is:
CREATE TABLE Works (
eid INTEGER NOT NULL ,
did INTEGER NOT NULL ,
pcttime INTEGER,
PRIMARY KEY (eid, did),
UNIQUE (eid),
FOREIGN KEY (did)
REFERENCES Dept )
Exercise 3.8
Solution for (1)
Furthermore, when a user attempts to
delete a tuple from Dept, we can
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary:
real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real,
managerid: integer
2.
Exercise 3.8
Solution for (2)
Emp(eid: integer, ename: string, age: integer, salary:
real)
Works(eid: integer, did: integer, pcttime: integer)
CREATE TABLE Emp ( CREATE TABLE Works (
eid INTEGER,
eid INTEGER,
did INTEGER,
ename CHAR(10),
pcttime INTEGER,
age INTEGER,
salary REAL,
PRIMARY KEY (eid, did),
PRIMARY KEY
FOREIGN KEY (did)
(eid) )
REFERENCES Dept,
FOREIGN KEY (eid)
REFERENCES Emp,
ON DELETE CASCADE)
Exercise 3.8
Solution for (2)
Dept(did: integer, dname: string, budget: real, managerid:
integer
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real, managerid:
integer
3.
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real, managerid:
integer
4.
INSERT
INTO Emp (eid, ename, age,
salary)
VALUES (101, John Doe, 32,
15000)
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real, managerid:
integer
5.
Exercise 3.8
Problem
Emp(eid: integer, ename: string, age: integer, salary:
real)
Works(eid: integer, did: integer, pcttime: integer)
Dept(did: integer, dname: string, budget: real,
managerid: integer
6.
Exercise 3.8
Solution for (6)
DELETE
FROM Dept D
WHERE D.dname =
Toy
Exercise 3.8
Solution for (6)
What other actions can the system take
on deleting a Dept tuple? What are the
pros and cons of each action?