31.12.2014 Views

Solutions to Midterm Examination

Solutions to Midterm Examination

Solutions to Midterm Examination

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Prof. Li-Yan Yuan<br />

CMPUT 391: Database Management Systems<br />

<strong>Solutions</strong> <strong>to</strong> <strong>Midterm</strong> <strong>Examination</strong><br />

February 28, 2007<br />

It is a close-book examination and the time for the test is 50 minutes. There are six (6) questions. The value of<br />

each question is indicated in [ ] and the <strong>to</strong>tal is 90. Good luck <strong>to</strong> all of you.<br />

1. One of the user-defined data-types, as specified by SQL’99, is a distinct type that is based on a<br />

built-in data type, such as INTEGER. Give one simple example <strong>to</strong> demonstrate the benefit of<br />

using such a user-defined data type. [10]<br />

solution: A simple example is <strong>to</strong> create two distinct types: canadian dollar and us dollar, based<br />

on numeric.<br />

2. Indicate whether the following table satisfies<br />

(a) the functional dependency A → C; and<br />

(b) the multivalue dependency B →→ C. [10]<br />

A B C<br />

1 2 3<br />

3 3 2<br />

2 2 3<br />

3 3 4<br />

solution: (a) No. (b) Yes.<br />

3. One of the inference rules for both FD and MVD is that X → A |= X →→ A.<br />

Prove or disprove that X →→ A |= X → A. [15]<br />

Solution: The table in (2) provides a counter example <strong>to</strong> the claim for it satisfies B →→ C but<br />

violates B → C.<br />

4. Consider a database consisting of the following tables.<br />

employee( e_id, e_name, position, salary )<br />

works( e_id, d_id, pct_time )<br />

dept( d_id, budget, manager_id )<br />

The first table indicates the job title and salary for each employee, the second shows the percentage<br />

of time that a given employee works for a given department, and the last one indicates the budget<br />

and boss of each department. Note that a manager must be an employee and his/her manager id<br />

is the corresponding e id.<br />

Write SQL triggers (in the Oracle style) <strong>to</strong> ensure that no employee will work over time (i.e., the<br />

<strong>to</strong>tal of pct time is over 100), except managers. [20]<br />

Solution: First, we can see that the following events may lead <strong>to</strong> the violation of the constraint:<br />

INSERT and UPDATE on works<br />

INSERT, UPDATE and DELETE on dept<br />

1


Therefore, we need <strong>to</strong> create triggers for all the above events.<br />

One trigger for INSERT and one for UPDATE on work are given below.<br />

CREATE TRIGGER no_hard_working_1<br />

BEFORE INSERT ON works<br />

FOR EACH ROW<br />

WHEN new.e_id NOT IN ( SELECT manager_id FROM dept )<br />

DECLARE <strong>to</strong>tal INT;<br />

BEGIN<br />

SELECT sum(pct_time) INTO <strong>to</strong>tal<br />

FROM works w<br />

WHERE w.e_id = new.e_id;<br />

IF (<strong>to</strong>tal > 100 - new.pct_time )<br />

THEN raise_application_error(-2000, ’working <strong>to</strong>o hard’);<br />

ELSE IF<br />

END<br />

CREATE TRIGGER no_hard_working_2<br />

BEFORE UPDATE ON works<br />

FOR EACH ROW<br />

WHEN new.e_id NOT IN ( SELECT manager_id FROM dept )<br />

DECLARE <strong>to</strong>tal INT;<br />

BEGIN<br />

SELECT sum(pct_time) INTO <strong>to</strong>tal<br />

FROM works w<br />

WHERE w.e_id = new.e_id;<br />

END<br />

IF (<strong>to</strong>tal > 100 - new.pct_time + old.pct_time )<br />

THEN raise_application_error(-2000, ’working <strong>to</strong>o hard’);<br />

ELSE IF<br />

5. Consider a relation schema with attributes R = ABCGW XY Z and the set of dependencies<br />

F = {XZ → ZY B, Y A → CG, C → W, B → G, XZ → G}.<br />

(a) Find a minimal cover for F.<br />

After right-reducing, we have<br />

XZ → B<br />

XZ → G<br />

XZ → Y<br />

Y A → C<br />

Y A → G<br />

C → W<br />

B → G<br />

2


It is easy <strong>to</strong> see that the left-had sides XZ and YA (as well as C and B) cannot be reduced<br />

because there are no FSs of the form X → ..., Y → ..., Z → ..., and A → ....<br />

Further, XZ → G is redundant and none of the others. So, the minimal cover consists of<br />

the above set minus XZ → G.<br />

(b) Decompose R in<strong>to</strong> a join-lossless 3NF database schema. [20]<br />

D = {XZBY, Y ACG, CW, BG, XZA} is a join-lossless 3NF database schema.<br />

Note that XZA is added because no other table in D that contains a key of R.<br />

6. Consider R = ABCDEF GH and F = {BE → GH, G → F A, D → C, F → B}. [15]<br />

(a) Can there be a key that does not contain D Explain.<br />

Solution: No, there cannot be a key that does not contain D because D does not appear in<br />

the right-hand side of any FD in F.<br />

(b) Is R in BCNF Explain.<br />

Solution: No, R is not in BCNF with respect <strong>to</strong> F. This is because F → B and F is not a<br />

key of R.<br />

3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!