You are on page 1of 12

CS 348 Introduction to Database Systems (Fall 2012)

Midterm Exam 24 October 2012

Student Id: Student Name:

Instructions: 1. This is a closed book examination. 2. Exam duration is 2 hours (5:10PM-7:10PM). 3. Answer all the questions. 4. Answer each question in the space provided. 5. The exam is 8 (eight) pages; make sure you have all of the pages. Question 1 2 3 4 5 Total: Points 12 15 15 4 4 50 Score

Student ID: .................................

Student Name: ...................................................

Q1. (12 points) Use an Entity-Relationship Diagram to depict the following enterprise. Explain any additional assumptions you make and list any aspects you were not able to depict. A house is identied by a three-part address consisting of a number, street, and city. Each house also has a style (e.g., bungalow) and a set of colours. A person is identied by a social insurance number. For each person we record his/her name, age, and sex. Persons who are at least 18 years old may own zero or more houses, and every house is owned by at least one person. Any person (regardless of age) lives in at most one house as his/her principal residence, and a house can have zero or more persons living there. Solution:

Number

Street

City SIN Age Name

Address Style

House Colours

LivesIn

Person

Sex

COVER
Own

DISJOINT

Over 18

Under 18

Marking Scheme: There are two main entities, each worth 1 mark. The specialization worth 2 marks Two relations each worth 2 marks (you may lose marks for cardinality error) Attributes in each entity worth 1 mark Primary key worth .5 mark each Composite and multi-valued attributes worth .5 mark each

Q2. (15 points) Consider the E-R diagram shown in Figure 1. Convert this E-R model to a relational

CS 348

Page 2 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

one. You dont need to give SQL create statements. All you need to show are the relations, attributes and the key attribute(s). You can use a format such as the following: R(A1,A2,A3,A4, A5). However, indicate the foreign key-primary key relationships (not by arrows, but just textually). In case you choose from among a number of alternatives, indicate why you have made a particular decision. Note that, to conserve space, the attribute names are listed next to the attributes and relationships rather than with ovals. The underlined attributes are identiers. (You can use the back page to work out your solution, but the nal solution needs to appear in the space below.)
Name Address Phone Employer

Name, Code Course

Start date
Current Employment

Start Date End Date


PastEmp Type

SIN, Last name, Sex, Age, City of Birth Trainee


Attendance

Course Edition

Edition No, Start date, End date, No. of participants

DISJOINT

Teaching

Employee Position, Level

Professional Professional Title, Expertise

Instructor SIN, Last name, Phone, Age, City of birth

Figure 1: Figure for Question 2 Solution: I am developing the solution step-by-step, but you dont need to do that; all I asked was the nal set of relations. Step 1 Regular Entities: We map all of the regular entities to tables, renaming attributes when needed to ensure clarity (you may have chosen not to do that) and underlying keys as customary:

CS 348

Page 3 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

Employer(Name, Address, Phone) Trainee(SIN, Surname, Sex, Age, CityofBirth) Course(C-Code, Name) Instructor(SIN, Surname, Phone, Age, CityofBirth) Step 2 Weak Entities: We have a single weak entity: Course Edition. We map it to a table whose key is the composite key (C-Code, E-Code): CourseEdition(C-Code, E-Code, StartDate,EndDate, NoOfPart) Step 3 1:1 Relationships: Although we can do relationships in any order, it is good to do the 1:1 and 1:N ones rst as they do not need to be always mapped to a relation. So, we can get rid of them quickly. We have a single 1:1 relationship: Teaching. Remember that we resolve these by taking the primary key of the relation corresponding to the entity on the total participation side (i.e., cardinalty (1:1)) and making it a foreign key in the table corresponding to the entity on the other side. In this case, both of the entities have partial participation; so we have a choice. If we take the primary key of CourseEdition and make it a foreign key in Instructor, it becomes: Instructor(SIN, Surname, Phone, Age, CityofBirth, C-Code, E-Code) The alternative is to modify the CourseEdition to include Instructors primary key: CourseEdition(C-Code, E-Code, StartDate, EndDate, NoOfPart, I-SIN) It might be argued that the latter is a better solution, since CourseEdition is a weak entity and can be deleted if the corresponding Course entity is deleted. If the rst approach is chosen, this deletion would leave the foreign key null and some delete propagation policy has to be enforced. The latter approach does not have this diculty, so I will use that in the rest of the solution. (Note: I will accept both.) Step 4 1:N Relationships: We have the following 1:N relationships: Current Employment, Type, and Attendance. We resolve these by including the primary key of the relation on the N side, as a foreign key in the relation on the 1 side. Thus, Employee, Trainee and CourseEdition will be aected. However, the Current Employment relationship links Employer to a specialized type, so we leave its resolution until we deal with specializations. For Type relationship, the situation is somewhat interesting. Normally, we would take the primary key of Course (N side) and make it a foreign key of CourseEdition (1 side). However, CourseEdition is a weak entity dependent upon Course. So, it already contains the primary key of Course as a foreign key (in fact, as part of its own key). Therefore, we can leave it as is and we know that the two relations can be joined. For Attendance relationship, we take the key of CourseEdition and make it a foreign key of Trainee:

