Docsity

Solved Homework 5 - Database Management System | COP 5725, Assignments of Computer Science

Toggle sidebar
bg1
Database Management Systems (COP 5725)
(Fall 2008)
Instructor: Dr. Markus Schneider
TAs:
Mingsong Chen
Wenjie Yuan
Homework 5
Name:
UFID:
Email Address:
Pledge (Must be signed according to UF Honor Code)
On my honor, I have neither given nor received unauthorized aid in doing this assignment.
_______________________________________________Signature
For scoring use only:
Maximum Received
Exercise 1 20
Exercise 2 20
Exercise 3 20
Exercise 4 20
Exercise 5 20
Total 100
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Solved Homework 5 - Database Management System | COP 5725 and more Assignments Computer Science in PDF only on Docsity!

Database Management Systems (COP 5725)

(Fall 2008)

Instructor: Dr. Markus Schneider

TAs:

Mingsong Chen Wenjie Yuan

Homework 5

Name: UFID: Email Address:

Pledge (Must be signed according to UF Honor Code)

On my honor, I have neither given nor received unauthorized aid in doing this assignment.

_______________________________________________Signature

For scoring use only:

Maximum Received

Exercise 1 20

Exercise 2 20

Exercise 3 20

Exercise 4 20

Exercise 5 20

Total 100

Exercise 1 (Concept) [20 points]

  1. What is a trigger? Discuss the strengths and weaknesses of the trigger mechanism.

Answer: A trigger is a procedure that is automatically invoked in response to a specified change to the database. The advantages of the trigger mechanism include the ability to perform an action based on the result of a query condition. The set of actions that can be taken is a superset of the actions that integrity constraints can take (i.e. report an error). Actions can include invoking new update, delete, or insert queries, perform data definition statements to create new tables or views, or alter security policies. Triggers can also be executed before or after a change is made to the database (that is, use old or new data). There are also disadvantages to triggers. These include the added complexity when trying to match database modifications to trigger events. Also, integrity constraints are incorporated into database performance optimization; it is more difficult for a database to perform automatic optimization with triggers. If database consistency is the primary goal, then integrity constraints offer the same power as triggers. Integrity constraints are often easier to understand than triggers.

  1. Give a set of FDs for the relation schema R(A,B,C,D) with primary key AB under which R is in 1NF but not in 2NF.

Solution: Consider the set of FD: AB โ†’ CD and B โ†’ C. AB is obviously a key for this relation since AB

โ†’ CD implies AB โ†’ ABCD. It is a primary key since there are no smaller subsets of keys that hold over R(A,B,C,D). The FD: B โ†’ C violates 2NF since:

  1. C โˆˆ B is false, that is, it is not a trivial FD
  2. B is not a superkey
  3. C is not part of some key for R
  4. B is a proper subset of the key AB (transitive dependency)
  1. Give a set of FDs for the relation schema R(A,B,C,D) with primary key AB under which R is in 2NF but not in 3NF.

Answer: Consider the set of FD: AB โ†’ CD and C โ†’ D. AB is obviously a key for this relation since AB โ†’ CD implies AB โ†’ ABCD. It is a primary key since there are no smaller subsets of keys that hold over R(A,B,C,D). The FD: C โ†’ D violates 3NF but not 2NF since: D  C is false; that is, it is not a trivial FD C is not a superkey D is not part of some key for R

  1. For each of the two numbered statements given below, choose the most suitable answer from among the following: (A) Statement 1 alone is correct. (B) Statement 2 alone is correct. (C) Both statements are correct. (D) Both statements are incorrect.

i. (1) On insert triggers have no :OLD values (2) On delete triggers have no :NEW values.

Answer: (C)

ii. (1) If a transaction is rolled back, the data changed by the trigger cannot be rolled back. (2) Triggers do not commit transactions. Unhandled exceptions in the trigger will cause a rollback of the entire transaction, not just the trigger.

Answer: (B)

iii. (1) A commit/rollback affects the entire transaction, it is all or none. (2) Commits, rollbacks and save points are not allowed in the trigger body.

Answer: (A)

iv. (1) A trigger cannot cause other events to execute triggers. (2) A trigger can change a table that it has read from, using the :OLD and :NEW values.

Answer: (D)

v. (1) If more than one trigger is defined on an event, the order in which they fire is not defined. (2) If the triggers must fire in order, create one trigger that executes all the actions in the required order.

Answer: (C)

Exercise 2 (Closure) [20 points]

Given relation:

R= {A,B,C,G,H,I}

and a set F of functional dependencies:

F= {A โ†’ B, A โ†’ C, CG โ†’ H, CG โ†’ I, B โ†’ H}

(a) Determine the closure F+ of F (Note: Trivial dependencies do not have to be shown). Answer: F+ = {Aโ†’B, Aโ†’C, CGโ†’H, CGโ†’I, Bโ†’H, Aโ†’H, CGโ†’HI, AGโ†’I} *can also include trivial dependencies, not shown for simplicity.

