Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354 1. (10) Convert the below E/R diagram to a relational database schema, using each of the following approaches: (3)The straight E/R Method Depts(name, chair) Courses(number, deptname, room) LabCourses(number, deptname, allocation) (4)The object-oriented method Lab Courses has all the attributes of Courses. Depts(name, chair) Courses(number, deptname, room) LabCourses(number, deptname, room, allocation) (3) The nulls method Courses and LabCourses are combined into one relation. Depts(name, chair) Courses(number, deptname, room, allocation)
2. (10) Design a database for a bank, including information about customer and their accounts. Information about a customer includes their name, address, phone, and Social Security number. Accounts have numbers, types (e.g., saving, checking) and balances. Also record the customer(s) who own an account. a. (3)Draw the UML diagram for this database. b. (3)Change your diagram so an account can have only one customer. c. (2)Further change your diagram so a customer can have only one account. d. (2)Change your original diagram so that a customer can have a set of address (which are street-city-state triples) and a set of phones.
3. (10) Computing the following: on the two given relations. (You can write answer in table format if you find it easier) R(A,B): {(0,1), (2,3), (0,1), (2,4), (3,4)} S(B,C): {(0,1), (2,4), (2,5), (3,4),(0,2), (3,4)} Duplicate-elimination Operator Turn a bag into a set by eliminating all but one copy of each tuple Aggregation Operators SUM Return the sum of a column with numerical values AVG Return the average of a column with numerical values MIN Return smallest value for column with numerical values MAX Return largest value for column with numerical values COUNT Return the number of values in a column (including duplicates) Grouping Operator Subscript L is a list is a list of attributes of R, L can have following kinds of element: 1. A single attribute of R 2. An expression x->y, rename attribute x to y 3. An expression E->z, E is an expression, and rename E to z. Extended Projection Subscript L is a list is a list of attributes of R, L can have following kinds of element: 1. A single attribute of R 2. An expression x->y, rename attribute x to y 3. An expression E->z, E is an expression, and rename E to z. Sorting Operator Sorting the tuples of R by their value of C, and tuples with the same C-value are ordered by their B value. Outerjoin Operator Outerjoin is formed is formed by starting with R, and R S adding any dangling tuples from R or S. a. (2) (S) B+1 C-1 1 0 3 3 3 4 4 3 1 1 4 3
b. (2) (R) A B 0 1 2 3 2 4 3 4 d (3) (S) B AVG(C) 0 1.5 2 4.5 3 4 c. (3) R S A R.B S.B C 0 1 2 4 0 1 2 5 0 1 2 4 0 1 2 5 2 3 2 4 3 4 0 1 0 2
4. (10) This question introduces an example, concerning World War 2 capital ships. It involves the following relations: Classes(class, type, country, numguns, bore, displacement) Ships(name, class, launched) Battles(name, date) Outcomes(ship, battle, result) Ships are built in classes from the same design, and the class is usually named for the first ship of that class. The relation Classes records the name of the class, the type( bb for battleship or bc for battlecruiser), the country that build the ship, the number of main guns, the bore (diameter of the gun), and the displacement(weight, in tons). Relation Ships records the name of the ship, the name of its class, and the year in which the ship was launched. Relation Battles gives the name and date of battles involving these ships, and relation Outcomes gives the result (sunk, damaged, or ok) for each in each battle. Write the following SQL queries without subqueries a. (2) Find the ships heavier than 35,000 tons. SELECT S.name FROM Ships S, Classes C WHERE S.class = C.class AND C.displacement > 35000; b. (3) Find those countries that have both battleships and battlecruisers.(assuming the intersect operator is implemented) SELECT C1.country FROM Classes C1, Classes C2 WHERE C1.country = C2.country AND C1.type = 'bb' AND C2.type = 'bc' ; or SELECT country FROM Classes C1, WHERE C1.type = 'bb' Intersect SELECT country FROM Classes C1, WHERE C1.type = 'bc'
c. (5) Find those battles with at least three ships of the same country. SELECT O.battle FROM Outcomes O, Ships S, Classes C WHERE O.ship = S.name AND S.class = C.class GROUP BY C.country, O.battle HAVING COUNT(O.ship) > 3; 5. (10) Write the following SQL queries using at least one subquery (Based on the relations in question 4) a. (4) 6.3.2.a Find the countries whose ships had the largest number of guns. SELECT C.country FROM Classes C WHERE numguns IN (SELECT MAX(numGuns) FROM Classes ); SELECT C.country FROM Classes C WHERE numguns >= ALL (SELECT numguns FROM Classes); b. (6) Find the classes of ships, at least one of which was sunk in a battle. SELECT DISTINCT C.class FROM Classes C, Ships S WHERE C.class = S.class AND EXISTS (SELECT ship FROM Outcomes O WHERE O.result='sunk' AND O.ship = S.name ) ; SELECT DISTINCT C.class FROM Classes C, Ships S WHERE C.class = S.class AND S.name IN (SELECT ship FROM Outcomes O WHERE O.result='sunk' ) ;
6. (10) Write the following SQL queries. You can use any of the operators taught in the lectures (Based on the relations in question 4) a. (4) Find the average number of guns of battleship classes. SELECT AVG(C.numGuns) AS Avg_Guns FROM Classes WHERE type ='bb' ; b. (6) Find for each class the year in which the first ship of that class was launched. SELECT C.class, MIN(S.launched) AS First_Launched FROM Classes C, Ships S WHERE C.class = S.class GROUP BY C.class ;