CS 348

Page 4 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

Trainee(SIN, Surname, Sex, Age, CityofBirth, C-Code, E-Code) Step 5 M:N Relationships We have one M:N relationship: Past Employment. We map it to a table: PastEmploy(Name, SIN, EmStDate, EmEndDate) Step 6 Specialization We need to take care of Trainees specialization into Employee and Professional. Note that Trainee table has already been created and it participates in a relationship with CourseEdition. Thus, Option 2 (creating only tables for specialized entities) is not an option. So, Option 1 should be chosen where two tables are created for the two specialized entities: Employee(SIN, Position, Level) Professional(SIN, ProfessionalTitle, Expertise) Step 4 revisited 1:N Relationships Now that we have taken care of Employee, we can go back and deal with Current Employment relationship. This is 1:N so, we modify the 1 side (Employee) by including in it, as foreign key, the primary key of the N side (Employer) and the attribute of the Current Employment relationship: Employee(SIN, Position, Level, Name) Thus the nal set of relations are: Employer(Name, Address, Phone) Trainee(SIN, Surname, Sex, Age, CityofBirth, C-Code, E-Code) Course(C-Code, Name) Instructor(SIN, Surname, Phone, Age, CityofBirth) CourseEdition(C-Code, E-Code, StartDate, EndDate, NoOfPart,I-SIN) PastEmploy(Name, SIN, EmStDate, EmEndDate) Employee(SIN, Position, Level, Name, StartDate) Professional(SIN, ProfessionalTitle, Expertise) The foreign key constraints are the following: I-SIN in CourseEdition is foreign key of Instructor C-Code in CourseEdition is foreign key of Course (C-Code, E-Code) in Trainee is foreign key of CourseEdition Name in PastEmploy is foreign key of Employer SIN in PastEmploy is foreign key of Trainee Name in Employee is foreign key of Employer

Marking scheme:

CS 348

Page 5 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

Handling each of the following correctly is worth 1 mark (8 marks total): Employer, Trainee, Course, CourseEdition, Instructor, PastEmp, Attendance, Teaching. (You lose 0.5 marks for incorrectly specifying primary keys) Handling the specialization with CurrentEmp relationship correctly is worth 1 mark in total. Specifying each foreign key correctly is work 1 mark (6 marks in total.) (You lose 0.5 marks for not specifying which relation the foreign key references) Q3. (15 points) Consider the following relations: Emp(eno, ename, title, city) Proj(pno, pname, budget, city) Works(eno, pno, resp, dur) Pay(title, salary) where the primary keys are underlined, and Emp.title is a foreign key to Pay.title, Works.eno is a foreign key to Emp.eno, and Works.pno is a foreign key to Proj.pno. For each part of this question (considered independently of the other parts), write a single SQL statement that accomplishes the given requirements. (a) (3 points) For each city, how many projects are located in that city and what is the total budget over all projects in the city? Solution: SELECT c i t y , count ( pno ) AS t o t p r o j , sum ( budget ) AS t o t b u d g e t F R O M Proj G R O U P BY c i t y Marking scheme: use count: 0.5 use sum: 0.5 group by: 0.5 select attributes: 0.5 overall correctness: 1.0

