You are on page 1of 6

University of Sussex

Informatics

Autumn 2014

Databases
Assignment 2 (Deadline 4.12.14, 4pm)
The coursework assigned below should be submitted electronically on our
StudyDirect site by 4 pm on Thursday, 4th of December 2014. You have
to answer all questions. Your answers must be submitted online in one
(ASCII) file named after your candidate number and with suffix .sql. You
must use the template file provided on the StyDirect page that provides necessary comments for automatic testing (simply download and rename it).
Do not remove any line from this template. Each question requires exactly
one SQL statement (or procedure definition). DO NOT add extra code as
this will modify the behaviour of the testing routines. Your submission must
only contain the code you wrote yourself (not the code that has been provided). Format your code so that it is readable (this means in particular
avoid putting long queries on one line).
YOU MUST WORK ON THE ASSIGNMENT ON YOUR OWN! The standard rules for collusion and plagiarism apply and any cases discovered
will be reported and investigated. Furthermore read the instructions below
carefully as your submission must conform to testing.
Detailed Instructions you must follow carefully
This assignment refers to an implementation of the motor sport database as
designed in the the first assignment (see StudyDirect). For this second assignment you must first run the SQL script a2-setup.sql available from
our StudyDirect site. This defines the tables that your code will rely on.
Load the file into HeidiSQL with the help of the feature Tools-->Load
SQL file. It is important that you inspect the code in this script and
understand how it implements the requirements outlined in the first assignment. Do not modify the structure of the tables in the given script when you
write your answers unless the questions ask you to do so.
Just a few rows of data have been inserted into the tables. It is recommended
that you test your code with additional/different sample data. However,
do not include any insert statements in your submission (unless explicitly
asked for) as this will confuse the testing scripts. Please note that your code
will be tested not only on the small data set provided but on other data sets
as well and you only receive full marks if your query runs correctly on all
1

data sets. If it only works on the data provided you might not get all marks
available.
In the following, SQL always refers to the MySQL 5.6 dialect and all your
code must run on our ITS server where it will be tested for marking purposes. Make sure you get the expected results on our ITS server. If you
test it on other servers (that you install yourself for instance) you might get
different behaviour. Do not hand in files in .doc or .pdf format as they cant
be run in SQL and will be awarded 0 marks. Make sure you fix any syntax
errors before you submit. Format your code properly so that it is readable.
Queries with syntax errors will receive 0 marks.
Do not include any parts of the code of a2-setup.sql in your answer.
For every question you answer, write your SQL statement after the comment
that contains the corresponding question number in the template. You must
not remove any of those comments as they will guide the testing procedure.
If you do not provide an answer for a question leave the corresponding space
blank.
You must not deviate from the requested order and name of the columns in
queries. Note that for every query the names and order of columns is clearly
specified in the paper. Any change will most likely confuse the testing script
and cost you some marks.
1. Write SQL code to set up table MoSpo HallOfFame according to the
following Relational Schema:
MoSpo HallOfFame(hoFdriverName,hoFdriverDOB,hoFYear,hoFSeries,
hoFWins,hofPodiums)
primary key (hoFdriverName,hoFdriverDOB,hoFYear)
foreign key (hoFdriverDOB,hoFdriverName) references
MoSpo Driver(driverDOB,driverLastName)
Your code must execute without syntax error and without runtime error assuming that all other tables have been set up by running script
a2-setup.sql which you must not include. The data types (domains) you choose for the columns should be most appropriate for the
data they will contain. You must however accommodate the following
requirements:
(a) For table and column names you must pick exactly the names
used in the schema above (otherwise you will lose marks as tests
2

(b)
(c)

(d)
(e)
(f)

will fail).
hoFYear is a 4-digit number representing a year between 1000
and 9999.
hoFSeries is one of the following strings: Formula1, FormulaE, BritishGT, SuperGT. Please make sure you use the same
spelling. Values for this this attribute must not be missing.
hoFWins and hoFPodiums are positive numbers and never larger
than 99. The default is always 0.
Where applicable, equip any foreign constraints with constraint
names of your choosing.
Ensure that if a driver is deleted from the database their corresponding hall of fame entries are deleted automatically too.
[13 marks]

