Database Systems - Introduction to Database Systems - Solved Exams, Exams of Introduction to Database Management Systems

Main points of this past exam are: Database Systems, Database Tables, Relational Calculus, Participated, Costly Accident, License Number, Smallest Cost, Corresponding Owner, Relational Calculus, External Sorting Algorithm

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shalaby_88cop
shalaby_88cop 🇮🇳

4.3

(15)

63 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Answer Key
UNIVERSITY OF CALIFORNIA
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!
I /24
II /10
III /6
IV /10
V /10
VI /20
VII /20
Total /100
Please write your login at the top of every page; you must turn in all the pages of the exam. Do not
remove pages from the stapled exam! Two pages of extra answer space have been provided at the back in
case you run out of space while answering. If you run out of space, be sure to make a “forward reference”
to the page number where your answer continues.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Database Systems - Introduction to Database Systems - Solved Exams and more Exams Introduction to Database Management Systems in PDF only on Docsity!

Answer Key

UNIVERSITY OF CALIFORNIA

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!

I /
II /
III /
IV /
V /
VI /
VII /

Total /

Please write your login at the top of every page; you must turn in all the pages of the exam. Do not remove pages from the stapled exam! Two pages of extra answer space have been provided at the back in case you run out of space while answering. If you run out of space, be sure to make a “forward reference” to the page number where your answer continues.

I. Query Languages (30 points).

Given the following database tables, choose all queries in SQL, Relational Algebra or Tuple Relational Calculus that return the proper answer to the corresponding question. Note that each question may have more than one correct answers. Circle all that apply.

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.

Person(SSN, name, address) Car(license, year, model) Accident(license, accident_date, driver, damage_amount) Owns(SSN, license)

  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)

B. π^ SSN ( Owns^^ )^ −^ π SSN ( Owns^ >< Accident )

C. { O | ∃ O 1 ∈ Owns ( O. SSN = O 1. SSN ∧¬∃ AAccident ( O 1. license = A. license ))}

D. None of the above

B is right. A and C return the SSN of every person that has at least one car that is not involved in an accident, while the question asks for the people for whom all of their cars have never been involved in an accident.

  1. (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’)

B. π^ SSN (^ Owns^ ><σmod el =' TOYOTA '∨mod el =' DODGE '( Car^ ))

C.

( 1.. (. ' '. ' ')))}

C CarO license Clicense Cmodel TOYOTA Cmodel DODGE

O O OwnsO SSN OSSN

D. None of the above

A, B and C are right.

C. { C | ∃ C 1 ∈ Car ( C. license = C 1. license ∧∃ AAccident ( C 1. accident _ dateA. year < 5 ))}

D. None of the above

D is right. None of the queries return the number of years.

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

B. SELECT O.SSN, P.address, (SELECT MIN(damage_amount) FROM Accident) FROM Owns O, Person P, Accident A WHERE O.SSN=P.SSN AND A.license=O.license

C. SELECT O.SSN, P.address, A.damage_amount FROM Owns O, Person P, Accident A WHERE O.SSN=P.SSN AND A.damage_amount=(SELECT MIN(damage_amount) FROM Accident) D. None of the above.

