MidtermPracticeQuestions Solutions
MidtermPracticeQuestions Solutions
F={AB → C, B → F, E → G, A → DE }.
(a) Find all keys of this relation. Do not report a superkey that is not a (minimal) key.
AB+ = ABCDEFG
No it is not in 3NF.
B->F, E->G, A->DE all violate 3NF. Because B, E, A are not (super)keys and F,G,DE are not parts of a key.
Decompose R into a collection of BCNF relations and state whether the decomposition is lossless-join and
dependency preserving.
1st decomposition: R==> R1(B,F) with {B->F} and R2(A,B,C,D,E,G) with { AB → C, E → G, A → DE}
2nd decomposition R2 ==> R21(E,G) with {E->G} and R22(A,B,C,D,E) with { AB → C, A → DE}
3rd decomposition R22==> R221(A,D,E) with {A->DE} and R222(A,B,C) with { AB → C}
The resulting decomposition R1(B,F), R21(E,G), R221(A,D,E) and R222(A,B,C) is a lossless and
dependency preserving decomposition.
a) You want to ensure that the number of courses that a student is enrolled should not be more than 7. Define
an assertion to achieve this.
1
b) Define a trigger to achieve the same constraint in part a).
c) Suppose there is a functional dependency such that student_id → status. Write an assertion to enforce this
functional dependency.
2
Q3. Consider the following database schema and the query:
Schema: Viewers(Id,age)
Movie(title, director)
HasSeen(viewerId, title)
Query:
SELECT M.title
FROM Movie M, HasSeen H, Viewers V
WHERE M.title= H.title AND H.viewerId=V.Id AND
V.age > 25 AND V.age < 30 AND M.director = ‘JohnDoe’;
Compute the selectivity (i.e. reduction factor) for each individual term in the WHERE clause.
M.title= H.title :
1/1000 (Assuming title is a foreign key in HasSeen, each HasSeen will join with exactly one Movie record;
thus T(Movie)xT(HasSeen)/1000 records will be in the result)
H.viewerId=V.Id :
(30-25-1)/(60-20+1) = 4/41=0.097
M.director = ‘JohnDoe’:
3
Q4. For each of the following questions, write your answer in the given space.
(1) Consider relations R(a, b) and S(a, c), for the following query. Would a B+ tree index on S.a help in query
processing? Briefly explain.
Answer: A B+ tree index on S.a may help the optimizer consider INL-join. If b<10 eliminates many R tuples and
V(S,a) is small then the index will be very useful.
(2) For the above SQL query, is there a unique way to write it in relational algebra? If so, give
Answer: There is no unique way to write it in relational algebra. There are several RA expressions equivalent to
the given query. For example selection conditions can be done first and join can be done next. Projection
operation can be done early as well.
(3) For the following table, show a decomposition (into two tables) that is lossless.
A B C D
1 3 2 2
2 3 2 4
3 1 3 6
3 1 1 6
A ->B holds in this instance. We can decompose it using this functional dependency:
4
Q5. Consider two relations R(a, b) and S(b, c) with the following statistics:
(iii) Suppose we have a clustered B+tree index available for attribute b of S. The tree has 3 levels
including the leaf level, and each leaf node contains 4 records. Assume each node of the index
occupies one block. Estimate the cost of executing the following query: (the cost is measured by disk
I/Os for accessing the index and the records).
SELECT *
FROM S
WHERE b = 100 OR b = 1000
5
Q6. Consider a relation R with attributes {A, B, C, D, E, F, G, H} and the set of functional dependencies
F = {G FD, E D, GD CE, BD A }
a) Minimal cover:
G->F, E->D,G->C,G->E,BD->A
b) Decomposition:
R1(G,C,E,F)
R2(E,D)
R3(A,B,D)
R0(B,G,H) (BGH is key for R)
c) 3NF decomposition results in a lossless and dependency preserving decomposition. The resulting
relations are also in BCNF.
d) It is possible that with a BCNF schema we ma not preserve some functional dependencies, in which case
we need to write triggers to check those FDs. Those triggers will be costly since they will require joining
two or more tables.
Q7. Consider the relation R(A,B,C,D,E) with multi-valued dependencies MD = { A B, AB C} and
functional dependencies FD = {A D, AB E}.
a) ABC is the key for the relation (we consider only FDs in finding key)
b) R1(A,D), R2(A,B),R3(A,C,E)