You are on page 1of 4

CSCI 5333.

1 DBMS
Fall 2013
Suggested Solution to Final Examination
(1)

For example, minimally documented:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSCI 5333.1 DBMS Fall 2013 Final Examination, Question 1</title>
</head>
<body>
<?php
// Minimally documented.
include('dbconfig_sakila.php');
// Get keyword using the HTTP GET method.
if (array_key_exists('keyword', $_GET)) {
$keyword = $_GET['keyword'];
}
// Get category name, film counts and actor counts
$query = <<<__QUERY
select concat(a.first_name, ' ', a.last_name), count(distinct fa.film_id) as NumFilms
from actor a, film_actor fa
where a.actor_id = fa.actor_id
and (a.last_name like ? or a.first_name like ?)
group by a.first_name, a.last_name;
__QUERY;
if ($stmt = $mysqli->prepare($query)) {
$searchPattern = "%$keyword%";
$stmt->bind_param('ss', $searchPattern, $searchPattern);
$stmt->execute();
$stmt->bind_result($actor, $countFilms);
$stmt->store_result();
if ($stmt->num_rows > 0) {
echo "<table border=\"1\"><tr><td>Actor</td><td>Number of
films</td></tr>\n";
while ($stmt->fetch()) {
$actor = preg_replace("/($keyword)/i", '<span
style="background:#FFFF88">\\1</span>', $actor);
echo "<tr><td>$actor</td><td>$countFilms</td></tr>\n";
}
echo "</table>";
$stmt->free_result();

}
}
$mysqli->close();
?>
</body>
</html>
(2)

To show F = {AB->C, CD->E, A->B, DE-> F}

implies
AD-> F
Proof: For example:
[1] A-> B (given)
[2] AA->AB (augmentation of A on [1])
[3] A->AB (simplification of [2])
[4] AB->C (given)
[5] A-> C (transitivity on [3] and [4])
[6] AD->CD (augmentation of D on [5])
[7] CD -> E (given)
[8] AD -> E (transitivity on [6] and [7])
[9] DE-> F (given)
[10] ADD-> F (pseudo-transitivity on [8] and [7])
[11] AD->F (simplification of [10])
QED.
(3) Yes, the decomposition is lossless.
Given {AD->C, DB->AE, E->CD}. For R to decompose into R1(A,B,D,E), R2(B,C,D)
and R3(C,D,E):
(a) The decomposition of R into R3(C,D,E) and R(A,B,C,D,E) is lossless since the
common attributes are CDE and CDE -> in R3.
(b) The further decomposition of R into R1(A,B,D,E) and R2(B,C,D) is also lossless
since the common attributes are BD and both BD->AE in R1.
Alternatively, you can use the algorithm for checking for lossless decomposition
below.
Given {AD->C, DB->AE, E->CD}. For R to decompose into R1(A,B,D,E), R2(B,C,D)
and R3(C,D,E):

Step 1. Create a table of 5 columns (number of columns and 3 rows (number of


relations). Populate it with b(i,j).
Relation

R1

b(1,1)

b(1,2)

b(1,3)

b(1,4)

b(1,5)

R2

b(2,1)

b(2,2)

b(2,3)

b(2,4)

b(2,5)

R3

b(3,1)

b(3,2)

b(3,3)

b(3,4)

b(3,5)

Step 2. For each relation Ri, set all attribute Aj that appears in Ri from b(i,j) to a(j).
Relation

R1

a(1)

a(2)

b(1,3)

a(4)

a(5)

R2

b(2,1)

a(2)

a(3)

a(4)

b(2,5)

R3

b(3,1)

b(3,2)

a(3)

a(4)

a(5)

Step 3. For each FD X-> Y, if two rows have the common X values, for every
attribute W in Y:

If one cell is an a and the other cell is an b, change the b to the a.

If both cells are b's, change them to the same b.

Applying AD->C: no change


Relation

R1

a(1)

a(2)

b(1,3)

a(4)

a(5)

R2

b(2,1)

a(2)

a(3)

a(4)

a(5)

R3

b(3,1)

b(3,2)

a(3)

a(4)

a(5)

R1

a(1)

a(2)

b(1,3)

a(4)

a(5)

R2

a(1)

a(2)

a(3)

a(4)

a(5)

R3

b(3,1)

b(3,2)

a(3)

a(4)

a(5)

Applying DB->AE:
Relation

Since row 2 has only a's, the decomposition is lossless.


Note that you may continue to apply AD->C to change b(1,3) to a(3).
(4)

(a)

(b)

(c)

(d)

(e)

(5) F= {AB->C, CE->D, B->DF, BF->CA, AC->B}


(a) A+ = A, B+ = ABCDF, C+ = C, D+ = D, E+=E, F+ = F
(b) The candidate keys are: BE and ACE. Prime: ABCE; non-prime: DF.
(c) For example: {B->ACDF, AC->B, CE->D}
(d) 1NF. The FD B->D violates 2NF as B is a part of a candidate key and D is nonprime.
(e) Yes, the decomposition of R into the following is lossless and it preserves all FDs.
R1(A,B,C,D,F) with {B->ACDF,AC->B}
R2(C,D,E) with {CE-> D}
R3(B,E)
It is easy to see that R1 to R3 are all in BCNF. Furthermore, you can show that the
decomposition is lossless using standard techniques.
(6) For example:
DROP FUNCTION NumberOfFilmsTogether;
DELIMITER //
CREATE FUNCTION NumberOfFilmsTogether(actorId_1 SMALLINT,
actorId_2 SMALLINT) RETURNS INT
BEGIN
DECLARE count INT DEFAULT 0;
SELECT COUNT(DISTINCT fa1.film_id) INTO count
FROM FILM_ACTOR fa1, FILM_ACTOR fa2
WHERE fa1.actor_id = actorId_1
AND fa2.actor_id = actorId_2
AND fa1.film_id = fa2.film_id;
RETURN count;
END //
DELIMITER ;
-- Call
SELECT NumberOfFilmsTogether(27,60);

You might also like