cs186 Fa03 mt1
cs186 Fa03 mt1
cs186 Fa03 mt1
College of Engineering
Department of EECS, Computer Science Division
CS186 Eben Haber
Fall 2003 Midterm
Midterm Exam: Introduction to Database Systems
This exam has seven problems, worth different amounts of points each. Each problem is
made up of multiple questions. You should read through the exam quickly and plan your
time-management accordingly. Before beginning to answer a question, be sure to read it
carefully and to answer all parts of every question!
Good luck!
CS 186 – Fall 2003 Login _____________
Primary keys are underlined. Note that the driver involved in a car accident may not
always be the owner of the car. Assume that accident_date is of type integer, and
represents a year (e.g. 1980). Year is also of type integer. We assume that a car cannot
get involved in more than one accident at a certain date.
1. (4 points) Find the SSN of every person who owns one or more cars, none of which
has ever been involved in a car accident.
A. SELECT O.SSN
FROM Owns O
WHERE O.license NOT IN (SELECT A.license
FROM Accident A)
2. (4 points) Find the SSN of every person, who owns a TOYOTA or a DODGE
A. SELECT O.SSN
FROM Owns O, Car C
WHERE O.license=C.license AND
(C.model=’TOYOTA’ OR
C.model=’DODGE’)
3. (4 points) Find the SSN of all persons who have had all of their cars involved in an
accident.
A. SELECT O.SSN
FROM Owns O, Accident A
WHERE O.license=A.license
EXCEPT
SELECT O.SSN
FROM Owns O
WHERE O.license NOT IN (SELECT A.licens
FROM Accident A)
4. (4 points) Who is the driver who participated in the most costly accident? Return the
driver and the amount of damage.
A. SELECT driver, damage_amount
FROM Accident
WHERE damage_amount IN (SELECT MAX(damage_amount)
FROM Accident)
B. SELECT driver, damage_amount
FROM Accident
WHERE damage_amount=MAX(damage_amount)
5. (4 points) Find the license number of all cars that had an accident in less than 5 years
after their production. Return the license number and the number of years
A. SELECT c.license
FROM Car c, Accident a
WHERE a.accident_date-c.year<5
CS 186 – Fall 2003 Login _____________
6. (4 points) Find the accident with the smallest cost and return the corresponding owner
of the car, his/her address and the amount of damage.
A. SELECT O.SSN, P.address, MIN(A.damage_amount)
FROM Accident A, Owns O, Person P
WHERE O.license=A.license AND O.SSN=P.SSN
GROUP BY O.SSN, P.address
7. (4 points) Find the license number of all cars that have been involved in more than one
accident. (DO NOT RETURN DUPLICATES)
A. SELECT A1.license
FROM Accident A1, Accident A2
WHERE A1.license=A2.license AND A1.accident_date<>a2.accident_date
8. (2 points) Explain, in English, what the following Relational Calculus query expresses:
CS 186 – Fall 2003 Login _____________
2. (2 points) If you have 100 pages of data, and 10 pages of memory, what is the
minimum number of passes required to sort the data.
3. (2 points) If you have 500,000 pages of data, what is the minimum number of pages of
memory required to sort the data in 3 passes?
CS 186 – Fall 2003 Login _____________
.
III. Join Algorithms (6 points)
Consider a relation R with attributes (x,y) and a relation S with attributes (y, z). Column
y in S is a key and the set of values of y in R are the same as the set of values of y in S.
Assume that there are no indexes available and that there are 25 pages in the buffer
available. Table R is 1,500 pages with 50 tuples per page. Table S is 400 with 100 tuples
per page.
Compute the I/O costs for the following joins. Assume the simplest cost model, where
pages are read and written one at a time.
A. Block nested loops join with R as the outer relation and S as the inner relation
B. Block nested loops join with S as the outer relation and R as the inner relation
C. Sort merge join with R as the outer relation and S as the inner relation
D. Sort merge join with S as the outer relation and R as the inner relation
E. Hash join with S as the outer relation and R as the inner relation
CS 186 – Fall 2003 Login _____________
IV Query Optimization
(a) Assuming uniform distribution of values and column independence, estimate the
number of tuples returned by this query.
(c) For the first join in each query plan (the one at the bottom of the tree, what join
algorithm would work best? Assume that you have 50 pages of memory. There are no
indexes, so indexed nested loops is not an option.
CS 186 – Fall 2003 Login _____________
In this exercise, you will compare the LRU, MRU, and CLOCK buffer management
algorithms for a given workload (just as you did in the first part of homework 1). Here
are the assumptions you must make:
• Assume there are four page slots your buffer manager must manage: P1, P2, P3,
and P4.
• All four slots are empty to start.
• When the buffer pool has unused slots (such as at the beginning, when all four
slots are empty), it will put newly read data in the leftmost empty slot (e.g., if
slots 2 and 3 are free, it will use slot 2).
• The pages to be read from disk are labelled A through G.
• For each access the page is pinned, and then immediately unpinned.
Below are three tables for describing the contents of the buffer pool at each time step. A
page is read at the beginning of each time step. You should record, in the table, the
contents of each Buffer Page after the new page has been read in.
CS 186 – Fall 2003 Login _____________
b) (10 points) Using SQL, convert the following ER diagram to the relational model.
Hint: You do not need CHECK constraints.