Docsity

Database Management Systems - Assignment 4 | COP 5725, Assignments of Computer Science

Toggle sidebar
bg1
fDatabase Management Systems (COP 5725)
(Fall 2008)
Instructor: Dr. Markus Schneider
TA:
Mingsong Chen
Wen jie Yua n
Homework 4
Solutions
Name:
UFID:
Email Address:
Pledge (Must be signed according to UF Honor Code)
On my honor, I have neither given nor received unauthorized aid in doing this
assignment.
_______________________________________________Signature
For scoring use only:
Maximum Received
Exercise 1 14
Exercise 2 36
Exercise 3(a) 15
Exercise 3(b) 35
Total 100
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Database Management Systems - Assignment 4 | COP 5725 and more Assignments Computer Science in PDF only on Docsity!

fDatabase Management Systems (COP 5725)

(Fall 2008)

Instructor: Dr. Markus Schneider

TA:

Mingsong Chen Wenjie Yuan

Homework 4

Solutions

Name: UFID: Email Address:

Pledge (Must be signed according to UF Honor Code)

On my honor, I have neither given nor received unauthorized aid in doing this assignment.

_______________________________________________Signature

For scoring use only:

Maximum Received Exercise 1 14

Exercise 2 36

Exercise 3(a) 15 Exercise 3(b) 35

Total 100

Exercise 1 (Knowledge) [14 points]

  1. What is a view? What conditions are needed when a view can be updated? [5 points]
  2. What are DDL and DML? [4 points]
  3. Describe in your own words how grouping works and how its interplay with aggregate functions is. [5 points] (refer to slides)

football_ players fid fname rating age _id P._S

football_ players fid fname rating age ┐ _id >4 <

  1. Print average ages by rating. football_ players fid fname rating age G.P _A P.AVG._A

  2. Print ratings for which the average age is more than 30. football_ players fid fname rating age G.P _A

Conditions AVG._A > 30

  1. Print the fids of football players who have reserved all playgrounds for which there is some reservation football_ players fid fname rating age P.G._id

Reserves fid pid day _id _B _B

Conditions COUNT._B1 =COUNT._B

  1. Print the names of football players who are younger than 20 or older than 30. football_ players fid fname rating age P. _A

Conditions _A<20 OR 30<_A

  1. insert information into the database about a new football player Janice whose id is 74, rating is 7 and age is 41. football_ players fid fname rating age I. 74 Janice 7 41

  2. Update the age of the football player with sid 74 to be 42 years. football_ players fid fname rating age 74 U.

Exercise 3 (SQL) [50 points]

(A) Two queries are said to be equivalent if they give the same answer for every instance of the database. Consider the following database: [15 points]

government(name, rank, salary) politician(name, party, gender) donation(name, organization, amount)

For each of the following five pairs of SQL queries, determine whether they are equivalent. If they are not, give a database instance as a counter-example. Otherwise, just state that they are equivalent.

o select name from donation A where not exists (select * from donation B where name = 'Campbell' and not exists (select * from donation C where C.organization = B.organization and C.name = A.name)) o select name from donation A where not exists (select * from donation where name = 'Campbell' and organization not in (select organization from donation B where B.name = A.name))

Yes, they are equivalent.

o (select name from government where salary >= 100000) union (select name from government where salary < 100000)

Charlie 150000

Don 50000

Then, the first query returns Albert, Charlie; but the second query returns empty set

o select name from politician where name not in (select name from government) o select name from politician where not exists (select * from government where government.name = politician.name)

Yes, they are equivalent.

o select name from politician where name not in (select name from government) o select politician.name from politician, government where not politician.name = government.name

No, a counterexample is:

politician relation

name

Albert

Charlie

government relation

name

Albert

Bobbie

Then, the first query returns Charlie but the second query returns Albert and Charlie

(B) Consider the following relations: [35 points]

Student ( snum , sname, major, level, age) Class ( name , meets_at, room, fid) Enrolled ( snum, cname ) Faculty ( fid , fname, deptid)

Write the following queries in SQL. Eliminate duplicates, and explicitly specify the keys for join operations.

  1. Find the names of all seniors (level = SR) who are enrolled in a class taught by Henry Hudson. [2 points]
  2. Find the age of the oldest student who is either a Geography major or enrolled in a course taught by Henry Hudson. [3points]
  3. Find the names of all classes that either meet in room E107 or have five or more students enrolled. [3points]
  4. Find the names of all students who are enrolled in two classes that meet at the same time. [3points]
  5. Find the names of faculty members who teach in every room in which some class is taught. [3points]
  6. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five. [3points]
  7. For each level, print the level and the average age of students for that level. [3points]
  8. For all levels except JR, print the level and the average age of students for that level. [3points]
  9. For each faculty member that has taught classes only in room E107, print the faculty member’s name and the total number of classes she or he has taught. [3points]
  10. Find the names of students enrolled in the maximum number of classes. [3points]
  11. Find the names of students not enrolled in any class. [3points]
  12. For each age value that appears in Students, find the level value that appears most often. For example, if there are more FR level students aged 18 than SR, JR, or SO students aged 18, you should print the pair (18, FR). [3points]
  1. SELECT S.level, AVG(S.age) FROM Student S GROUP BY S.level;
  2. SELECT S.level, AVG(S.age) FROM Student S WHERE S.level <> ‘JR’ GROUP BY S.level;
  3. SELECT F.fname, COUNT(*) AS CourseCount FROM Faculty F, Class C WHERE F.fid = C.fid GROUP BY F.fid, F.fname HAVING EVERY ( C.room = ‘E107’);
  4. SELECT DISTINCT S.sname FROM Student S WHERE S.snum IN (SELECT E.snum FROM Enrolled E GROUP BY E.snum HAVING COUNT () > = ALL (SELECT COUNT () FROM Enrolled E GROUP BY E2.snum ));
  5. SELECT DISTINCT S.sname FROM Student S WHERE S.snum NOT IN (SELECT E.snum FROM Enrolled E );
  6. SELECT S.age, S.level FROM Student S GROUP BY S.age, S.level, HAVING S.level IN (SELECT S1.level FROM Student S WHERE S1.age = S.age GROUP BY S1.level, S1.age HAVING COUNT () > = ALL (SELECT COUNT () FROM Student S WHERE s1.age = S2.age GROUP BY S2.level, S2.age));