Instructions
For each of the tasks specified below write one single SQL query,
respectively, that solves the task. You can use nested queries (ie. subselects and subqueries) wherever you like. You must not create any
tables of any form and you must not use (nor declare) any stored procedures or functions. You must produce column headings as specified
with each query. Do not change order or name of the columns as this
will cause tests to fail which will cost you marks. Double check that
they are exactly as specified. It is important that your queries will
work correctly with any data (according to the schema). All references to time, when not explicit, are relative and refer to the time of
running the query.
2. The weight of drivers has been omitted from the MoSpo Driver table.
Without deleting and recreating the table, add a column driverWeight
to the table that allows values to be missing. Take into consideration
that a drivers weight can never be more than 99.9.
[6 marks]
3. Change the location of the racing team Beechdean Motorsport to (the
following string including the white space) High Wycombe. [6 marks]
4. Remove the drivers with last name Senna from the database. [6 marks]
5. Find out how many racing teams are on the database. The headings
must look like this:
3

numberRacingTeams
[4 marks]
6. List all racing drivers (first name, last name, and dob) born in July.
The headings must look like this:
firstName lastName DOB
[4 marks]
7. List all races (name and date, but not the time) that have been won by
a car that started on a medium tyre. Order by date (latest race first).
The headings must look like this:
raceName

raceDate

Note that a won race is indicated when racesInWon has value 1 (representing true).
[6 marks]
8. List all racing teams (names only) with all their drivers (first name and
last name together separated by a a blank space, e.g. Lewis Hamilton)
and their current age in two digit years (e.g. 19). Order alphabetical
by race team and within a race team order alphabetically by driver last
name. For the computation of the age you do not have to consider leap
years. The headings must look like this:
team driverName driverAge
[6 marks]
9. Find out all the different engine types (makes) of cars that had to retire
in a race this year. The heading must look like this:
carMake
Note that a retirement is indicated when lapInfoRetired has value 1
(representing true).
[6 marks]
10. How many races has a driver called Lewis Hamilton won? The headings must look like this:
4

Wins
[6 marks]
11. List all drivers (last name, dob, and nationality) who had no retirement
ever. The headings must look like this:
driverLastName driverDOB driverNationality
[8 marks]
12. List all driver last names and races for which the following is true: the
given driver made three or more pitstops in the given race. For the
race just provide the name and the 4-digit year in which it took place.
The headings must look like this:
driverLastName raceName raceYear
[8 marks]
13. List the car engine make (or makes) that suffered the most retirements
overall. The make(s) and its number of retirements must be included
in the result table. The headings must look like this:
engineMake numberRetirements
[9 marks]
Instructions
For the creation of stored procedures, you need to change your delimiter. Note that in the submission template this has already been done
for you and set to $$ which you must use to terminate your procedure
definition. Do not remove any of the lines in the template. Strictly
name your procedure as indicated in the question. You are not allowed to include or use any stored routine declaration other than the
one asked for. Note that syntax correct routines can still throw runtime errors. But if your answer throws any runtime errors caused by
references to non-existing tables or attributes it will receive 0 or low
marks. So please test your routine before submission.

14. Write a stored function totalRaceTime that, given a drivers last


name, a drivers date of birth, the name of a race, and the date & time
of a race (as one argument), returns the total race time for the given
driver in the given race. If the given driver does not exist, the routine
should throw the error Driver. If then the given race does not exist,
the routine should throw the error Race. The error handling should
be implemented as explained in Lecture 17 meaning the SQL error
message shown will automatically have a does not exist following
the name of the thrown error. In the case that not all lap times for the
given race are available, the routine should throw the error some lap
time. If the race was not completed by the driver due to retirement
from the race, the routine must return null.
Note that this stored routine has four arguments and you must declare
them in the order given above.
[12 marks]

You might also like