Database Management Systems (COP 5725) (Spring 2014) Instructor: Dr. Markus Schneider TAs: Soham Das, Yan Qiao, Yi Wang Homework 5 Solutions 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: Exercise 1 Exercise 2 Exercise 3 Maximum 22 28 25 Exercise 4 Total 25 100 Received Exercise 1 (Functional Dependency) [22 points] Consider a relation with schema R(A,B,C,D) and functional dependencies (FD’s): ABC, CD, DA. (a) What are all the nontrivial FD’s that follow from the given FD’s? You can restrict to FD’s with single attributes on the right side. [5 pts] Solution: CA, ABD, ACD, BCA, BCD, BDA, BDC, CDA, ABCD, ABDC, and BCDA. (b) What are all the keys of R? [5 pts] Solution: AB, BC, and BD are keys. All other sets either do not have ABCD as the closure or contain one of these three sets. (c) What are all the superkeys of R that are not keys? [5 pts] Solution: The superkeys are all those that contain one of those three keys. That is, a superkey that is not a key must contain B and more than one of A, C, and D. Thus, the (proper) superkeys are ABC, ABD, BCD, and ABCD. (d) Consider schema T(A,B,C,D) with FD’s: BDC, CD, CDA and schema S(A,B,C,D) with FD’s: BDC, CD, DA. Use Armstrong’s axioms to prove or disprove if S or T or both are equivalent to R. [7 pts] Solution: Neither of them is equivalent to R. First, R implies S. Need to prove ABC implies BDC. Since DA, so BDAB (augmentation), together with ABC we get BDC (transitivity) Second, R implies T. Need to prove DA implies CDA. Since DA, we have CDAC (augmentation), then we get CDA (decomposition) Third, T can’t imply R. Because CD, CDA can’t derive DA, it only gets CA. Forth, T can’t imply S. Because BDC, DA can’t derive ABC, it only gets ABDC. Exercise 2 (Normal Forms) [25 points] (a) Consider relation schema R(A,B,C,D,E) with FD’s: ABC, DEC, BD. First indicate all the BCNF violations. You don’t need to consider violations that has more than one attributes on the right side. Then decompose the relations into collections of relations that are in BCNF. [14 pts] Solution: We can find all the nontrivial FDs: ABC, DEC, BD, ABD, BCD, BEC, BED, ABCD, ABDC, ABEC, ABED, ADEC, BCED, BDEC, ABCED, and ABDEC. From the closures we can also deduce that the only key is ABE. Thus, any dependency above that does not contain ABE on the left is a BCNF violation. These are: ABC, DEC, BD, ABD, BCD, BEC, BED, ABCD, ABDC, ADEC, BCED and BDEC. One choice is to decompose using the violation ABC. Using the above FDs, we get ABCD and ABE as decomposed relations. ABE is in BCNF because there are no keys and no nontrivial FDs, but ABCD is not in BCNF since AB is its only key and the FD BD follows for ABCD. Using violation BD to further decompose, we get BD and ABC as decomposed relations. BD is in BCNF because it is a two-attribute relation. ABC is in BCNF since AB is the only key and ABC is the only nontrivial FD. Thus the three relations of the decomposition are ABC, BD and ABE. (b) Consider relation schema R(A,B,C,D,E) with FD’s: ABC, CD, DB and DE. First indicate all the 3NF violations. You don’t need to consider violations that has more than one attributes on the right side. Then decompose the relations into collections of relations that are in 3NF. [14 pts] Solution: There are 41 nontrivial dependencies: CB, CD, CE, DB, DE, ABC, ABD, ABE, ACB, ACD, ACE, ADB, ADC, ADE, BCD, BCE, BDE, CDB, CDE, CEB, CED, DEB, ABCD, ABCE, ABDC, ABDE, ABEC, ABED, ACDB, ACDE, ACEB, ACED, ADEB, ADEC, BCDE, BCED, CDEB, ABCDE, ABCED, ABDEC and ACDEB. The keys are AB, AC and AD. FDs where the left side is not a superkey or the attributes on the right are not part of some key are 3NF violations. The 3NF violations are CE, DE, BCE, BDE, CDE and BCDE. We can decompose into relations using the minimal basis ABC, CD, DB and DE. The resulting decomposed relations would be ABC, CD, BD and DE. Since relation ABC contains a key, we can stop with the decomposition. The final set of decomposed relations is ABC, CD, BD and DE. Exercise 3 (Constraints and Triggers) [25 points] (a) How to use assertion and trigger to check integrity constraints? What’s the difference between assertion and trigger? [5 pts] Solution: refer to lecture slides. (b) Consider the same database schema as in exam 2 question 3: Product (maker, model, type) Desktop (model, speed, ram, hd, rd, price) Laptop (model, speed, ram, hd, screen, price) Printer (model, color, type, price) Write the following assertions in Oracle SQL. (1) No manufacturer of desktop may also make laptops. [5 pts] (2) A manufacturer of a desktop must also make a laptop with at least as great a processor speed. [5 pts] Solution: (1) CREATE ASSERTION CHECK (NOT EXISTS ( (SELECT maker FROM Product NATURAL JOIN Desktop) INTERSECT (SELECT maker FROM Product NATURAL JOIN Laptop) ) ); (2) CREATE ASSERTION CHECK (NOT EXISTS (SELECT maker FROM Product NATURAL JOIN Desktop WHERE speed > ALL (SELECT L2.speed FROM Product P2, Laptop L2 WHERE P2.maker = maker AND P2.model = L2.model ) ) ); Write the following triggers in Oracle SQL. (3) When updating the price of a desktop, check that there is no lower priced desktop with the same speed. [5 pts] (4) When inserting a new printer, check the model number exists in Product. [5 pts] Solution: (3) CREATE TRIGGER LowPriceDesktopTrigger AFTER UPDATE OF price ON Desktop D REFERENCING OLD ROW AS OldRow, OLD TABLE AS OldStuff, NEW ROW AS NewRow, NEW TABLE AS NewStuff FOR EACH ROW WHEN (NewRow.price < ALL (SELECT Desktip.price FROM Desktop WHERE Desktop.speed = NewRow.speed)) BEGIN DELETE FROM Desktop WHERE (model, speed, ram, hd, price) IN NewStuff; INSERT INTO Desktop (SELECT * FROM OldStuff); END; (4) CREATE TRIGGER NewPrinterTrigger AFTER INSERT ON Printer REFERENCING NEW ROW AS NewRow, NEW TABLE AS NewStuff FOR EACH ROW WHEN (NOT EXISTS (SELECT * FROM Product WHERE Product.model = NewRow.model)) DELETE FROM Printer WHERE (model, color, type, price) IN NewStuff; Exercise 4 (PL/SQL) [25 points] Consider the same relational schemas as in Exercise 3 (b). (a) Write a PL/SQL function to return and print out the total number of printer models. [8 pts] Solution: CREATE OR REPLACE FUNCTION totalPrinters RETURN number IS total number(5) := 0; BEGIN SELECT count(*) into total FROM Product Where type = ‘printer’; dbms_output.put_line('Number of printer models: ' || total); RETURN total; END; / (b) Write a PL/SQL block to set the desktop price to be at least half of the most expensive one and then print out the average desktop price after update. [8 pts] Solution: declare avg_price number; max_price number; begin select max(price) into max_price from desktop; update desktop set price := 0.5 * max_price where price < 0.5 * max_price; commit; select avg(price) into avg_price from desktop; dbms_output.put_line(“Average price is “ || avg_price); exception when others then dbms_output.put_line(“Update error!”); rollback; end; (c) Write a PL/SQL procedure to set the laptops with same speed and ram size to have the same price (their average price), then print out the new minimum price of all laptops. [9 pts] Solution: declare min_price number; CURSOR c1 IS SELECT speed, ram, ave(price) as avp FROM laptop FOR UPDATE of laptop; begin FOR x IN c1 LOOP UPDATE laptop SET price = x.avp WHERE speed = x.speed and ram = x.ram; END LOOP; commit; select min(price) into min_price from laptop; dbms_output.put_line(“Minimum price is “ || min_price); exception when others then dbms_output.put_line(“Update error!”); rollback; end;
© Copyright 2024 ExpyDoc