The answer is D. A returns the cheaper accident for each owner, B returns every owner involved in an accident, along with the minimum value of damage, and C doesn’t work because it doesn’t perform the join between Accident and the other relations.

  1. (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 A WHERE A1.license=A2.license AND A1.accident_date<>a2.accident_date

B. SELECT DISTINCT A1.license FROM Accident A WHERE A1.license IN (SELECT A2.license FROM Accident A WHERE A1.accident_date<>A2.accident_date)

C. SELECT license FROM Accident GROUP BY license HAVING COUNT(accident_date)>

D. None of the above

B and C are right. A returns duplicates.

  1. (2 points) Explain, in English, what the following Relational Calculus query expresses:

A AccidentAlicence C license

C model C model

C C Car C model Cmodel C Car

“Return the model of those cars, for which all cars of that model have been involved in an accident” (e.g. If every car of model ‘TOYOTA’ has been involved in an accident, the query must return the model ‘TOYOTA’. If there is one ‘TOYOTA’ car not involved in an accident, ‘TOYOTA’ should NOT be returned by the query)

II. External Sorting (10 points)

  1. (6 points) Suppose we wish to sort the following values:

Assume that:

  • you have three pages of memory for sorting
  • you will use an external sorting algorithm with a 2-way merge
  • a page only holds two values For each sorting pass, show the contents of all temporary files.
  1. (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.

Num passes = 1 + log (^) B-1N/B  = 1 + log 9 100/10  = 1 + log 910  = 1 + 2 = 3

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

84, 22 19, 11 60, 68 31, 29 58, 23 45, 93 48, 31 7

22, 84 11, 19 60, 68 29, 31 23, 58 45, 93 31, 48 7

11, 19

22, 84

29, 31

60, 68

23, 45 58, 93

7, 31 48

11, 19

22, 29 31, 60

68, 84

7, 23 31, 45

48, 58 93

7, 11

19, 22 23, 29

31, 31 45, 48

58, 60 68, 84

93

IV Query Optimization

Consider the following schema

Sailors(sid, sname, rating, age) Reserves(sid, did, day) Boats(bid, bname, size)

Reserves.sid is a foreign key to Sailors and Reserves.bid is a foreign key to Boats.bid.

We are given the following information about the database: Reserves contains 10,000 records with 40 records per page. Sailors contains 1000 records with 20 records per page. Boats contains 100 records with 10 records per page. There are 50 values for Reserves.bid. There are 10 values for Sailors.rating (1...10) There are 10 values for Boat.size There are 500 values for Reserves.day

Consider the following query

SELECT S.sid, S.sname, B.bname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.size > 5 AND R.day = 'July 4, 2003';

(a) Assuming uniform distribution of values and column independence, estimate the number of tuples returned by this query.

The maximum cardinality this query is R × S × B = 10 9. Reduction Factors: 1/2 for B.size > 5, 1/500 for R.day = ‘July 4, 2003’, 1/100 for R.bid = B.bid, and 1/1000 for S.sid = R/sid

So number of tuples return is (1/2)(1/500)(1/100)(1/1000)(10 9 ) = 10

Consider the following query

SELECT S.sid, S.sname, B.bname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid

(b) Draw all possible left-deep query plans for this query:

Note that we cannot join S and B since that would result in a cross product.

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

|R| = 250, |S| = 50, |B| = 10.

R join S:

  • Page-oriented nested loops join: R + R × S = 250 + 250 × 50 = 12570
  • Block nested loops join: R + R /49 × S = 250 + 6 × 50 = 550
  • Sort-merge join: note that 250 < (num buffers)^2 so cost is 3(R + S) = 3(250+50) = 900
  • Hash join: we can use hash join since each of the R partitions from phase one fit into memory. So

cost is 3 (R + S) = 3(250+50) = 900.

  • Cheapest: Block nested loops

S join R:

  • Page-oriented nest loops join: S + S × R = 50 + 50 × 250 = 12550
  • Block nested loops join: S + S /49 × R = 50 + 2 × 250 = 550
  • Sort-merge join: 3(R + S) = 3(250+50) = 900
  • Hash join: 3(R + S) = 3(250+50) = 900
  • Cheapest: Block nested loops

B join R:

  • Page-oriented nested loops: B + B × R = 10 + 10 × 250 = 2510
  • Block nested loops join: B + B /49 × R = 10 + 250 = 260
  • Sort-merge join: 3(10+250) = 780
  • Hash join: 3(10+250) = 780
  • Cheapest: Block nested loops

R join B:

  • Page-oriented nested loops join: The thing to note is that we can fit the entire Boats relation into memory, so we only need to scan Boats once. So the cost is R + B = 260. This is the same if we use simple nested loops join.
  • Block nested loops join: R + R /49 × B = 250 + 6 × 10 = 310
  • Sort merge join: 3(10+250) = 900
  • Hash join: 3(10+250) = 900
  • Cheapest: Page-oriented nested loops, or simple nested loops

VI. Functional Dependencies and Normalization (20 points total)

Consider the attributes A B C D E F G which have the following functional dependencies:

AD → F
AE → G
DF → BC
E → C
G → E
  1. List all candidate keys (not superkeys): (5 points)
A D E
A D G
  1. Which of the following dependencies are implied by those above: (5 points) a. (IS) (IS NOT) G → C b. (IS) (IS NOT) A → G c. (IS) (IS NOT) ADF → E d. (IS) (IS NOT) ADC → B e. (IS) (IS NOT) AGF → E
  2. Consider the decomposition into 4 relations: (ADF) (CE) (EG) (ABDG). This decomposition: (5 points) a. (IS) (IS NOT) in BCNF b. (IS) (IS NOT) in 3NF c. (IS) (IS NOT) in 1NF d. (IS) (IS NOT) Dependency Preserving e. (IS) (IS NOT) Lossless
  3. Consider the decomposition into 3 relations: (ADF) (EC) (ABDEG). This decomposition: (5 points) a. (IS) (IS NOT) in BCNF b. (IS) (IS NOT) in 3NF c. (IS) (IS NOT) 1NF d. (IS) (IS NOT) Dependency Preserving e. (IS) (IS NOT) Lossless

VII. Entity-Relational Model (20 Points)

a) (10 points) Draw an E-R diagram for the following situation:

  • This is a simplified model for reserving baseball tickets.
  • There are teams, which are identified by the team name. Teams are also located in a city.
  • Teams play each other in games, which occur on a particular date at a particular time. Games are identified by a game ID, and each game has exactly two teams that play in it.
  • A game is played in exactly one stadium.
  • A stadium is identified by its name, and is also located in a city.
  • Stadiums have seats, which have a section number, a row number, and a seat number.
  • Ticket holders reserve seats for a game. Ticket holders are identified by their name.
  • Some ticket holders are students (students get discounts, but we are not including that in the model).