20.11.2014 Views

The Solutions to the Midterm of CMPUT 391 Fall term, 2005 1. Give ...

The Solutions to the Midterm of CMPUT 391 Fall term, 2005 1. Give ...

The Solutions to the Midterm of CMPUT 391 Fall term, 2005 1. Give ...

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.

<strong>The</strong> <strong>Solutions</strong> <strong>to</strong> <strong>the</strong> <strong>Mid<strong>term</strong></strong> <strong>of</strong> <strong>CMPUT</strong> <strong>391</strong><br />

<strong>Fall</strong> <strong>term</strong>, <strong>2005</strong><br />

<strong>1.</strong> <strong>Give</strong> a schedule, as a sequence <strong>of</strong> database access operations, with at least two transactions<br />

that observes <strong>the</strong> repeatable read isolation level but not <strong>the</strong> (anomaly)serializable isolation level.<br />

Each operation, with a transaction id attached, may be <strong>of</strong> <strong>the</strong> following forms:<br />

• READ(T i , A),<br />

• WRITE(T i , A),<br />

• READ LOCK(T i , A),<br />

• WRITE LOCK(T i , A),<br />

• an SQL statement by T i .<br />

Specify any reasonable assumptions if needed. [10]<br />

Solution Assume <strong>the</strong> database contains a table sample with <strong>the</strong> folowing rows:<br />

number<br />

10<br />

20<br />

30<br />

<strong>The</strong> following schedule observes REPEATABLE READ but not SERIALIABLE.<br />

T 1 T 2<br />

READ LOCK(number(10)<br />

READ LOCK(number(20)<br />

SELECT * FROM sample WHERE number


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

works( e_id, d_id )<br />

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

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

an employee works for, and <strong>the</strong> last one indicates <strong>the</strong> budget and boss <strong>of</strong> each department.<br />

Write one or more triggers <strong>to</strong> ensure that no employee’s salary is higher than that <strong>of</strong> <strong>the</strong> manager<br />

<strong>of</strong> <strong>the</strong> department he/she is working for. [20]<br />

Solution This constraint can only be imposed by three triggers, one for each table, whenever<br />

an update on or an insertion <strong>to</strong> <strong>the</strong> table occurs. For simplicity, one trigger on employee is<br />

presented below, and <strong>the</strong> o<strong>the</strong>r two are similart.<br />

CREATE TRIGGER salary_cap<br />

BEFORE INSERT OR UPDATE ON employee<br />

FOR EACH ROW<br />

BEGIN<br />

SELECT e.salary INTO salary<br />

FROM employee e, works w, dept d<br />

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

w.d_id = d.d_id AND<br />

manager_id = e.e_id;<br />

END<br />

IF (salary > NEW.salary )<br />

THEN raise_application_error(-2000, ’Salary is <strong>to</strong>o high’);<br />

ELSE IF<br />

4. Consider <strong>the</strong> following functional dependencies over <strong>the</strong> attribute set R = ABCDEF GH:<br />

A → E<br />

AD → BE<br />

AC → E<br />

E → B<br />

BG → F<br />

BE → D<br />

BDH → E<br />

F → A<br />

D → H<br />

CD → A<br />

(a) Find a minimal cover <strong>of</strong> <strong>the</strong> given set <strong>of</strong> FDs.<br />

(b) Find a join loss-less, dependency preserving and 3NF decomposition <strong>of</strong> R.<br />

(c) Check if all <strong>the</strong> resulting relations in <strong>the</strong> previous step are in BCNF. If you find a schema<br />

that is not, decompose it in<strong>to</strong> a lossless BCNF.<br />

Explain all steps. [20]<br />

Solution<br />

(a) <strong>The</strong> following three steps lead <strong>to</strong> a minimal cover.<br />

Right-reducing It is done by split AD → BE in<strong>to</strong> AD → B and AD → E.<br />

Left-reducing leads <strong>to</strong> <strong>the</strong> following set:<br />

2


A → E BD → E<br />

A → B F → A<br />

E → B D → H<br />

BG → F CD → A<br />

E → D<br />

By eliminating redundant ones, we obtain <strong>the</strong> following minimal cover <strong>of</strong> <strong>the</strong> given set <strong>of</strong><br />

FDs:<br />

A → E BD → E<br />

E → B F → A<br />

BG → F D → H<br />

E → D CD → A<br />

(b) <strong>The</strong> following is a join lossless, dependency preserving and 3NF decomposition <strong>of</strong> <strong>the</strong> original<br />

database schema.<br />

AE, EBD, BGF, FA, DH, CDA, BGC.<br />

Note that BFC is a candicate key that was added later.<br />

(c) All tables, except CDA, in <strong>the</strong> above database schema are in BCNF. CDA is not in BCNF<br />

because A → D but not A → C. CDA can be decomposited in<strong>to</strong> {AD, AC}.<br />

5. Consider a university registration database with <strong>the</strong> following tables:<br />

student(sid, sname, address,city,status)<br />

pr<strong>of</strong>essor(pid, pname, deptid )<br />

course( crscode, title, description )<br />

transcript( tr sid, tr crscode, grade)<br />

teaching( t pid, t crscode )<br />

with <strong>the</strong> obvious meanings.<br />

Assume that <strong>the</strong> Web server delivers <strong>the</strong> contents <strong>of</strong> <strong>the</strong>se relations using <strong>the</strong> following XML<br />

format:<br />

<strong>the</strong> name <strong>of</strong> <strong>the</strong> relation is <strong>the</strong> <strong>to</strong>p-level element under root, each tuple is represented as a<br />

table-nameed tuple element, and each relation attribute is represented as an empty element that<br />

has a value attribute.<br />

For instance, <strong>the</strong> student table would be represented as followings:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

.<br />

.<br />

.<br />

3


.<br />

.<br />

.<br />

<br />

.<br />

<br />

<br />

Formula <strong>the</strong> following queries using XQuery:<br />

• Produce <strong>the</strong> list <strong>of</strong> all students who live at Edmon<strong>to</strong>n.<br />

FOR $s IN document(‘‘registrantion.cml’’)/root/student/<br />

WHERE $s/city@value = ‘‘Edmon<strong>to</strong>n’’]<br />

RETURN<br />

<br />

@s<br />

<br />

• List <strong>the</strong> titles <strong>of</strong> all <strong>the</strong> CS courses. A CS course is a course taught by a pr<strong>of</strong>essor working<br />

for <strong>the</strong> department with deptid = ’CS’. [20]<br />

FOR $t IN document(‘‘registrantion.cml’’)/teaching/teaching_tuple,<br />

$c IN document(‘‘registrantion.cml’’)/course/course_tuple,<br />

$p IN document(‘‘registrantion.cml’’)/pr<strong>of</strong>essor/pr<strong>of</strong>essor_tuple<br />

WHERE $p/deptid/@value = ‘‘CS’’ AND<br />

$t/t_pid/@value = $p/pid/@value AND<br />

$t/t_crscode/@value = $c/coscode/@value<br />

RETURN<br />

<br />

@c/title<br />

<br />

4

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

Saved successfully!

Ooh no, something went wrong!