(b) Given a functional dependency X โ†’ Z (where X, Z โІ R).

  1. How can we check whether X โ†’ Z โˆˆ F+ without having to compute F+?
  2. Write down an algorithm that implements the method you propose. Answer: By calculating the closure of the attribute X(X+) regarding F. If Y โІ X+ holds then X โ†’ Y โˆˆ F+ holds.

Let us consider ABC joining ACDE = ABCDE. Common attributes between R1 and R2 are (AC). Since ( R 1 โˆฉ R 2) โ†’ R 1 โˆˆ F +, hence R1 and R2 is lossless-join. Now consider ABCDE joining ADG = ABCDEG. The common attributes are AD. Since ADโ†’R3โˆˆ F + (i.e., from F, AD โ†’ E and E โ†’ G => AD โ†’ G =>AD โ†’ ADG). So, decomposition is lossless-join

Exercise 4 (Normalization) [20 points]

  1. Suppose you are given a relation R with four attributes ABCD. For each of the following sets of FDs, assuming those are the only dependencies that hold for R, do the following: (a) Identify the candidate key(s) for R. (b) Identify the best normal form that R satisfies (1NF, 2NF, 3NF, or BCNF). (c) If R is not in BCNF, decompose it into a set of BCNF relations that preserve the dependencies.
    1. C โ†’ D, C โ†’ A, B โ†’ C
    2. B โ†’ C, D โ†’ A
    3. ABC โ†’ D, D โ†’ A
    4. A โ†’ B, BC โ†’ D, A โ†’ C
    5. AB โ†’ C, AB โ†’ D, C โ†’ A, D โ†’ B

Answer:

  1. (a) Candidate keys: B (b) R is in 2NF but not 3NF. (c) C โ†’ D and C โ†’ A both cause violations of BCNF. One way to obtain a (lossless) join preserving decomposition is to decompose R into AC, BC, and CD.
  2. (a) Candidate keys: BD (b) R is in 1NF but not 2NF. (c) Both B โ†’ C and D โ†’ A cause BCNF violations. The decomposition: AD, BC, BD (obtained by first decomposing to AD, BCD) is BCNF and lossless and join-preserving.
  3. (a) Candidate keys: ABC, BCD (b) R is in 3NF but not BCNF. (c) ABCD is not in BCNF since D โ†’ A and D is not a key. However if we split up R as AD, BCD we cannot preserve the dependency ABC โ†’ D. So there is no BCNF decomposition.
  4. (a) Candidate keys: A (b) R is in 2NF but not 3NF (because of the FD: BC โ†’ D). (c) BC โ†’ D violates BCNF since BC does not contain a key. So we split up R as in: BCD, ABC.
  5. (a) Candidate keys: AB, BC, CD, AD (b) R is in 3NF but not BCNF (because of the FD: C โ†’ A). (c) C โ†’ A and D โ†’ B both cause violations. So decompose into: AC, BCD but this does not preserve AB โ†’ C and AB โ†’ D, and BCD is still not BCNF because D โ†’ B. So we need to decompose further into: AC, BD and CD. However, when we attempt to revive the lost functional dependencies by adding ABC and ABD, we that these relations are not in BCNF form. Therefore, there is no BCNF decomposition.

CREATE ASSERTION managerAge CHECK ((SELECT E.age FROM Emp E, Dept D WHERE E.eid = D.managerid ) > 30 )

Since the constraint involves two relations, it is better to define it as an assertion, independent of any one relation, rather than as a check condition on the Dept relation. The limitation of the latter approach is that the condition is checked only when the Dept relation is being updated. However, since age is an attribute of the Emp relation, it is possible to update the age of a manager which violates the constraint. So the former approach is better since it checks for potential violation of the assertion whenever one of the relations is updated.

4: To write such statements, it is necessary to consider the constraints defined over the tables. We will ssume the following:

CREATE TABLE Emp ( eid INTEGER, ename CHAR(10), age INTEGER, salary REAL, PRIMARY KEY (eid) )

CREATE TABLE Works ( eid INTEGER, did INTEGER, pcttime INTEGER, PRIMARY KEY (eid, did), FOREIGN KEY (did) REFERENCES Dept, FOREIGN KEY (eid) REFERENCES Emp, ON DELETE CASCADE)

CREATE TABLE Dept ( did INTEGER, buget REAL, managerid INTEGER , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp, ON DELETE SET NULL)

Now, we can define statements to delete employees who make more than one of their managers:

DELETE FROM Emp E WHERE E.eid IN ( SELECT W.eid FROM Work W, Emp E2, Dept D WHERE W.did = D.did AND D.managerid = E2.eid AND E.salary > E2.salary )