(Print in caps) Last Name............................................ First Name .............................................. Student ID # ............................................ Signature ................................................. CS 143 DATABASE SYSTEMS - FALL 2003 Professor Melkanoff MIDTERM EXAMINATION Wednesday May 7, 2003 Question # 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Total Maximum possible pts 8 6 7 8 8 7 7 8 6 6 7 7 8 7 100 Actual points - For multiple choice questions requiring a single answer place a circle around the single correct answer. For multiple choice questions requiring multiple answers, follow directions. 1 General advice: Do not assume a query has a trivial syntactic error and therefore produces nothing. Call the instructor if you think there is a typo or an error. Relations mentioned in the queries may have attributes not mentioned, but their existence should not affect the answer. Relations may have NULL's unless stated otherwise. The result of SQL queries is generally a multiset, but the result of a query in relational algebra is a set. Initially, tables do not contain tuples unless so stated explicitly. 2 Questions 1, 2, and 3 all start from the following SQL declarations: CREATE TABLE Emp ( eid INTEGER PRIMARY KEY, name CHARACTER (30), level CHARACTER (10)); CREATE TABLE Dept ( did INTEGER PRIMARY KEY, school CHARACTER (30), mgr INTEGER. FOREIGN KEY mgr REFERENCES Emp(eid) ): CREATE TABLE Works_in ( eid INTEGER FOREIGN KEY REFERENCES Emp (eid) , did INTEGER FOREIGN KEY REFERENCES Dept (did), PRIMARY KEY (eid, did ) ); Hint: Employees work in departments and some of them also manage departments. Question 1 (8 points) Draw the ER diagram from which these schemas could have been produced. name Emp level did eid Works_in school eid Dept mgr 3 Question 2 (6 points) If we add the constraint that each employee must work in at least one department, redraw the ER diagram so as to properly reflect this constraint. name did eid Emp level Works_in school eid Dept mgr Question 3 (7 points) Show how the additional constraint stated in Question 2 may be included in the initial declarations. CREATE ASSERTION AA ( CHECK NOT EXISTS (SELECT * FROM Emp E WHERE E.eid NOT IN (SELECT W.eid FROM Works_in )) ) 4 Given the following relation describing students, courses and course enrollments: STUDENTS ( sid, sname, major) ENROLLED(sid, courseid, grade) COURSES(courseid, units); writhe the following query as indicated: Write the names of students who are taking exactly three courses. Question 4 (8 points) In SQL: SELECT S.sname FROM STUDENTS S, ENROLLED E WHERE E.sid = S.sid GROUP BY E.sid HAVING COUNT (*) = 3 ; Question 5 (8 points) In Relational Algebra: [USING cid FOR courseid FOR BREVITY] E2 = E1 ⋈E1.sid=Esid⋀E1.cid<>Ecid E ρ (C ( 2→cid1, 5→cid2), E2) E3 = E2⋈E2.sid=E.sid⋀E2.cid<>E.cid⋀E2.cid2<>E.cid E ρ (C ( 8 →cid3 ) E3 ) E4 = E3 ⋈E3.sid=E.sid⋀E3.cid1<>E.cid⋀E3.cid2⋀E.cid⋀E3cid3<>E.cid E πsnane (( πsid ( E3 ) – (πsid (E4) ⋈ S ) 5 The following two questions refer to the Sailors/Boats/Reserves database described in the text and which we have been using in class. Express the stated query in SQL. Question 6 (7 points) For each rating, write the number of sailors who have reserved all the red boats. SELECT S.rating, COUNT (*) FROM Sailors S WHERE NOT EXISTS (SELECT * FROM Boats B WHERE B.color = ‘red’ AND B.bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid = S.sid)) Question, 7 (7 points) For each date showing in Reserves, how many boats were not used? SELECT R.daY, COUNT (DISTINCT B.bid) FROM Reserves R, Boats B WHERE B.bid NOT IN (SELECT R1.bid FROM Reserves R1 WHERE R1, day = R.day) GROUP BY R.day 6 Question 8 (8 points) Given the following ER model diagram, write the SQL statements which can be used to build the corresponding relational database, combining tables as much as possible without introducing unnecessary redundancies p a d b A D B q E do not include any types for the attributes c C r CREATE TABLE A (a, p, PRIMARY KEY (a)) CREATE TABLE BD (b, q, d, a NOT NULL, PRIMARY KEY (b) FOREIGN KEY (a) REFERENCES A) CREATE TABLE C (c, r, PRIMARY KEY (c)) CREATE TABLE E( a, b, c, PRIMARY KEY (a, b, c), FOREIGN KEY (a) REFERENCES A, FOREIGN KEY (a) REFERENCES B, FOREIGN KEY (a) REFERENCES C) CREATE ASSERTION A_in_BD CHECK NOT EXISTS ( SELECT a FROM A WHERE a NOT IN ( SELECT a FROM BD )) 7 Question 9 ( 6 points) Given the following three relations R (a, b) = {<1, 3>, <3, 3>, <5, 6>, <6, 3>} S (b, c) = {<2, 4>, <3, 4>, <3, 8>, <6, 4>, < 6, 8>} T (c, d) = {<4, 4>, <4, 8>, <5, 4>, <8, 9>} The number of tuples in R ⋈ S ⋈ T where ⋈ is the natural join is: (a) 4 (b) 6 (c) 12 (d) 13 Question 10 ( 6 points) Place a check mark next to all the equations below which hold true for sets, but not necessarily for multisets, a cross for those that hold for both and leave blank otherwise. R R R R ∩(S∩R) ∩(S∩R) ∪ ( S ∩ R) ∪ ( S ∩ R) = = = = ( ( ( ( R R R R ∩S)∩R ∩S)∩S ∪S)∩R ∪S)∩S ........ ......... ........ ........... 8 Question 11 ( 7 points) Consider the following three schema declarations. You may assume that all columns have the same type which has been omitted for brevity. Schema 1: CREATE TABLE R(A PRIMARY KEY, B) CREATE TABLE S(C, D REFERENCES R(A)); Schema 2: CREATE TABLE R(A PRIMARY KEY, B) CREATE TABLE S(C, D CHECK ( D IN SELECT A FROM R ))); Schema 3: CREATE TABLE R(A PRIMARY KEY, B) CREATE TABLE S(C, D) CREATE ASSERTION Assert CHECK( NOT EXISTS (SELECT * FROM S WHERE D NOT IN (SELECT A FROM R))); Which of the following statements is true? (Circle one only.) (a)All three schemas are equivalent in terms of their behavior. (b) Schemas 1 and 2 are equivalent but schema 3 is different. (c) Schemas 1 and 3 are equivalent but schema 2 is different. (d) Schemas 2 and 3 are equivalent but schema 1 is different. (e) None of the three schemas are equivalent. 9 Question 12 ( 7 points) In SQL 3-valued logic the value of the expression R.a > R.b AND R. a <= 0 AND R.b >=0 can be (a) only TRUE or FALSE (b) only FALSE or UNKNOWN (c) only TRUE or UNKNOWN (d) Any of TRUE, FALSE, or UNKNOWN Condition A: R.a > R.b Condition B: R.a <= 0 Condition C: R.b >= 0 A = T, whereby B = F and C = F, or A = F, whereby B = T and C = T, or A = U, whereby B = U and C = U a b a AND b A OR b TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE UNKNOWN UNKNOWN TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE UNKNOWN FALSE UNKNOWN UNKNOWN TRUE UNKNOWN TRUE UNKNOWN FALSE FALSE UNKNOWN UNKNOWN UNKNOWN UNKNOWN UNKNOWN As may be seen from the Truth table, a AND b can only be true when both a and b are true. Since this is impossible, the only possible values for the expression are FALSE or UNKNOWN. 10 Question 13 (8 points) Given the following ER model diagram, write the necessary SQL declarations combining tables as much as possible without excessive redundancies. q p a A D B b d CREATE TABLE ABD ( a, b, p, q, d, UNIQUE (a), PRIMARY KEY (b) ) 11 Question 14 (7 points) Q1: SELECT R1.a, R3.a FROM R R1, R R2 , R R3 WHERE R1.a = R2.a AND R2.b = R3.b AND R1.b <> R2.b; Q2: SELECT R1.a, R3.a FROM R R1, R R2, R R3 WHERE R1.b = R2.b AND R2.a = R3.a AND R2.b <> R3.b; (a) Q1 and Q2 produce the same answer. (b) The answer to Q1 is always contained in the answer to Q2. (c) The answer to Q2 is always contained in the answer to Q1. (d) Q1 and Q2 produce different answers. The only difference between Q1 and Q2 is an exchange of labels between R1 and R3. 12