(b) (3 points) For each project, what fraction of the budget is spent (in total) on salaries for the people working on that project? Sort your answer by the value of the budget. Solution: Two alternative ways exist. The rst is the following: SELECT P . pno , pname , s a l / budget AS s a l f r a c F R O M Proj P,

CS 348

Page 6 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

(SELECT pno , sum ( s a l a r y ) AS s a l F R O M Works , Emp, Pay W H E R E Works . eno=Emp. eno A N D Emp. t i t l e =Pay . t i t l e G R O U P BY pno ) AS Q W H E R E P . pno=Q. pno O R D E R BY budget An alternative solution is the following: SELECT P . pno , pname , sum ( s a l a r y ) / budget AS s a l f r a c F R O M P r o j P , Works W, Emp E , Pay W H E R E P . pno= W. pno A N D W. eno=E . eno A N D E . t i t l e =Pay . t i t l e G R O U P BY P . pno , budget O R D E R BY budget Marking scheme: compute fraction properly: 0.5 order by: 0.5 join predicates (where clause): 0.5 group by: 0.5 select attributes: 0.5 overall correctness: 0.5

(c) (3 points) Remove the work assignment of all persons to any projects for which more than any 2 persons share the same responsibility. Solution: DELETE from Works W H E R E pno in (SELECT W. pno F R O M Works W G R O U P BY W. pno , W. r e s p HAVING count ( ) > 2) Marking scheme: use delete: 0.5 has the where condition in or = any etc.: 0.5 use subquery: 0.5 group by: 0.5 having: 0.5 overall correctness: 0.5

(d) (3 points) List all projects located in Toronto and include for each one the number of persons working on the project.

CS 348

Page 7 of 12

Midterm Exam

Student ID: ................................. Solution:

Student Name: ...................................................

SELECT P r o j . pno , count ( ) F R O M P r o j LEFT OUTER JOIN Works ON ( P r o j . pno=Works . pno ) W H E R E c i t y = Toronto G R O U P BY P r o j . pno , pname Marking scheme: use count(): 0.5 use outer join: 1.0 join condition: 0.5 group by: 0.5 overall correctness: 0.5

(e) (3 points) Insert into the Works relation all tuples that meet the following domain relational calculus expression: { e, p, r, d |en, pn, b, c(Emp(e, en, r, c) Proj(p, pn, b, c) d = 0)} Solution: INSERT INTO Works (SELECT eno , pno , t i t l e , 0 F R O M Emp, P r o j W H E R E Emp. c i t y=P r o j . c i t y ) Marking scheme: use insert into: 0.5 use subquery: 0.5 show condition on city: 1.0 select constant 0: 0.5 overall correctness: 0.5

Q4. (4 points) Formulate the following SQL queries in relational algebra. These are expressed over the same schema as given in the previous question (a) (2 points) SELECT pname , budget F R O M Proj , Works , Emp W H E R E t i t l e =Programmer A N D Works . eno = Emp. eno A N D Works . pno = P r o j . pno

CS 348

Page 8 of 12

Midterm Exam

Student ID: ................................. Solution: pname,budget (Proj

Student Name: ...................................................

Works

(title=Programmer Emp))

You can put the selection outside all of the joins if you wish; that would work too: pname,budget (title=Programmer (Proj Marking scheme: lose points: Works Emp))

This was straightforward to mark so I indicate where you

If you used cross product rather than join, you lose 0.5 marks. If you insert a projection that eliminates columns that you need in the outer projection, you lose 1 mark. If you missed one of the joins, you lost 1 mark If you got the join generally, but then made other errors (such as doing two joins and then trying to union them) you lost 1.5 marks.

