Name:___________________ UNIVERSITY OF CALGARY Lab section: ________ FACULTY OF SCIENCE MIDTERM EXAMINATION CPSC 471 Closed book exam Total 100 points Page 1 of 9 CPSC 471 University of Calgary Department of Computer Science CPSC 471 Database Management Systems ID: Midterm Exam Before you start the exam, please note that: 1. Write you ID and name before your start 2. There are 8 pages with a total of 5 questions in this exam. 3. Read the five exam questions carefully. 4. The points for the problems are marked below. 5. Answer all the questions in the space provided. 6. You can use the back side of the page, if necessary 7. The problems are not necessarily arranged in the order of their difficulties. 8. You may do the ones that you feel comfortable first. 9. Partial solutions will be accepted for partial credits. Question Total Points 1 25 2 20 3 25 4 20 5 10 100 Result Page 2 of 9 CPSC 471 Q1. Consider the following relations that contain information about countries, cities, streets and houses. For each country, name, area and population are kept; and its neighbor countries are stored in Border; note that if (Canada, USA) is in Border then (USA, Canada) is not in Border. For each city, we keep city name, country name, area and population of the city. For each street, we keep street number, city name and length of the street. For each house we keep house number, number of rooms, owner name and street number. Country(name, area, population) Border(country_name1, country_name2) City(city_name, country_name, area, population) Street(stno, cityn, length) House(hno, #rooms, stno, owner-name) Code the following queries using Relational Algebra: (5) a) Find street number of the longest street in each city in Canada? Longest (city, max_ length) =[ cityn ℑmax(length) ( street Re sult = ∏ ( Longest stno >< city = cityn ∧ max_ length = length >< city _ name=cityn ( σ country _ name= 'Canada ' (city))] Street ) (5) b) Find names of persons who own at least one house in each city in Canada? R(cityn) = S= σ ∏ ( city _ name country _ name = 'Canada ' ∏ (City )) ( Street * House) cityn ,owner _ name Re sult = S ÷ R (5) c) Find names of persons who own at least one house outside USA? Re sult = ∏ ( σ owner _ name country _ name≠ 'USA' (City ) ∞ city _ name=cityn ( Street * House)) Page 3 of 9 CPSC 471 Country(name, area, population) City(city_name, country_name, area, population) House(hno, #rooms, stno, owner-name) Border(country_name1, country_name2) Street(stno, cityn, length) Code the following queries using SQL: (5) d) Find names of countries that have border with Germany? (SELECT country-name1 FROM border WHERE countryname2=’Canada’) UNION (SELECT country-name2 FROM border WHERE country-name1=’Canada’); (5) e) Find names of persons who own at least one house in Canada? SELECT FROM WHERE h.owner_name (City AS c JOIN Street AS s on c.city_name=s.cityn NATURAL JOIN House as h) c.country_name=’Canada’; Page 4 of 9 CPSC 471 Q2. Consider the following relations that contain information about books distributed by publishers to schools. A book may be published by several publishers and distributed to different schools. Here, pname and sname stand for publisher name and school name, respectively. Publisher(name, phone, city) Book(ISBN, title, color, #pages) School(name, phone, city, director) Distribute(pname, sname, ISBN, quantity) Code the following two queries using SQL: (5) a) Find names and cities of publishers distributing red books to at least one school not located in Calgary? SELECT FROM WHERE p.name, p.city (Publisher AS p JOIN Distribute AS d ON p.name=d.pname JOIN School as s ON d.sname=s.name JOIN Book as b ON d.ISBN=b.ISBN) b.color=’red’ AND s.city<>’Calgary’; (5) b) Find directors of the schools located in Paris and receiving books from publishers located in London? SELECT FROM WHERE s.director (Publisher AS p JOIN Distribute AS d ON p.name=d.pname JOIN School as s ON d.sname=s.name) s.city=’Paris’ AND p.city=’London’; Page 5 of 9 CPSC 471 Publisher(name, phone, city) Book(ISBN, title, color, #pages) School(name, phone, city, director) Distribute(pname, sname, ISBN, quantity) Code the following query using Relational Algebra: (5) c) Find names of schools receiving books from publishers located in Roma and distributing books to at least one school located in Toronto? P= ∏ σ (( Publisher . name ∏ Re sult = ∞ ( Publisher ) (P Distribute. sname ( Distribute )) Publisher .name = Distribute . pname city = ' Roma ' ∞ ∞ ( σ Distribute . sname = School . name city = 'Toronto ' Distribute) P .name= Distribute. pname (5) d) Find the name and total quantity of each book distributed only to all the schools in Calgary? S ( sname ) = ∏( σ name R= ∏ ( School )) city = 'Ca lg ary ' ( Distribute ) ISBN , sname T = R÷S Q = T − ∏ ((T * Distribute) ISBN ∞ ( σ School )) Distribute. sname= School .name city ≠ 'Ca lg ary ' Re sult (Title, Total )= title ℑsum ( quantity ) (Q * Distribute * Book ) ( School ))) CPSC 471 Q3. Consider the following ER diagram, derive the corresponding relational schema. OWNER ownerId CORPORATION Name Address Phone PERSON Ssn Address Phone EMPLOYEE Ssn Salary PILOT Name ownerId ownerId Shift Ssn Lic_Num Restr PLANE_TYPE Model Capacity Weight AIRPLANE Reg# Model Number HANGAR Number Capacity Location SERVICE Reg# Date OWNS ownerId Reg# Workcode Hours Pdate WORKS_ON Ssn Model Ssn Model FLIES Page 6 of 9 Page 7 of 9 CPSC 471 MAINTAIN Ssn Reg# Date Workcode Q4. You have been hired by COMSEL as a database designer. Their problem has been defined as follows and you are asked to draw the corresponding extended ER diagram, specifying your reasonable assumptions when necessary. COMSEL is running a business selling computer services. Services are classified into internet service and computing service. Internet service has a bandwidth and IP address. Computing service has MAC address, memory size and processor power. A user may ask for an internet service for unlimited period or may specify the period. Also a user has to specify the maximum memory size required for the computing service. COMSEL has representatives in different locations in Canada. A representative has name, phone, and technicians. A technician has name and specialization. A technician uses and keeps the amount of certain material in providing service to a user. A material has a name, cost, and supplier. It is the duty of each representative to keep track of the amount of material in the stock and to order from the supplier by specifying the date of order and the quantity. A supplier has name, location and phone. Each order leads to delivery in certain quantity and on certain date. Page 8 of 9 CPSC 471 Memory size bandwidth MAC address IP address Processor power Internet service Computing Service period use Ask for max memory User name location supplier Order amount phone delivery date Order date Supply order name delivery amount material use name amount phone technician location cost Representative name Work for specialization Page 9 of 9 CPSC 471 Q5. Consider relation R (A, B, C, D, E), which has two candidate keys: AB and AD List in the table below five valid tuples of R in a way that minimizes the total number of values in the five tuples, that is, Minimize [ number of values(A) + number of values(B) + number of values(C) + number of values(D) + number of values(E)]; Use symbols like a1, a2, …, b1, b2, …, A, B, C, D, and E, respectively. R A c1, c2, …, B d1, d2, …., and C D e1, e2, … as values for E a1 b1 c1 d1 e1 a1 b2 c1 d2 e1 a2 b1 c1 d1 e1 a2 b2 c1 d2 e1 a3 b1 c1 d1 e1 And another solution could be A= a1, a2,a3,a4,a5 and each of the rest has one single value. Both solutions lead to using 9 values