Sei sulla pagina 1di 14

InformationandDatabaseSystemsI(CIS4301)

(Spring2011) Instructor:Dr.MarkusSchneider
TA:GaneshViswanathan

Homework2Solutions
Topicscovered:SQL&RelationalAlgebra Due:Tuesday02/15/2011beforeclass

Name: UFID: Email: Pledge(MustbesignedinacceptanceofUFHonorCode): Onmyhonor,Ihaveneithergivennorreceivedunauthorizedaidindoingthisassignment. ______________________________________________ Signature

For scoring use only: Maximum Exercise I Exercise II Exercise III Exercise IV Exercise V Total 10 16 30 20 24 100 Received

Instructions:
You must attempt all questions. Attach SQL spool logs wherever applicable. Provide a hard-copy of your completed homework at the beginning of class on the due date shown above. Answers will be graded only for correctness and quality. Partial queries (SQL/RA) will only be graded if accompanied with a textual explanation of the attempt made in solving the query. Here is the grading breakdown: E-I: 5 x 2 points each. Total = 10 points. E-II: (A) 4 points, (B) 6 x 2 points each = 12 points. Total = 16 points. E-III: 12 x 2.5 points each. Total = 30 points. E-IV: 10 x 2 points each. Total = 20 points. E-V: 8 x 3 points each. Total = 24 points. Grand total = 100 points.

Exercise I (Knowledge Questions)


Answer the following questions in your own words. 1. What is Relational Algebra? Relational algebra is a procedural language which allows users to specify how a query has to be evaluated (execution plan). 2. What are aggregate functions? Aggregate functions are functions that take a set of values and that yield a single value in a result tuple 3. Briefly describe the grouping operation. Grouping is used to subdivide a relation into partitions according to a certain criterion (equality of partition attribute values) and to aggregate over each partition in a desired way. 4. What is a multi-relation (M-relation)? A multi-relation (M-relation) is based on the concept of a multiset, i.e., equal elements (tuple) may occur in a relation multiple times. The M-relation schema is equal to the respective relation schema (without duplicates in the relations). 5. Mention the five basic operations of the Relational Algebra. Which of these operations require the relations to be schema compliant? Some basic RA operations: union, difference, Cartesian product, project, select, rename. Union and difference operations require R and S to be schema compliant.

Exercise II (SQL - I)
Consider an agricultural database being built for a small garden that gets planted in Spring and is picked in Summer. The sunlight attribute refers to the percentage of a 24 hour day that the location gets sunlight and the plant optimally wants sunlight. The water attribute refers to the percentage of average rainfall that makes it to the root level for a location or is optimal for a plant. The planted (average expected) and picked (actual) weight is in kilograms. The picked amount is the number of items (one carrot, one beet, an ear of corn, one tomato, one radish) picked. (A) Create the following instance of the database schema in Oracle SQL. Refer to the class webpage for instructions on connecting to the CISE Oracle database. Use appropriate data types and formatting (e.g., for date) to match the provided schema. Provide the spool log file.
Location -------Name Sunlight ----------East .28 North .17 West .38 South .45 Plant ----Name Sunlight ----------Carrot .26 Beet .44 Corn .44 Tomato .42 Radish .28 Gardener -------GardenerID Name ---------- ---0 Mother 1 Father 2 Tim 3 Erin

LocationID ---------0 1 2 3

Water ----.80 .84 .48 .66

| | | | | |

Age --36 38 15 12

PlantID ------0 1 2 3 4

Water ----.82 .80 .76 .80 .84

Weight ----.08 .04 .26 .16 .02

PlantFK ------0 0 1 2 2 3 4

Planted ------GardenerFK LocationFK Date1 ---------- ------------0 0 04-18-2010 1 1 04-14-2010 0 2 04-18-2010 1 3 04-14-2010 2 2 04-19-2010 3 3 04-25-2010 2 0 04-30-2010

Seeds ----28 14 36 20 12 38 30

PlantFK ------0 0 2 2 3 4

Picked -----GardenerFK LocationFK Date2 ---------- ------------2 0 08-18-2010 3 1 08-16-2010 1 3 08-22-2010 2 2 08-28-2010 3 3 08-22-2010 2 0 07-16-2010

Amount -----28 12 52 18 15 23

