Sample Midterm
Sample Midterm
Sample Midterm
• We have a set of teams, each team has an ID (unique identifier), name, main stadium, and to
which city this team belongs.
• Each team has many players, and each player belongs to one team. Each player has a number
(unique identifier), name, DoB, start year, and shirt number that he uses.
• Teams play matches, in each match there is a host team and a guest team. The match takes place
in the stadium of the host team.
• For each match we need to keep track of the following:
o The date on which the game is played
o The final result of the match
o The players participated in the match. For each player, how many goals he scored,
whether or not he took yellow card, and whether or not he took red card.
o During the match, one player may substitute another player. We want to capture this
substitution and the time at which it took place.
• Each match has exactly three referees. For each referee we have an ID (unique identifier),
name, DoB, years of experience. One referee is the main referee and the other two are
assistant referee.
Design an ER diagram to capture the above requirements. State any assumptions you have that
affects your design (use the back of the page if needed). Make sure cardinalities and primary
keys are clear.
1
ID
ExpYears
DoB
Referee
name
(3…3)
isMain
stadium
role
ID
city
host
date
Match
Team
name
Host-‐score
guest
guest-‐score
belongs
inMatch
numGoals ID
Player
DoB
Match-‐Player
RedFlag
num plays
StartYear
name
YellowFlag
shirtNum
Sub
time
Assumptions:
1-‐ In
Match-‐Player
entity
set,
we
added
a
unique
identifier
for
each
record
ID.
2-‐ The
final
result
in
Match
entity
set
is
captured
using
two
attributes
Host-‐score
and
guest-‐score
3-‐ The
attribute
‘isMain’
in
relationship
‘role’
is
true
if
the
referee
is
the
main
referee
in
the
match,
otherwise,
it
w ill
be
false.
2
Question
2
(Relational
Model):
Map the ERD in Question 1 to create the relational model corresponding to the described
application. Basically, list the CREATE TABLE statements with the attribute names, and
appropriate data types. Also make sure to have the primary keys and foreign keys clearly
defined (use the back of the page if needed).
** In your exam, I will not ask for Create Table statements, I will ask only for R(A1, A2, …An)
format.
3
Question
3
(Relational
Algebra):
Consider the following relations:
Doctor(SSN, FirstName, LastName, Specialty, YearsOfExperience, PhoneNum)
Patient(SSN, FirstName, LastName, Address, DOB, PrimaryDoctor_SSN)
Medicine(TradeName, UnitPrice, GenericFlag)
Prescription(Id, Date, Doctor_SSN, Patient_SSN)
Prescription_Medicine(Prescription Id, TradeName, NumOfUnits)
• The Doctor relation has attributes Social Security Number (SSN), first and last names, specialty, the
number of experience years, and the phone number.
• The Patient relation has attributes SSN, first and last names, address, date of birth (DOB), and the
SSN of the patientʼs primary doctor.
• The Medicine relation has attributes trade name, unit price, and whether or not the medicine is
generic (True or False).
• The Prescription relation has attributes the prescription id, the date in which the prescription is
written, the SSN of the doctor who wrote the prescription, and the SSN of the patient to whom the
prescription is written.
• The Prescription_Medicine relation stores the medicines written in each prescription along with
their quantities (number of units).
Write the relational algebra expressions for the following queries (consider the three
performance/optimization rules taken in class)
1. List the trade name of generic medicine with unit price less than $50.
4
2. List the first and last name of patients whose primary doctor named ʻJohn Smithʼ.
3. List the first and last name of doctors who are not primary doctors to any patient.
5
4. For medicines written in more than 20 prescriptions, report the trade name and the total
number of units prescribed.
5. List the SSN of patients who have ʻAspirinʼ and ʻVitaminʼ trade names in one
prescription.
6
6. List the SNN of distinct patients who have ʻAspirinʼ prescribed to them by doctor named
ʻJohn Smithʼ.
R2 !!ID!Prescription_id("TradeName=‘Aspirin’(Prescription_Medicine)) # R1
7. List the first and last name of patients who have no prescriptions written by doctors other
than their primary doctors.
R2 !!SSN(Patient) - R1
7