Assignment #2 CS 348 - Spring 2015 Due on Friday, June 19, 2015, 9 AM For instructions on how to submit your assignment check the course website. 1
CS 348 - Spring 2015 : Assignment #2 QUESTION 1. Question 1. Consider the following database schema: Emp(eid : integer, ename : string, age : integer, salary : real) W orks(eid : integer, did : integer, pct time : integer) Dept(did : integer, budget : real, managerid : integer) Write SQL statements to answer the tasks that follow: 1. Create the Emp table. CREATE TABLE emp ( eid INTEGER PRIMARY KEY, ename VARCHAR(20), age INTEGER, salary DECIMAL(8, 2) ); 2. Insert the following instance into Emp. Emp eid ename age salary 1 Donald Duck 35 18, 000 4 Minnie Mouse 19 9, 000 2 Mickey Mouse 24 20, 000 3 Goof y 20 13, 000 INSERT INTO emp VALUES (1, Donald Duck, 35, 18000), (4, Minnie Mouse, 19, 9000 ), (2, Mickey Mouse, 24, 20000 ), (3, Goofy, 20, 13000 ); Page 2 of 11
CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 3. Try altering the Emp table to define a constraint that will ensure that every employee makes at least $10,000. ALTER TABLE emp ADD CONSTRAINT check_salary CHECK (salary > 10000); 4. Give Minnie a salary increase of $2,000. UPDATE emp SET salary = salary + 2000 WHERE ename LIKE %Minnie% ; 5. Alter the Emp table to define a constraint that will ensure that every employee makes at least $10,000. ALTER TABLE emp ADD CONSTRAINT check_salary CHECK (salary > 10000); 6. Create the Dept table. CREATE TABLE dept ( did INTEGER PRIMARY KEY, budget DECIMAL(9, 2), managerid INTEGER ); 7. Alter the Dept table to define a foreign key constraint (with cascade delete) on managerid, referencing Emp table. ALTER TABLE dept ADD CONSTRAINT manager_fk FOREIGN KEY (managerid) REFERENCES emp (eid) ON DELETE CASCADE; 8. Insert a valid tuple in Dept. INSERT INTO dept VALUES (1, 180000, 1); 9. Delete all tuples from Emp. DELETE FROM emp; Page 3 of 11
CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 10. List all tuples from Dept. SELECT * FROM dept; 11. Create the W orks table and define foreign key constraints on eid and did, referencing Emp and Dept tables, respectively. CREATE TABLE works ( eid INTEGER REFERENCES emp ON DELETE CASCADE, did INTEGER REFERENCES dept ON DELETE CASCADE, pct_time INTEGER, PRIMARY KEY (eid, did) ); 12. Populate all 3 relations with 3 to 6 valid tuples. INSERT INTO emp VALUES (1, Donald Duck, 35, 18000), (4, Minnie Mouse, 19, 11000 ), (2, Mickey Mouse, 24, 20000 ), (3, Goofy, 20, 13000 ); INSERT INTO dept VALUES (100, 180000, 1), (200, 280000, 1), (300, 360000, 3); INSERT INTO works VALUES (1, Page 4 of 11
CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 100, 310), (1, 200, 120), (4, 300, 410), (3, 300, 180), (2, 200, 190); 13. Delete all information about employees whose salaries exceed that of the manager of one or more departments that they work in. Ensure that all the relevant integrity constraints are satisfied after your updates. DELETE FROM emp E WHERE E.eid IN (SELECT W.eid FROM works W, emp M, dept D WHERE W.did = D.did AND D.managerid = M.eid AND E.salary > M.salary); Page 5 of 11
Question 2. Consider the following database schema: P roduct(manufacturer, Model, T ype) Computer(M odel, Speed, RAM, HD, CD, Screen, P rice) P rinter(model, Color, T ype, P rice) and its instance: P roduct M anuf acturer M odel T ype A 1001 pc A 1002 pc A 1003 pc B 1004 pc B 1006 pc B 3002 printer B 3004 printer C 1005 pc C 1007 pc D 1008 pc D 1009 pc D 1010 pc D 2001 laptop D 2002 laptop D 2003 laptop D 3001 printer D 3003 printer E 2004 laptop E 2008 laptop F 2005 laptop G 2006 laptop G 2007 laptop H 3005 printer I 3006 printer Computer M odel Speed RAM HD CD Screen P rice 1001 3 256 120 48x 17 795 1002 2 256 120 48x 17 699 1003 3.5 512 180 48x 16 999 1004 3.5 1024 180 56x 16 1099 1005 3.5 256 160 56x 14 1099 1006 4.5 1024 200 56x 17 1199 1007 4.5 1024 300 56x 19 1249 1008 4 1024 160 56x 14 1699 1009 4.5 1024 180 56x 14 1299 1010 3.4 256 100 56x 17 700 2001 2 256 60 72x 14 1099 2002 2.1 192 40 48x 15.4 1298 2003 2.1 1024 50 56x 15 1499 2004 3 256 60 48x 15.5 1399 2005 3.5 256 50 48x 15.4 1299 2006 2 128 45 56x 17 1099 2007 2.5 256 80 56x 17 1999 2008 2 256 60 56x 17 1199 P rinter Model Color T ype P rice 3001 true ink jet 99 3002 true ink jet 69 3003 f alse laser 209 3004 true laser 391 3005 f alse ink jet 59 3006 true dry 129 Page 6 of 11
Write SQL queries to answer the following: 1. Which laptop models are shown in the P roduct relation? SELECT DISTINCT model WHERE model IS NOT NULL AND type = laptop ; 2. Find the set of all manufacturers ordered alphabetically. SELECT DISTINCT manufacturer WHERE manufacturer IS NOT NULL ORDER BY manufacturer ASC ; 3. Which computer models appear in the database? SELECT DISTINCT model FROM computer WHERE model IS NOT NULL; 4. Which computer models use 256 MB of RAM? SELECT DISTINCT model FROM computer WHERE ram = 256 AND model IS NOT NULL; 5. Which computer models cost less than $1100? SELECT DISTINCT model FROM computer WHERE price < 1100 AND model IS NOT NULL; 6. Which laptop models have a HD of at least 50 GB and cost less than $1400? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND c.hd >= 50 AND c.price < 1400; Page 7 of 11
7. Which laser printer models can print color documents? SELECT DISTINCT model FROM printer WHERE color = true AND type = laser AND model IS NOT NULL; 8. Which laptop models that have more than 256 MB of RAM are produced by manufacturer D? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND p.manufacturer = D AND c.ram > 256; 9. Which computer models that have more than 256 MB of RAM are produced by manufacturer D? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.manufacturer = D AND c.ram > 256; 10. Which laptop models cost less than $1200? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND c.price < 1200; 11. Which manufacturers produce personal computers and printers? SELECT DISTINCT p1.manufacturer p1, product p2 WHERE p1.manufacturer = p2.manufacturer AND p1.type = pc AND p2.type = printer ; 12. Which manufacturers produce all the product types? Page 8 of 11
SELECT DISTINCT manufacturer WHERE manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT type) >= ALL (SELECT Count(DISTINCT type) ); 13. Which manufacturers supply only personal computers? WHERE type = pc AND manufacturer IS NOT NULL EXCEPT WHERE type <> pc ; 14. Which manufacturers supply only one type of product? WHERE manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT type) = 1; 15. You want to buy: a personal computer with at least 1024 MB of RAM and a CD with 56x, and a laptop with a screen larger than 15, and a printer cheaper than $300 all produced by the same manufacturer. Which manufacturers should you consider? SELECT DISTINCT pc.manufacturer FROM (product natural JOIN computer) AS pc, (product natural JOIN computer ) AS l, (product JOIN printer ON product.model = printer.model) AS pr WHERE pc.manufacturer = l.manufacturer AND pc.manufacturer = pr.manufacturer AND pc.type = pc AND pc.ram >= 1024 AND pc.cd = 56x Page 9 of 11
AND l.type = laptop AND l.screen > 15 AND pr.price < 300; 16. What HD sizes are common to at least 2 computers? SELECT hd FROM computer GROUP BY hd HAVING Count(DISTINCT model) >= 2; 17. Which manufacturers sell more than one computer that has a processor speed of less than 3 GHz? natural JOIN computer WHERE speed <= 3 AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) > 1; 18. Which manufacturers sell the fastest computers? SELECT DISTINCT manufacturer natural JOIN computer WHERE speed = (SELECT Max(speed) FROM computer) AND manufacturer IS NOT NULL; 19. Which manufacturers do not sell laptops? SELECT DISTINCT manufacturer WHERE manufacturer NOT IN ( WHERE type = laptop ); 20. Which manufacturers offer at least 2 laptop models? WHERE type = laptop AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) >= 2; Page 10 of 11
21. Which manufacturers offer exactly 3 types of personal computers? WHERE type = pc AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) = 3; 22. You need to buy: a computer that has at least 1024 MB of RAM, a processor speed of 3 GHz or 3.5 GHz, a HD of at least 160 GB, and a color printer. Which manufacturers can offer you this combination and at what total price? SELECT DISTINCT p1.manufacturer, c.price + i.price AS total_price FROM computer AS c, product AS p1, printer AS i, product AS p2 WHERE p1.manufacturer = p2.manufacturer AND c.model = p1.model AND c.ram >= 1024 AND c.hd >= 160 AND ( c.speed = 3 OR c.speed = 3.5 ) AND i.model = p2.model AND i.color = true; Page 11 of 11