Exam 1 Sol
Exam 1 Sol
Exam 1 Sol
University of Florida
Exam I Solutions
friends
writes
email times
name text
tags
(2) [8 points] Translate the ER diagram into relational database tables. Make sure
that the translation captures any key and participation constraints in the ER dia-
gram.
Solution:
(3) [5 points] Based on your schema in (2), how would you disallow a user posting
on non-friends’ profile pages? Write down the SQL statement(s) you use to enforce
this constraint.
Solution: Assume Friends is symmetric, i.e., (x, y) are friends ⇒ (y, x) are friends.
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 relational algebra expressions to answer the following queries:
COP5725, Fall 2013 Exam I Page 3 of 7
(1) [5 points] Find the names and ages of ships that sunk in battles.
Solution:
(2) [5 points] Find the countries that had both battleships and battlecruisers.
Solution:
(3) [5 points] Find the year in which the last ship of class “Tennessee” was launched.
Solution:
R1 ← σclass=‘Tennesee’ (Ships)
R2 ← ρR2 (R1 )
Ans ← πlaunched (Ships) − πR1 .launched (R1 ./R1 .launched<R2 .launched R2 )
Solution:
R1 ←πclass (Classes)−
πclass (σresult6=‘ok’ (Ships ./name=ship Outcomes)).
Ans ←πclass,type,country (R1 ./ Classes)
(5) [8 points] Consider three database relations (sets) R, S, T . Tell whether each of
the following is true or not. Give a counter-example if the law does not hold. (Hint:
./ stands for the full outer join.)
A. (R ./ S) ./ T = R ./ (S ./ T )
COP5725, Fall 2013 Exam I Page 4 of 7
The P roduct relation gives the manufacturer, model number and type (PC, laptop,
or printer) of various products. We assume for convenience that model numbers are
unique over all manufacturers and product types. The P C relation gives for each model
number that is a PC the speed (of the processor, in gigahertz), the amount of RAM(in
megabytes), the size of the hard disk (in gigabytes), the speed and type of the removable
disk (CD or DVD), and the price. The Laptop relation is similar, except that the screen
size (in inches) is recorded in place of information about the removable disk. The P rinter
relation records for each printer model whether the printer produces color output (true
if so), the process type (laser, ink-jet, or bubble), and the price.
Write a SQL statement for the following questions:
(1) [5 points] Find the manufacturers that make at least three different models of PC.
Solution:
SELECT maker
FROM Product
WHERE type=’pc’
GROUP BY maker
HAVING COUNT(model) >= 3;
(2) [5 points] Find the average hard disk size of a PC for all those manufacturers that
make printers.
Solution:
(3) [5 points] Find the model number of the item (PC, laptop, or printer) with the
highest price.
Solution:
(4) [5 points] Delete all laptops made by a manufacturer that doesn’t make printers.
(5) [8 points] When inserting a new printer, check that the model number exists in
Product. Reject the printers with no corresponding models.
Solution:
Solution: 3/200 or 1.5%. Only 100, 99, and 98 are hit, one each.