HW 2 Sol
HW 2 Sol
Homework 2
Instructor: Dr. Daisy Zhe Wang
TAs:
Yang Chen, Kun Li, Yang Peng
yang, kli, [email protected] l.edu
October 8, 2013
Name:
UFID:
Email Address:
Signature
i
COP5725, Fall 2013 Homework 2 Page 1 of 6
Ships are built in “classes” from the same design, and the class is usually named for the
first ship of that class. The relation Classes records the name of the class, the type (‘bb’
for battleship of ‘bc’ for battlecruiser), the country that built the ship, the number of
main guns, the bore (diameter of the gun barrel, in inches) of the main guns, and the
displacement (weight, in tons). Relation Ships records the name of the ship, the name of
its class, and the year in which the ship was launched. Relation Battles gives the name
and date of battles involving these ships, and relation Outcomes gives the result (sunk,
damaged, or ok) for each ship in each battle.
Write a SQL query for the following:
(1) [5 points] For each class, find the year in which the first ship of that class was
launched. Sort the results by launch years in decreasing order.
Solution:
(2) [5 points] Find the countries of the classes with at least one ship sunk in a battle.
Solution:
(3) [5 points] For each class with at least three ships, find the number of ships of that
class sunk in battle.
Solution:
FROM Ships
WHERE class IN (
SELECT class FROM Ships
GROUP BY class
HAVING COUNT(*) >= 3)
) M
LEFT JOIN
(SELECT ship, result
FROM Outcomes
WHERE result = ’sunk’) S ON M.name = S.ship
GROUP BY M.class
(4) [5 points] Find the names of the ships whose number of guns was the largest for
those ships of the same bore.
Solution:
SELECT S.name
FROM Ships S, Classes C
WHERE S.Class = C.Class
AND numGuns >= ALL (SELECT numGuns
FROM Classes C2
WHERE C2.bore = C.bore);
UPDATE Classes
SET bore = 2.5 * bore,
displacement = displacement/1.1;
(1) [5 points] Imagine you are running a massive database schema (such as EBAYs
server) with thousands of tables and millions of transactions per day.
Is it a good idea to use ASSERTIONS? Whether you chose YES or NO, please
explain why, briefly.
Solution:
As databases get bigger, it becomes inefficient to use assertions and it essentially slows
down the system. It is quite difficult to automatically figure out an efficient way to
check only the tables that are being affected by specific queries, so DBMS usually ends
up checking all the tables and this is simply not practical when you have millions of
tables in your database system.
(2) You own a bar and you have the following two tables to manage your database:
1.Drinkers(name, tolerance)
2.Drinks(drink name, drinker name, drink power)
In table Drinkers, the tolerance is used to indicate the drinking tolerance of any
drinker. In table Drinks, drink power is used to indicate the strength of the
Drink. Both tolerance and drink power are integers. When a drinker orders a
drink with certain power value, an entry about this action will be added to the
Drinks table.
i. [5 points] We need to gurantee that the tolerance of any drinker is inside the
closed interval [0, 10]. Use ASSERTION to enforce this constraint.
Solution:
Version 1
Version 2
Version 3
ii. [5 points] When someone wants to order a drink, we need to check after this
entry inserted into Drinks table, whether the sum of Drinks.drink power for
that drinker stay below the drinker’s tolerance value. If the tolerance is less
than the the sum of drink power, we should cancel this transaction. Use a
TRIGGER to enforce this constraint. (Hint: Use ROLLBACK to cancel the
transaction)
Solution:
(3) [5 points] What is the time required to read the file sequentially? What if the
disk were capable of reading/writing from all heads in parallel?
Solution: A file containing 100,000 records of 100 bytes needs 40 cylinders or 400 tracks
in this disk. The transfer time of one track of data is 0.011 seconds. Then it takes
400 × 0.011=4.4 seconds to transfer 400 tracks.
This access seeks the track 40 times. The seek time is 40 × 0.01=0.4 seconds. Therefore,
total access time is 4.4+0.4=4.8 seconds.
If the disk were capable of reading/writing from all heads in parallel, the disk can read
10 tracks at a time. The transfer time is 10 times less, which is 0.44 seconds. Thus total
access time is 0.44+0.4=0.84 seconds.
(4) [5 points] What is the time required to read each block in the file in a random or-
der? (assuming that each block request incurs the average seek time and rotational
delay)
Solution: For any block of data, average access time = seek time+rotational delay+transfer
time.
The average access time for a block of data would be 16.44ms. For a file containing 100k
records of 100 bytes, the total access time would be 164.4 seconds.