(b) (2 points) SELECT pname F R O M Proj W H E R E budget > ALL ( Select budget F R O M Proj W H E R E c i t y = Waterloo ) Solution: This is the challenge question in the exam it requires some deep thinking. There are a number of ways to solve it; Ill explain them in some detail. 1. One way to tackle this is to rethink the query which asks for the names of projects whose budgets are greater than the projects of all projects located in Waterloo. The alternative way to think about this is as follows: Find the names of projects such that there is no project located in Waterloo that has a budget greater than it. How to write that query? Here is how to think about it: We have (budget > ALL Q) where Q is the inner query. Now, the following is always true: (budget > ALL Q) NOT NOT (budget > ALL Q) since two NOTs cancel each other. Now push one of the NOTs inside the parentheses to get NOT (NOT budget > ALL Q), which is equivalent to NOT (budget <= SOME Q). Thus the query becomes SELECT pname F R O M Proj W H E R E NOT ( budget <= SOME (SELECT budget

CS 348

Page 9 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

F R O M Proj W H E R E c i t y = Waterloo ) ) Now you can rewrite this query as a correlated subquery: SELECT pname F R O M P r o j P1 W H E R E NOT EXISTS (SELECT budget F R O M P r o j P2 W H E R E c i t y= Waterloo A N D P1 . budget <= P2 . budget ) Note that the subquery nds budgets of projects located in Waterloo if that project has a larger budget than what is found in the outer query. The not exists says that you want this subquery to return an empty result, in which case the projects found in the outer relation have budgets greater than all projects located in Waterloo. Now you can rewrite this query in relational algebra using set dierence: (a) First rename the Proj relation and nd all project names: T 1 = pname Proj (b) Now compute the entire nested query (outer and inner) except the not: T2 = pname ((budget budget1, Proj)
budget1<=budget2

budget2 ((budget budget2, city= Waterloo )(Proj)))

(c) Finally the result is everything in T1 that is not in T2 (due to not): T1 T2 2. The second approach follows these steps: (a) First get the projection for the relevant attributes (including the primary key): T 1 = pno,pname,budget Proj (b) Then nd the budgets of all projects which are located in Waterloo; rename the budget attribute to avoid name collision: T 2 = budgetbudget2 (budget (city= Waterloo Proj))) (c) Now nd the projects in T 1 that have budgets greater than the projects in T 2: T 3 = T 1 budget>budget2 T 2

CS 348

Page 10 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

(d) Now nd projects in T 3 that have budgets greater than all projects in T 2, and project on pname : pname (T 3 T 2) 3. A third approach goes as follows: (a) First nd the budgets of all projects located in Waterloo: T 1 = budget (city= Waterloo (Proj)) (b) Now start with the original Proj relation and nd all the projects there that have budgets smaller than or equal to that in T 1: T 2 = Proj
budget<=budget1

(budget budget1, T 1)

(c) Project out all the attributes in T 2 except for the attributes of Proj: T 3 = pno,pname,budget,city T 2 (d) Finally compute the result by nding all projects in the Proj table that are not in T 3: pname (Proj T 3) Marking scheme: This was a challenge question and the marking did not follow a detailed scheme. If I thought you were on the right track even if you did not get the solution, you got partial marks. Q5. (4 points) Consider the instance of the Sailors relation given in Figure 2 where the key is underlined. Show the results of the following queries. sid 22 29 31 32 58 64 71 74 85 95 sname Dustin Brutus Lubber Andy Rusty Horatio Zorba Horatio Art Bob rating 7 1 8 8 10 7 10 9 3 3 age 45.0 33.0 55.5 25.5 35.0 35.0 16.0 35.0 25.5 63.5

Figure 2: Database instance for Question 5 (a) (2 points)

CS 348

Page 11 of 12

Midterm Exam

Student ID: .................................

Student Name: ...................................................

SELECT S . s i d F R O M Sailors S W H E R E S . r a t i n g >= ALL (SELECT S2 . r a t i n g F R O M S a i l o r s S2 ) Solution: sid 58 71 Note: The result that is returned is a table. You need to specify the column title. (b) (2 points) SELECT S . r a t i n g , A V G( S . age ) AS avgage F R O M Sailors S G R O U P BY S . r a t i n g HAVING 1 < (SELECT C O U N T( ) F R O M SAILORS S2 W H E R E S . r a t i n g = S2 . r a t i n g ) Solution: rating avgage 3 44.5 7 40.0 8 40.5 10 25.5

Marking scheme: Very many of you got these right. If you did not specify column headings, you lost 0.5 marks. The result that is returned is a table, so you need to specify the column headings. If you made other minor errors, you lost 0.5 marks.

NOTE: THESE ARE EXAMPLES THAT ARE DISCUSSED IN THE BOOK.

CS 348

Page 12 of 12

Midterm Exam

You might also like