DBT Exercises
DBT Exercises
1. A calendar program that allows users to browse each others calendars and to book
common appointments shall be developed. The program has a database which keeps track
of the users and their calendars.
You use the calendar to store data concerning appointments. An appointment starts
and ends at a given time on a given day and is described by a text. You may specify that
you wish to be reminded of an appointment. Reminders are of different kinds: a signal in
the computers loudspeaker, a pop-up window with the description of the meeting, or an
e-mail containing the description. You may, for each reminder, specify how long before the
appointment that you wish to be reminded.
Develop an E/R model of the database.
2. A TV company wishes to develop a database to store data about the TV series that the
company produces. The data includes information about actors who play in the series, and
directors who direct the episodes of the series.
Actors and directors are employed by the company. A TV series are divided into
episodes. Each episode may be transmitted at several occasions. An actor is hired to
participate in a series, but may participate in many series. Each episode of a series
is directed by one of the directors, but different episodes may be directed by different
directors.
Examples of database queries:
Develop an E/R model of this system. Find attributes of the entity sets. Determine which
of the attributes that can be used as primary keys.
The spaces with electric outlets are popular, and there is a queue of apartments that
wish to rent such a space. Each apartment may have at most one place in the queue. When
a space becomes available, the apartment with the longest queue time may rent it. One
of the apartment owners must sign the contract (an apartment may have more than one
owner, and an owner may own more than one apartment).
The association sometimes has problems with scrap cars that are deposited in the
parking lot. It is often a difficult procedure to get rid of these cars. Discovery of a
scrap car must be registered in the database, as well as each thing that happens with
the car (Parking space 43: 20090426 Discovered suspect car, license number XYZ789,
20090502 Called the police about the car, and so on).
How many boys and girls will start school during year x?
How many people will become old-age pensioners during year x?
How many households have more than x people?
How many people are single parents?
In how many households is at least one member unemployed?
How many households have a total income that is less than the norm for receiving
social benefits?
Develop an E/R model of the database, including attributes and primary keys. Carefully
consider the representation of household and parent.
Exercise 2 from E/R to relations, SQL 3
1. A company produces and sells products that are assembled from parts. A part is either
manufactured internally or bought from a supplier. In the company database this is
modeled as follows:
Part
Product
0..* 0..* partId PriceInfo
productId
name price
name
qtyInStock
1
0..* 0..*
ManufacturedPart PurchasedPart Supplier
manfCost supplierId
name
address
a) Translate the E/R diagram into relations. Make the translation of the hierarchical
structure between the Part entity sets in three variants, according to the three methods
described in the textbook.
b) Describe some typical tasks that the company wishes to solve with the help of the
database, and some typical questions to which the company wishes to receive answers.
c) Which are the advantages and disadvantages with the different alternatives for the
relations for the Part entity sets, when you wish to answer the questions in question
b?
2. The E/R diagram below describes a web store, which sells DVDs and CDs (items) to
customers. Items may belong to several categories. Customers may write reviews of the
items that they have purchased.
Translate the E/R model into a relational model.
3. A medical health research project has a database containing data about all patients at a
hospital. For each patient, data about the symptoms that the patient shows is registered:
fever, headache, cough, chest pains, . . . Symptoms can have different severity: low, middle,
or high. A patient may show several symptoms, e.g., high fever, medium headache and
some cough.
4 Exercise 2 from E/R to relations, SQL
The database also contains data about diseases. Each disease is characterized by
different symptoms: a patient with a cold should have fever and a cough, a malaria patient
should have fever and fits of shivering, etc.
4. Shops sell items at varying prices. Customers buy items from shops. This is described by
the following relations:
FD1. AC
FD2. BD
FD3. AC D
FD4. CD E
FD5. EA
Determine all the keys of the relation.
FD1. D AC
FD2. AB
FD3. BC
a) What are the keys of the relation?
b) Show that the relation is not in BCNF and not in 3NF.
c) Decompose the relation into smaller relations that are in BCNF.
3. A company has several employees, all with different names, who perform interviews with
job applicants (one applicant is interviewed by one employee). The job applicants also have
different names. The interviewer makes appointments for interviews with the applicants.
Each applicant may be interviewed at several occasions, possibly by different interviewers,
but in that case the interviews take place during different days.
The company has special interview rooms. Each interviewer uses the same room for all
interviews during a day. A room may, however, be used by different interviewers during a
day, as long as the interviews dont collide in time.
The reservation of interview appointments is to be computerized. The database devel-
oper has decided to use a single relation for all data, with the following schema:
4. We wish to develop a database to keep track of persons, their children and their cars. For
this purpose, we will use the following relation:
pNbr, pName, pAddress is the person number, name and address of a person. cNbr,
cName, cAddress is the corresponding information for a child. Each person has exactly
one address. aLic, aMake is the license number and make of a car. A car may be owned by
more than one person.
b) Find the keys of the relation and show that the relation is not in BCNF.
c) Decompose the relation into relations that are in BCNF.
d) Show that one of the resulting relations violates the 4NF condition.
e) Decompose this relation into relations that are in 4NF.
f) Draw an E/R diagram that describes the problem and use this diagram to create
relations. Persons and children are considered to be different entity sets. Are the
resulting relations the same as the ones that you created in questions ?? and ???
Exercise 4 database design 7
1. A company organizes its activities in projects. Products that are used in the projects are
bought from suppliers. This is described in a database with the following schema:
2. Another company also organizes its activities in projects. Each project has a name and is
run at a specific location. Unfortunately, there are several projects with the same name
it may even be the case that two projects with the same name are run at the same location.
Each project has a telephone, which is shared if there is more than one project at the same
location. Each project has a boss, who is one of the employees of the company.
Each company employee has a person number, address and telephone number. Many
of the employees have several telephone numbers (office telephone, cellular 1, cellular 2,
. . . ). The employees work in the projects, most of them in more than one project.1 Each
such project employment starts at a specific date and ends at another date (the end date
of current project employments is unknown).
The survey involves a lot of people. Each person has a person number, name, and
address. Each square is investigated by one person.
Exercise 1 Solutions
1. We start by identifying candidates for entity sets, for example by writing down the nouns
and noun phrases in the requirements specification. We obtain the following list:
Calendar program, user, calendar, appointment, program, database, track, data, time,
day, text, reminder, kind, signal, computer, loudspeaker, pop-up window, description,
meeting, e-mail.
Not all of these candidate are suitable entity sets. We discard most of the candidates,
for the following reasons:
An obvious generalization is that there are three kinds of reminders: signal, pop-up
window, e-mail.
This results in the following E/R diagram:
The relationship has between Appointment and Reminder seems to express what we
wish, but note that it says that an appointment has the same reminder in all calendars. If an
appointment could have different reminders in different calendars, we can make Reminder
an association class on the relationship stores between Calendar and Appointment.
Note that this is a conceptual E/R model, not a database model. Before we can
transform the E/R model into a relational model we must study it closer and modify
it quite a lot. For instance, the relationship looks-at will only be necessary if it has
10 Exercise 1 Solutions
attributes, e.g., if users only have access to certain other calendars. The entity sets User
and Calendar are not complete they dont have any attributes, and we cannot form a
key for the entity sets. Probably, this doesnt mean that the entity sets are unnecessary,
but rather that we havent yet found any suitable attributes. Appointment also needs an
attribute that can be used as a key. Finally, we can note that Reminder, at least in its
present form, is a weak entity set.
2. We use the same procedure as in question ??. First, candidates for entity sets:
TV company, database, data, TV series, company, information, actor, series, director,
episode, occasion.
Discarded candidates:
Since each episode may be transmitted at several occasions we need an entity set to hold
the transmission time. This entity set we call Transmission.
We also need an entity set that describes the common features of actors and directors,
namely that they are employed by a TV company. We call this entity set Employee. Entity
sets Actor and Director will be subentities to Employee.
E/R diagram:
0..*
Director Actor
1
From the requirements specification we only obtain the attribute time in Transmission.
Other attributes have been invented.
Entity sets Episode and Transmission are weak. To form the key for Episode, number
alone does not suffice: you need to know the name of the TV series as well. Similarly for
Transmission: the time, episode number, and TV series name together form the key.
We can check that it is possible to answer the database queries using only the E/R
diagram. To check that we can answer the query Which actors play in the series Big
Sister? we check that there is a relationship between the involved entity sets, here Actor
and TV series.
We can also check more complex queries, for instance At what times can we see
the actor Bertil Bom?. By starting in Actor and following relationships we first reach
the TV series in which Bertil Bom participate, then the episodes of this series, then the
transmissions of the episodes, and there we find the times. (In the database this will
correspond to select statements with several relations, joins.)
Exercise 1 Solutions 11
3. E/R diagram:
<<weak>>
QueuePlace
entryDate
0..1
Parkingspace 1
<<weak>>
nbr 1 0..1 0..* 1 Apartment
Contract
hasElectricity nbr
date
monthlyRate 0..*
0..*
1
0..*
0..1 <<weak>> Owner
1
ProblemCar 1 0..* ProblemNote pNbr
licNbr date name
info address
Note especially the relationship parent-in. This relationship does not describe a biological
parenthood, only that the person is considered to be a parent in a household. This will
make it easy to handle people that are divorced and later form a new household with a
new set of children.
12 Exercise 2 Solutions
Exercise 2 Solutions
1. We start with the entity sets. There are two entity sets that must be converted into relations,
and where we dont have any alternatives for the implementation (primary keys are
underlined):
Products(productId, name)
Suppliers(supplierId, name, address)
The many-many relationships must also be converted into relations (foreign keys are in
italics):
ProductParts(productId, partId)
PartSuppliers(partId, supplierId, unitPrice)
When we are to translate the hierarchical structure, we have three alternatives: E/R, OO
and NULLS (see the course book). We get the following relations for the three alternatives:
Regardless of which alternative we use, we also must insert a new tuple in Product-
Parts.
Update the stock (qtyInStock) when a purchased part is delivered:
E/R: Find all parts from ManufacturedParts that are part of the product,
sum all manfCosts. Do the same for PurchasedParts, but sum all the
corresponding unitPrices from PartSuppliers.
OO: Same as E/R.
NULLS: Find all parts from Parts with typecode manufactured, sum the manf-
Costs. Then sum the unitPrices for the parts with type code purchased.
In this model, it is difficult to find types of questions that demand that you, starting from a
subclass, need to find the superclass to get information or vice versa. In such questions,
the E/R variant requires that you make a join on the subclass table and the superclass
table. (An invented example of such a question: Given the name of a manufactured part,
find the price of that part.)
An advantage of hierarchical modeling should be that it is easy to introduce new
subclasses. In this case it will probably not be relevant it is hard to imagine a third
category of parts that you do not either manufacture or purchase. If you do introduce a
third category, it will lead to substantial changes in the database schema or in the SQL
questions, regardless of which variant of translation you have used.
2. We choose to implement the many-one relationships by inserting the key from the one
side into the relation created from the entity set on the many side. Review is weak, but
the key {customerNbr, itemId, date} is too long, so we add a key reviewId.
Relations:
3. E/R diagram:
severity
Patient
Symptom Disease
pNbr 0..* 0..* 1..* 0..*
name name
name shows appears-in
description description
address
We have two many-many relationships, so if we merge all entity sets into one relation the
resulting relation will not be in 4NF. But we, of course, use the ordinary method to convert
the E/R diagram into relations, and we get the following relations:
All these relations are in 4NF the only functional dependencies are the key dependencies
and there are no multivalued dependencies.
select name
from Patients
where pNbr not in
(select patientNbr
from PatientSymptoms
where severity = high);
select name
from Patients, SymptomCount
where pNbr = patientNbr and
nbrSymptoms >= 2;
select patientNbr
from PatientSymptoms, MalariaSymptoms
where PatientSymptoms.symptomName = MalariaSymptoms.symptomName
group by patientNbr
having count(*) >= 1;
Patients that have all symptoms of malaria (the view MalariaSymptoms is used here, too):
Exercise 2 Solutions 15
select patientNbr
from PatientSymptoms, MalariaSymptoms
where PatientSymptoms.symptomName = MalariaSymptoms.symptomName
group by patientNbr
having count(*) = (select count(*) from MalariaSymptoms);
The last two queries give us person numbers instead of names. To find the names, you can
use the query as a subquery, e.g., like this:
select name
from Patients
where pNbr in
(select patientNbr
from PatientSymptoms, MalariaSymptoms
where PatientSymptoms.symptomName = MalariaSymptoms.symptomName
group by patientNbr
having count(*) = (select count(*) from MalariaSymptoms));
4. E/R diagram:
Customer Shop
1 0..* Sale 0..* 1
customerId shopId
saleId
name name
date
address address
0..*
0..*
price
1
Item
itemId
name 0..*
description
Exercise 3 Solutions
1. To find the keys we must calculate the closures of all subsets of all combinations of
attributes, i.e., { A}+ , { B}+ , . . . , { AB}+ , { AC }+ , . . . If the closure of a subset contains all
attributes of the relation, the subset is a key.
Observation that makes things easier in the future: since B does not appear on the
right-hand side of any of the functional dependencies, all keys must contain B. Thus, we
only need to consider subsets that contain B.
The functional dependencies were:
FD1. AC
FD2. BD
FD3. AC D
FD4. CD E
FD5. EA
FD2
{ B}+ { B} { BD }
2. To find the keys we do as in the previous assignment, i.e., calculate the closures of all
subsets of the set of attributes. But the attribute D doesnt appear on the right-hand side
of any of the dependencies, so it must be included in the key.
FD1. D AC
FD2. AB
FD3. BC
FD1 FD2
{ D }+ { DAC } { DACB}
The closure contains all attributes so { D } is a key. It is the also the only key: all sets of two
or more attributes that contain D are superkeys.
FD2 and FD3 violate the BCNF condition, since the left-hand sides (A, B) arent
superkeys. The relation isnt in 3NF, since the right-hand sides of FD2 and FD3 (B, C)
arent part of the key.
Exercise 3 Solutions 17
R1(A, B)
R2(A, C, D)
R1 is in BCNF, since it has two attributes. R2 isnt in BCNF, since it has the same key as R
and the dependency A C (obtained from FD2 and FD3 and the transitive rule). We split
R2 using A C:
R2a(A, C)
R2b(A, D)
Both these relations have only two attributes and are in BCNF.
3. Functional dependencies:
FD1 and FD2 contain all attributes so {applicant, day} and {day, time, room} are keys.
(You must also check that no subsets of these sets are keys. This is easy to do.) There is
one more key by computing closures you obtain:
The relation is not in BCNF because of FD3: {interviewer, day} is not a superkey. But the
relation is in 3NF, since the right-hand side of FD3, room, is part of a key.
Conversion to BCNF: take one of the functional dependencies that violate the BCNF
conditions. Here we have only one such dependency, FD3.
Create a new relation with the attributes that appear in FD3, and another relation with
all attributes except those on the right-hand side of FD3:
R1 states that an interviewer books one room for all interviews during one day and should
be called RoomBookings, for example. R2 states that an interviewer books times for
interviews with the applicants and should be called Interviews, for example.
RoomBookings has the key {interviewer, day} (from FD3). Interviews has the keys
{interviewer, day, time} (from FD4) and {applicant, day} (from FD1). Both relations are in
BCNF.
First draft of an E/R diagram:
18 Exercise 3 Solutions
Interview
Interviewer 1 0..* day 0..* 1 Applicant
name time name
room
Note that the only FD that is visible in the diagram is FD2, day time room interviewer
applicant. If you, according to the rules, transform the E/R diagram into relations, you
obtain the following relations:
Interviewer(name)
Applicant(name)
Interviews(interviewerName, applicantName, day, time, room)
That is, the same unnormalized relation Interviews as you started with! This shows that
even a good E/R model can result in unnormalized relations, if there are conditions (in
this case the functional dependencies) that are difficult to express in the E/R model.
You can, after a lot of thought, develop an E/R model where most of the functional
dependencies are expressed. Days and rooms must be entity sets, and it may look like this:
0..*
RoomBooking time
room
0..*
0..*
1 Day
day
The question is if it was worth the trouble. I dont think so: the first E/R diagram is easy
to understand, the second is very detailed and hard to understand. It is better that the E/R
model is easy to understand than that it is complete, especially since it is easy to normalize
relations afterwards.
4. We start with an example that shows that the relation contains redundant information:
Nils (person number 111) and Anna (person number 222) live in Lund and have a daughter,
Eva, and a car, ABC123:
If Nils and Anna get a son, Johan, and also buy a new car, DEF456, the relation will look
like this:
Exercise 3 Solutions 19
Here the same information is repeated several times, so the relation obviously isnt
normalized. It remains to prove this. We start by looking at functional dependencies. The
person number is used to identify persons, and the license number is used to identify cars,
so the following functional dependencies hold:
The relation has the key {pNbr, cNbr, aLic}, so all functional dependencies violate the
BCNF rules. If we decompose the relation to get BCNF relations we obtain, after several
steps:
The first three relations contain no redundant information, but the relation PersChildAuto
does. With the same data as in the example above, the relation instance looks like this:
As an example of redundancy, it is stated twice that person 111 has a child 333. This must
be so, since 111 owns two cars.
The relation PersChildAuto has no functional dependencies at all, so it is in BCNF. But
each person has a well-defined set of children and a well-defined set of cars, and these sets
are independent of each other. This is called a multivalued dependency.
The following multivalued dependencies hold (multivalued dependencies always occur
in pairs):
20 Exercise 3 Solutions
PersChildAuto is not in 4NF, since pNbr is not a superkey. We can decompose the relation
using one of the multivalued dependencies, in the same way as when we decompose
relations into BCNF. We get:
HasChildren(pNbr, cNbr)
OwnsAutos(pNbr, aLic)
If we develop an E/R model starting from the original formulation of the problem we get,
without much trouble, the following diagram:
Person Child
Automobile
0..* 0..* pNbr 0..* 0..* cNbr
aLic
pName cName
aMake
pAddress cAddress
Exercise 4 Solutions
1. Expressions in relational algebra:
a) Projects
b) city=0 London0 ( Projects)
c) supplNbr (projNbr=123 ( Deliveries))
d) prodNbr (city=0 London0 ( Deliveries ./ Suppliers))
e) L1.prodNbr,L2.prodNbr
(L1.prodNbr< L2.prodNbr ( L1 ( Deliveries) ./ L2 ( Deliveries))
L1.supplNbr = L2.supplNbr
is the renaming operator: L1 ( Deliveries) renames Deliveries to L1.
2. We develop this solution in several steps. We start by describing that there are employees
and projects, and that employees work in projects. Employments have start and end dates.
We have tried to include most of the attributes:
Employee Project
persNbr works-for name
0..* 0..* location
name
address telephoneNbr
telephoneNbr Employment
startDate
endDate
We havent expressed that each project has a boss. This is best expressed by a relationship
between Project and Employee. We dont need an entity set for the bosses according to
the text, they dont have additional properties compared to other employees.
With this relationship, the diagram looks like this:
boss-of
0..1 0..*
Employee Project
persNbr works-for name
0..* 0..* location
name
address telephoneNbr
telephoneNbr Employment
startDate
endDate
We allow a boss to manage more than one project, and we also allow projects without a
boss. The text says that each project should have a boss, but it seems reasonable that a
project, at least temporarily, stands without a boss.
The entity set Employee has telephoneNbr as an attribute. All attributes must be
simple, i.e., this model specifies that an employee has one telephone number. This wasnt
the case, according to the specification, and we must be able to describe that an employee
has many telephone numbers. The only way to achieve this is to introduce a new entity
set and a relationship with multiplicity many. We have done this in the following
diagram (since we now have an entity set describing telephones we also have removed the
telephone number from the project it is better to describe a thing in only one place):
22 Exercise 4 Solutions
boss-of
0..1 0..*
Employee Project
persNbr 0..* works-for 0..* name
name location
address 0..*
Employment
0..1
startDate
endDate
0..* Telephone 1
has-phone telephoneNbr has-phone
By using 0..1 and 0..* as multiplicities on the relationships from Telephone we have
tried to express that a telephone is owned by an employee or a project (or several projects).
It mustnt happen that the multiplicity is non-zero for both relationships at the same time.
(UML has notation to express such restrictions, but we havent learned this.)
Before translating the E/R model into relations we study the keys of the entity sets.
Employee has the key persNbr and Telephone has the key telephoneNbr. We cannot form
a key for Project, since several projects may have the same name. This could indicate that
Project is a weak entity set and that we should fetch the key from another entity set, but
that doesnt seem to be the case here we cannot reasonably use a person number or a
telephone number to identify a project. The only way out is to invent a key for projects.
We call the key projNbr, but we dont show the E/R diagram again.
The entity sets are translated into the following relations:
The relationship works-for has multiplicity many-many, so this must become a relation:
For the remaining relationships, with multiplicity 1 or 0..1, we have several options. We
start by looking at boss-of. One option is to create a relation for this relationship:
BossOf(projNbr, persNbr)
But since each project has exactly one boss, or maybe no boss, we can instead insert the
corresponding key into the relation on the many side, i.e., in Projects:
Telephones(persNbr, telephoneNbr)
We cannot put the project telephones in this relation, and it seems unnecessary to create a
separate relation for the relationship between project and telephone. Since each project
only has one telephone we can insert the telephone number in the project relation:
Exercise 4 Solutions 23
The relation Telephones now only contains data concerning employee telephones, and we
should change the name of the relation to, for example, EmployeeTelephones.
Are these relations normalized? Yes, the relations that only have key dependencies are,
but this is not the case for all of the relations. The relation:
has a functional dependency in addition to the key dependency. The text says that . . . has
a telephone, which is shared if there is more than one project at the same location. This
means that the location owns the telephone, not the project. In other words, we have
the functional dependency location telephoneNbr. Location is not a superkey, and the
relation isnt in BCNF. We normalize the relation by splitting it in two:
LocationTelephones(location, telephoneNbr)
Projects(projNbr, name, location, bossPersNbr)
Resulting relations:
3. E/R diagram:
Person
pNbr
name
address
1
Meaurement
0..* value
Site
<<weak>>
name MeasuredQuantity
1 0..* Square 0..* 0..*
type name
dx
xCoord unit
dy
yCoord
0..*
Observation
coverage
0..*
Plant
name
24 Exercise 4 Solutions
Square is a weak entity set, since the coordinates are relative to the coordinates of the site.
Relations from the entity sets (will be modified later):