Weight -----2.32 1.02 12.96 4.58 3.84 0.52

The required tables are created and the constraints are applied. Custom format the DATE using the command: SQL> ALTER SESSION SET NLS_DATE_FORMAT='mm-dd-yyyy';

(B) Now answer the following queries using the tables created. 1. Write a valid SQL statement that calculates the total weight of all ears of corn that were picked from the garden. Show the result set. SQL> SELECT SUM(Picked.Weight) AS corn_weight FROM Picked, Plant WHERE Picked.PlantFK = Plant.PlantID and Plant.Name = Corn;
CORN_WEIGHT ----------17.54

2. Using a multiplicative conversion factor of 2.2 to convert kilograms to pounds, write a valid SQL statement that shows the weight of the picked crop in total pounds per plant type. Show the result set. SQL> SELECT P.Name, SUM(2.2*Pc.Weight) AS Pounds FROM Plant P, Picked Pc WHERE P.PlantID=Pc.PlantFK GROUP BY P.Name;
NAME POUNDS ---------- ---------Carrot 7.348 Corn 38.588 Radish 1.144 Tomato 8.448

3. Write a valid SQL statement that would produce a result set like the following:
NAME ---------Tim Tim Tim NAME ---------Radish Carrot Corn PICKED_DAT AMOUNT ---------- ---------07-16-2010 23 08-18-2010 28 08-28-2010 18

SQL> SELECT Gardener.Name, Plant.Name, Picked.Picked_Date, Picked.Amount FROM Picked, Gardener, Plant WHERE Plant.PlantId = Picked.PlantFK AND Gardener.GardenerId = Picked.GardenerFK AND Picked.GardenerFK = 2 ORDER BY Picked.Picked_Date; 4. Run the following valid VIEW SQL statement to produce a table in memory: CREATE VIEW weights AS SELECT plant.name, picked.weight as picked_weight, picked.amount as amount, plant.weight as weight FROM picked, plant WHERE picked.plantfk = plant.plantid; This command creates the following view:
NAME PICKED_WEIGHT AMOUNT WEIGHT ---------- ------------- ---------- ---------Carrot 2.32 28 .08 Carrot 1.02 12 .08 Corn 12.96 52 .26 Corn 4.58 18 .26 Tomato 3.84 15 .16 Radish .52 23 .02 6 rows selected.

Note that view can be used just like a table in a query. Now using this view, write a valid SQL statement to find the variation between picked and expected weight for each plant, to produce a result set like the following:
NAME EXPECTED_WEIGHT PICKED_WEIGHT VARIANCE ---------- --------------- ------------- ---------Corn 13.52 12.96 -.56 Corn 4.68 4.58 -.1 Carrot .96 1.02 .06 Radish .46 .52 .06

Carrot Tomato 6 rows selected.

2.24 2.4

2.32 3.84

.08 1.44

SQL> SELECT name, (amount * weight) AS expected_weight, picked_weight, (picked_weight-(amount * weight)) AS variance FROM weights ORDER BY variance; 5. Let us create a Carrot Planting Water-Analysis Report. Write a valid SQL statement that would produce a result set like the following:
NAME ---------Carrot Carrot Carrot Carrot NAME NEEDED AVAILABLE VARIANCE ---------- ---------- ---------- ---------East .82 .8 -.02 North .82 .84 .02 West .82 .48 -.34 South .82 .66 -.16

SQL> SELECT plant.name, location.name, plant.water AS needed, location.water AS available, location.water - plant.water AS variance FROM plant, location WHERE plant.name='Carrot'; 6. Write a valid SQL statement that calculates the average number of items produced per seed planted for each plant type (Plant Name). Output the average rounded to two decimal points. Show the result set. SQL> SELECT Plant.Name, CAST(SUM(Picked.Amount)/SUM(Planted.Seeds) AS NUMBER(4,2)) AS YIELD FROM Plant , Planted, Picked 2 WHERE Planted.PlantFK = Picked.PlantFK AND Planted.LocationFK = Picked.LocationFK 3 AND Plant.PlantID=Picked.PlantFK GROUP BY Plant.Name; Or, AVG() can be used.
NAME YIELD ---------- ---------Carrot .95 Corn 2.19 Radish .77 Tomato .39

