Sample Midterm 1 Questions

Transcription

Sample Midterm 1 Questions
Sample Midterm 1 Questions
The first midterm will cover material up to and including storage and indexing. The following questions
are samples of the types of things I have asked in the past on this material.
1. (15 points)
Consider the two ER-diagrams on the following page, which are intended to represent information about
baseball teams, their players and managers since the beginning of baseball. The Season attribute of
Plays and Manages is a year (such as 1990). Keys are indicated in bold font (i.e. the key of Player
is PID#, the key of Manager is MId# and the key of Team is TName in Teams 1 and TName, Year
in Teams 2.
(a) Tabularize the Plays-On relationship in each of Teams 1 and 2. Indicate the key by underlining
attributes in the key.
SOLUTION:
Teams1: Plays-On(PId#, TName, Season, Number, Stats)
Teams2: Plays-On(PId#, TName, Year, Number, Stats)
(b) State whether or not the schemas of Teams 1 and 2 can capture the following information. You
can assume that null values are allowed for non-key fields.
i. Joe Green with Id# 1 is a manager but has never managed any team.
SOLUTION: Teams 1 but not Teams 2.
ii. John Hitter with Id# 2 is a player who was on the White Sox for the 1990 season; his number
was 99 and stats are unknown.
SOLUTION: Both.
iii. Gary Chump with Id# 2 is a player who was on the Phillies during the 1992, 1995 and
1996 seasons. His number was 65 in each of those seasons and his stats were X, Y and Z
respectively.
SOLUTION: Teams1 cannot represent this, a player can only play on a team for one season.
Teams2 also can represent this since it is a one-many relationship.
iv. Michael Wonderful’s number was retired in 1996, and no player on the Orioles can use it
again.
SOLUTION: Neither can represent this.
v. There were two different managers of the Yankees in 2000 season (as would happen, say, if
one were fired in the middle of the season).
SOLUTION: Teams 1 but not Teams 2.
1
PId#
PName
Player
Season
TName
Plays-On
Number
City
Team
League
Stats
Season
Manages
Manager
MId#
MName
TName
City
Teams 1
PId#
PName
Player
Number
Stats
Plays-On
Team
MId#
Teams 2
2
MName
League
Year
2. Consider a database of beer drinkers with schema: Likes(Drinker, Beer), Frequents(Drinker, Bar), and
Serves(Bar, Beer). (The relations have the obvious interpretations.)
Write SQL expressions for the following queries:
(a) Drinkers who like no beer but frequent some bar.
SOLUTION:
select Drinker
from Frequents
where Drinker not in
(select Drinker
from Likes)
(b) Drinkers who like all beers that drinker John likes (you may assume that John likes at least one
beer).
SOLUTION:
select Drinker
from Likes L1
where not exists
(select *
from Likes L2
where L2.Drinker= ’John’ and not exists
(select *
from Likes L3
where L3.Drinker= L1.Drinker and L3.Beer= L2.Beer) )
(c) For each drinker whose name starts with ’J’ and who likes more than 4 beers, print their name
and the total number of beers they like.
SOLUTION:
select name, count(*)
from Likes l
where Drinker like ’J%’
group by Drinker
having count(*) > 4
(d) Print a table (Drinker, Bar, Beer) where the drinker likes the beer and frequents the bar, and the
bar serves the beer (no null values).
SOLUTION:
select l.drinker, l.beer, f.bar
from Likes l, Frequents f, Serves s
where l.Drinker = f.Drinker and l.Beer= s.Beer and f.Bar = s.Bar
3
3. (15 points)
Consider the following relation representing the name and type of costumes that a group of children
will be wearing for Halloween:
Halloween
Child
Joe
Alice
Zoe
Costume
Bunny
Witch
Ghost
Type
Cute
scary
scary
(a) Alice decides that she wants to create a table Scary which has one attribute containing the
names of all scary costumes in Halloween. Write the SQL statements she will need. (Do not
worry about the exact syntax.)
SOLUTION:
CREATE TABLE Scary (Name: string);
INSERT INTO Scary (SELECT Name FROM Halloween WHERE Type=’scary’;
(b) Zoe decides that she wishes to create a view CoolKids of all children who are not wearing cute
costumes, as well as the name and type of costume they are wearing. Write the SQL statements
that she will need. (Do not worry about the exact syntax.)
SOLUTION:
CREATE VIEW CoolKids AS
SELECT * FROM Halloween WHERE Type 6= ’cute’;
(c) The relation Halloween is now updated by inserting the tuple (’Albert’, ’Monster’, ’scary’).
If Alice now read all tuples in Scary and Zoe reads all tuples in CoolKids what will they each
see?
SOLUTION:
Scary
CoolKids
Costume
Witch
Ghost
Child
Alice
Zoe
Albert
Costume
Witch
Ghost
Monster
Type
scary
scary
scary
(d) Alice inserts a new tuple (’Alien’) into Scary, while Zoe inserts a tuple (’Jason’, ’Jason’, ’Horror’)
into CoolKids. What is the effect on Halloween?
SOLUTION: Since Scary is a table, it has no effect on Halloween. However, CoolKids is an
updatable view so the tuple would be inserted into Halloween.
(e) When Zoe issues the following query, what query is actually executed?
SELECT Type
FROM CoolKids
WHERE Name 6= ’Alice’
SOLUTION:
SELECT Type
FROM Halloween
WHERE Name 6= ’Alice’ and Type 6= ’Cute’
4
4. Consider the following B+ tree, and assume that when a split occurs at a leaf two values are copied
to the left (old) leaf and three to the right (new) leaf.
13 17 24 30 2* 3* 5* 7* 14* 16* 19* 20* 22* 24* 27*29* 33* 34* 38* 39* (a) (5 points)
Draw the resulting tree after deleting 7*, then 5*, then 3*.
SOLUTION: Deleting 7* and 5* is straightforward. Deleting 3* causes the two leftmost leaves
to be combined, and the index entry 13 (and pointer) to be deleted.
17 24 30 2* 14* 16* 19* 20* 22* 24* 27*29* 5
33* 34* 38* 39* (b) (5 points)
USING THE ORIGINAL TREE: Draw the resulting tree after inserting 30.
SOLUTION: This one is tricky, since you HAVE to insert into the last leaf, which causes a split
and the tree to grow in height.
24 13 17 2* 3* 5* 7* 14* 16* 30 34 19* 20* 22* 24* 27*29* 6
30* 33* 34* 38* 39* 5. Suppose that you are given a relation R(A, B, C, D). For each of the following questions, assume that
F is the only set of dependencies that hold over R.
(a) F ={AB −→ C, C −→ A, C −→ D}.
i. What are the candidate keys for R?
SOLUTION: AB and BC
ii. Suppose you decompose R into ACD and BC. Is this a good decomposition? Justify your
answer.
SOLUTION: No. Although decomposing R into ACD and BC will have a lossless join (due
to C −→ AD), not all dependencies are preserved: AB −→ C is lost.
iii. Now suppose you decompose R into ABC and CD. Is this a good decomposition? Justify
your answer.
SOLUTION: Yes. Decomposing R into ABC and CD will have a lossless join (C −→ D)
and at the same time is dependency preserving.
iv. Consider the schema ABC in the previous decomposition. Is it in BCNF? Is it in 3NF?
Justify your answers.
SOLUTION: No, it is not in BCNF. But it is in 3NF.
For schema ABC, AB −→ C and C −→ A hold. However, C −→ A is not a trivial dependency, nor is C is superkey for ABC. Hence, it is not in BCNF. But, A is member of the key
AB for ABC. Also, for AB −→ C, AB is superkey for ABC. Since every dependency over
ABC satisfies the requirements for 3NF, it is in 3NF.
(b) F ={A −→ B, B −→ C, C −→ D}
i. What are the candidate keys for R?
SOLUTION: A
ii. Decompose this schema into 3NF.
SOLUTION: AB, BC, CD
7