0% found this document useful (0 votes)
24 views

HW 2 Sol

This document contains instructions for Homework 2 for the Database Management Systems course COP 5725. It includes questions about advanced SQL, assertions, triggers, and disk management. The SQL questions involve queries on a database of World War II capital ships, including finding launch years of ship classes, countries of classes with sunken ships, and numbers of sunken ships by class. The assertions and triggers questions involve enforcing constraints in a database for a bar, such as drinker tolerance and total drink power consumed. The disk management questions calculate maximum rotational delay and data transfer rate for a hard disk configuration.

Uploaded by

Chuang James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

HW 2 Sol

This document contains instructions for Homework 2 for the Database Management Systems course COP 5725. It includes questions about advanced SQL, assertions, triggers, and disk management. The SQL questions involve queries on a database of World War II capital ships, including finding launch years of ship classes, countries of classes with sunken ships, and numbers of sunken ships by class. The assertions and triggers questions involve enforcing constraints in a database for a bar, such as drinker tolerance and total drink power consumed. The disk management questions calculate maximum rotational delay and data transfer rate for a hard disk configuration.

Uploaded by

Chuang James
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Database Management Systems (COP 5725)

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:

Pledge(Must be signed according to UF Honor Code)


On my honor, I have neither given nor received unauthorized aid in doing this assignment.

Signature

For grading use only:

Question: I II III IV Total


Points: 25 15 20 15 75
Score:

i
COP5725, Fall 2013 Homework 2 Page 1 of 6

I. [25 points] Advanced SQL.


Consider the following database concerning World War II capital ships:
Classes(class, type, country, numGuns, bore, displacement)
Ships(name, class, launched)
Battles(name, date)
Outcomes(ship, battle, result)

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:

SELECT C.class, MIN(S.launched) AS First_Launched


FROM Classes C, Ships S
WHERE C.class = S.class
GROUP BY C.class
ORDER BY First_Launched DESC;

(2) [5 points] Find the countries of the classes with at least one ship sunk in a battle.

Solution:

SELECT DISTINCT C.country


FROM Classes C, Ships S
WHERE C.class = S.class
AND S.name IN (
SELECT ship
FROM Outcomes O
WHERE O.result= sunk );

(3) [5 points] For each class with at least three ships, find the number of ships of that
class sunk in battle.
Solution:

SELECT M.class, COUNT(result) FROM


(SELECT class, name
COP5725, Fall 2013 Homework 2 Page 2 of 6

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);

Write the following database modifications:


(5) [2 points] Modify the Classes relation so that gun bores are measured in centime-
ters (1 inch = 2.5 centimeters) and displacements are measured in metric tons (1
metric ton = 1.1 tons).
Solution:

UPDATE Classes
SET bore = 2.5 * bore,
displacement = displacement/1.1;

(6) [3 points] Delete the classes with no ships.


Solution:

DELETE FROM Classes


WHERE NOT EXISTS (
SELECT * FROM Ships
WHERE Ships.class = Classes.class
);

II. [15 points] Assertions and Triggers.


COP5725, Fall 2013 Homework 2 Page 3 of 6

(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

CREATE ASSERTION Check_Tolerance CHECK (


NOT EXISTS (
SELECT * FROM DRINKER
WHERE TOLERANCE IS NULL OR TOLERANCE < 0 OR
TOLERANCE > 10
)
);

Version 2

CREATE ASSERTION Check_Tolerance CHECK (


ALL (SELECT TOLERANCE FROM DRINKER) >= 0
AND
ALL (SELECT TOLERANCE FROM DRINKER) <= 10
);

Version 3

CREATE ASSERTION Check_Tolerance CHECK (


(SELECT MIN(TOLERANCE) FROM DRINKER) >= 0$
AND
(SELECT MAX(TOLERANCE) FROM DRINKER) <= 10$
);
COP5725, Fall 2013 Homework 2 Page 4 of 6

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:

CREATE TRIGGER Cab_Trigger


AFTER INSERT ON DRINKS
REFERENCING NEW ROW AS NEWDRINK
FOR EACH ROW
WHEN (
(SELECT TOLERANCE FROM DRINKERS WHERE
NAME=NEWDRINK.DRINKER_NAME) <
(SELECT SUM(POWER) FROM DRINKS WHERE
DIRNKER_NAME=NEWDRINK.DRINKER_NAME)
)
ROLLBACK;

III. [20 points] Disk Management.


Consider a disk with a sector size of 512 bytes, 2000 tracks per surface, 50 sectors per
track, five double-sided platters, and average seek time of 10 msec. Suppose a block size
of 1024 bytes is chosen. A file containing 100k records of 100 bytes each is to be stored
on such a disk and that no record is allowed to span two blocks.
(1) [5 points] If the disk platters rotate 5400 rpm, what is the maximum rotational
delay?
Solution: If the disk platters rotate at 5400rpm, the time required for one complete
rotation, which is the maximum rotational delay, is
1
× 60 = 0.011s.
5400
The average rotational delay is half of the rotation time, 0.006s.
Note: The definition of maximum rotational delay is the time it takes to do a full rotation
excluding any spin-up time. If you use half rotation time according to the slides, we will
not deduct points.
(2) [5 points] If one track of data can be transferred per revolution, what is the trans-
fer rate?
Solution: The capacity of a track is 25K bytes. Since one track of data can be transferred
per revolution, the data transfer rate is
25K
= 2, 250KB/s.
0.011
COP5725, Fall 2013 Homework 2 Page 5 of 6

(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.

seek time = 10ms


rotational delay = 6ms
1K
transfer time = = 0.44ms
2, 250K/s

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.

IV. [15 points] Buffer Management.


Suppose you have a Buffer Pool that can hold 3 pages. There are 26 blocks on your disk;
for simplicity we will refer to each block with a letter of the English alphabet between
A and Z. An “access pattern” is a string of letters that log the requests to the Buffer
Pool for pages. For each access pattern below, figure out the number of I/Os that would
occur starting with an empty buffer pool each time, for different replacement policies.
(Spaces in the access patterns are there just for legibility.)
(1) [5 points] For access pattern ZYXW ZYXW, what are the numbers of I/Os that
would occure for FIFO, LRU and MRU. Explain how you get the resutls.
Solution: FIFO 8, LRU 8, MRU 5
(2) [5 points] For access pattern ABCD DCBA ABCD, what are the numbers of I/Os
that would occure for FIFO and LRU. Explain how you get the resutls.
Solution: FIFO 8, LRU 6
(3) [5 points] For access pattern ACEG BDFH ACEG BDFH, what are the numbers
of I/Os that would occure for LRU and MRU. Explain how you get the resutls.
COP5725, Fall 2013 Homework 2 Page 6 of 6

Solution: LRU 16, MRU 13

You might also like