Exercise III (SQL - II)


Consider the following relations: 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. Find the age of the oldest student who is either a Geography major or enrolled in a course taught by Henry Hudson. 3. Find the names of all classes that either meet in room E107 or have five or more students enrolled. 4. Find the names of all students who are enrolled in two classes that meet at the same time. 5. Find the names of faculty members who teach in every room in which some class is taught. 6. Find the names of faculty members for whom the combined enrollment of the courses that they teach is less than five. 7. For each level, print the level and the average age of students for that level. 8. For all levels except JR, print the level and the average age of students for that level. 9. For each faculty member that has taught classes only in room E107, print the faculty members name and the total number of classes she or he has taught. 10. Find the names of students enrolled in the maximum number of classes. 11. Find the names of students not enrolled in any class. 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). 1. SELECT DISTINCT S.Sname FROM Student S, Class C, Enrolled E, Faculty F WHERE S.snum = E.snum AND E.cname = C.name AND C.fid = F.fid AND F.fname = Henry Hudson AND S.level = SR; 2. SELECT MAX(S.age) FROM Student S WHERE (S.major = Geography) OR S.snum IN (SELECT E.snum FROM Class C, Enrolled E, Faculty F

WHERE E.cname = C.name AND C.fid = F.fid AND F.fname = Henry Hudson ); 3. SELECT C.name FROM Class C WHERE C.room = E107 OR C.name IN (SELECT E.cname FROM Enrolled E GROUP BY E.cname HAVING COUNT (*) >= 5); 4. SELECT DISTINCT S.sname FROM Student S WHERE S.snum IN (SELECT E1.snum FROM Enrolled E1, Enrolled E2, Class C1, Class C2 WHERE E1.snum = E2.snum AND E1.cname <> E2.cname AND E1.cname = C1.name AND E2.cname = C2.name AND C1.meets at = C2.meets at); 5. SELECT DISTINCT F.fname FROM Faculty F WHERE NOT EXISTS (( SELECT * FROM Class C ) EXCEPT (SELECTC1.room FROM Class C1 WHERE C1.fid = F.fid )); 6. SELECT DISTINCT F.fname FROM Faculty F WHERE 5 > (SELECT COUNT (E.snum) FROM Class C, Enrolled E WHERE C.name = E.cname AND C.fid = F.fid); 7. SELECT S.level, AVG(S.age) FROM Student S GROUP BY S.level; 8. SELECT S.level, AVG(S.age) FROM Student S WHERE S.level <> JR GROUP BY S.level; 9. 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); 10. 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 E2 GROUP BY E2.snum )); 11. SELECT DISTINCT S.sname FROM Student S WHERE S.snum NOT IN (SELECT E.snum FROM Enrolled E ); 12. SELECT S.age, S.level FROM Student S GROUP BY S.age, S.level, HAVING S.level IN (SELECT S1.level FROM Student S1 WHERE S1.age = S.age GROUP BY S1.level, S1.age HAVING COUNT (*) >= ALL (SELECT COUNT (*) FROM Student S2 WHERE s1.age = S2.age GROUP BY S2.level, S2.age));

10

Exercise IV (Relational Algebra - I)


Consider the following schema: Suppliers (sid: integer, sname: string, address: string) Parts (pid: integer, pname: string, color: string) Catalog (sid: integer, pid: integer, cost: real) The key fields are underlined and the domain of each field is listed after the field name. The catalog relation lists the prices charged for parts by suppliers. Write the following queries in relational algebra. 1. Find the names of suppliers who supply some red part. 2. Find the sids of suppliers who supply some red or green part. 3. Find the sids of suppliers who supply some red part or are at 221 Packer Street.. 4. Find the sids of suppliers who supply every part. 5. Find the sids of suppliers who supply every red part. 6. Find the sids of suppliers who supply every red or green part. 7. Find the sids of suppliers who supply every red part or supply every green part. 8. Find pairs of sids such that the supplier with the first sid charges more for some part than the supplier with the second sid. 9. Find the pids of parts supplied by at least two different suppliers. 10. Find the pids of the most expensive parts supplied by suppliers named Yosemite Sham. Answers: sname(sid((pidcolor=red Parts) 1) 2) 3)

Catalog)

Suppliers) Catalog)

sid(pid(color=redcolor=greenParts) R1(sid (pid(color=red Parts) Catalog)) R2(sid(address=221PackerStreet Suppliers))


R1R2 (sid, pid Catalog) / (pid Parts) (sid, pid Catalog) / (pidcolor=red Parts)

4) 5) 6) 7)

(sid, pid Catalog) / (pidcolor=red color=greenParts) R1(((sid, pid Catalog) / (pidcolor=red Parts)))

R2(((sid,
8) R1R2 R1(Catalog)

pid Catalog) / (pidcolor=green Parts)))

11

R2(Catalog) R1.sid, R2.sid(R1.pid=R2.pidR1.sidR2.sidR1.cost>R2.cost(R1


R2)) 9)

R1(Catalog) R2(Catalog) R1.pidR1.pid=R2.pidR1.sidR2.sid(R1 R2) R1(sid sname =Yosemite ShamSuppliers) R2(R1 Catalog) R3(R2) R4(1 sid, 2 pid, 3 cost)(R3.cost<R2.cost(R3 R2)) pid (R2 pid,costR4)

10)

12

Exercise V (Relational Algebra - II)


Consider the following relations containing airline flight information: Flights (flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time) Aircraft (aid: integer, aname: string, cruisingrange: integer) Certified (eid: integer, aid: integer) Employees (eid: integer, ename: string, salary: integer) Note that the Employees relation describes pilots and other kinds of employees as well. Every pilot is certified for some aircraft (otherwise, he or she would not qualify as a pilot) and only pilots are certified to fly. Write the following queries in relational algebra. 1. Find the eids of pilots certified for some Boeing aircraft. 2. Find the names of pilots certified for some Boeing aircraft. 3. Find the aids of all aircraft that can be used on non-stop flights from Bonn to Madras. 4. Identify the flights that can be piloted by every pilot whose salary is more than $100,000. 5. Find the names of pilots who can operate planes with a range greater than 3,000 miles but are not certified on any Boeing aircraft. 6. Find the eids of employees who make the highest salary. 7. Find the eids of employees who make the second highest salary. 8. Find the eids of employees who are certified for exactly three aircraft. Answers: eid(aname=Boeing(Aircraft 1) 2) 3) 4) 5)

Certified))

ename(aname=Boeing (Aircraft Certified Employees)) BonnToMadrid (from=Bonn to=Madrid (Flights)) aid(cruisingrange>distance(Aircraft BonnToMadrid)) flno(distance <cruisingrange salary>100,000(Flights ename(Employees
Aircraft Certified Employees))) R1(eid (cruisingrange >3000(Aircraft Certified)))

(R1 eid(aname=Boeing (Aircraft Certified)))) 6) The approach to take is first find all the employees who do not have the highest salary. Subtract these from the original list of employees and what is left is the highest paid employees. E1(Employees)

13

E2(Employees) E3(E2.eid(E1 (eidE1) E3

F(E1.salary>E2.salary) E2)

7) The approach taken is similar to the solution for the previous exercise. First find all the employees who do not have the highest salary. Remove these from the original list of employees and what is left is the highest paid employees. Remove the highest paid employees from the original list. What is left is the second highest paid employees together with the rest of the employees. Then find the highest paid employees of this new list. This is the list of the second highest paid employees. E1(Employees)

E2(Employees) E3(E2.eid(E1 E4(E2 E3) E5(E2 E3) E6(E5.eid(E4 (eidE3) E6

F(E1.salary>E2.salary) E2)

F(E1.salary>E5.salary) E5)

8) The approach behind this query is to first find the employees who are certified for at least three aircraft (they appear at least three times in the Certified relation). Then find the employees who are certified for at least four aircraft. Subtract the second from the first and what is left is the employees who are certified for exactly three aircraft. R1(Certif ied)

R2(Certif ied) R3(Certif ied) R4(Certif ied) R5(eid( (R1.eid=R2.eid=R3.eid)(R1.aid_=R2.aid_=R3.aid)(R1 R6(eid(

R2 R3)))
(R1.eid=R2.eid=R3.eid=R4.eid)(R1.aid_=R2.aid_=R3.aid_=R4.aid)(R

1 R2 R3 R4))) R5 R6 ***

14

Potrebbero piacerti anche