1 2007 Spring Database Systems Homework III 參考解答 1. Specify the following queries on the COMPANY relational database schema shown in Figure 5.5, using the relational operators discussed in this chapter. Also show the result of each query as it would apply to the database state of Figure 5.6. (a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. (b) List the names of employees who have a dependent with the same first name as themselves. (c) Find the names of employees that are directly supervised by 'Franklin Wong'. (d) For each project, list the project name and the total hours per week (by all employees) spent on that project. (e) Retrieve the names of employees who work on every project. (f) Retrieve the names of employees who do not work on any project. (g) For each department, retrieve the department name, and the average salary of employees working in that department. (h) Retrieve the average salary of all female employees. (i) Find the names and addresses of employees who work on at least one project located in Houston but whose department has no location in Houston. (j) List the last names of department managers who have no dependents. Answers: In the relational algebra, as in other languages, it is possible to specify the same query in multiple ways. We give one possible solution for each query. (a) EMP_W_X ← ( σσσσ PNAME='ProductX' (PROJECT)) PNUMBER=PNO (WORKS_ON) EMP_WORK_10 ← (EMPLOYEE) SSN=ESSN (σσσσ HOURS>10 (EMP_W_X)) RESULT ← Π LNAME,FNAME (σσσσ DNO=5 (EMP_WORK_10)) Result: LNAME FNAME Smith John English Joyce2 (b) E ← (EMPLOYEE) SSN=ESSN AND FNAME=DEPENDENT_NAME (DEPENDENT) R ← Π LNAME,FNAME (E) Result (empty): LNAME FNAME (c) WONG_SSN ← Π SSN (σσσσ FNAME='Franklin' AND LNAME='Wong' (EMPLOYEE)) WONG_EMPS ← (EMPLOYEE) SUPERSSN=SSN (WONG_SSN) RESULT ← Π LNAME,FNAME (WONG_EMPS) Result: LNAME FNAME Smith John Narayan Ramesh English Joyce (d) PROJ_HOURS(PNO,TOT_HRS) ← PNO ℑ SUM HOURS (WORKS_ON) RESULT ← Π PNAME,TOT_HRS ( (PROJ_HOURS) PNO=PNUMBER (PROJECT) ) Result: PNAME TOT_HRS ProductX 52.5 ProductY 37.5 ProductZ 50.0 Computerization 55.0 Reorganization 25.0 Newbenefits 55.0 (e) PROJ_EMPS(PNO,SSN) ← Π PNO,ESSN (WORKS_ON) ALL_PROJS(PNO) ← Π PNUMBER (PROJECT) EMPS_ALL_PROJS ← PROJ_EMPS ÷ ALLPROJS RESULT ← Π LNAME,FNAME (EMPLOYEE * EMP_ALL_PROJS) Result (empty): LNAME FNAME (f) ALL_EMPS ← Π SSN (EMPLOYEE) WORKING_EMPS(SSN) ← Π ESSN (WORKS_ON)3 NON_WORKING_EMPS ← ALL_EMPS - WORKING_EMPS RESULT ← Π LNAME,FNAME (EMPLOYEE * NON_WORKING_EMPS) Result (empty): LNAME FNAME (g) DEPT_AVG_SALS(DNUMBER,AVG_SAL) ← DNO ℑ AVG SALARY (EMPLOYEE) RESULT ← Π DNAME,AVG_SAL ( DEPT_AVG_SALS * DEPARTMENT ) Result: DNAME AVG_SAL Research 33250 Administration 31000 Headquarters 55000 (h) RESULT(AVG_F_SAL) ← ℑAVG SALARY (σσσσ SEX='F' (EMPLOYEE) ) Result: AVG_F_SAL 31000 (i) E_P_HOU(SSN) ← Π ESSN (WORKS_ON PNO=PNUMBER (σσσσ PLOCATION='Houston' (PROJECT))) D_NO_HOU ← Π DNUMBER (DEPARTMENT) - Π DNUMBER (σσσσ DLOCATION='Houston' (DEPARTMENT)) E_D_NO_HOU ← Π SSN (EMPLOYEE PNO=DNUMBER (D_NO_HOU)) RESULT_EMPS ← E_P_HOU - E_D_NO_HOU RESULT ← Π LNAME,FNAME,ADDRESS (EMPLOYEE * RESULT_EMPS) Result: LNAME FNAME ADDRESS Wallace Jennifer 291 Berry, Bellaire, TX (j) DEPT_MANAGERS(SSN) ← Π MGRSSN (DEPARTMENT) EMPS_WITH_DEPENDENTS(SSN) ← Π ESSN (DEPENDENT)4 RESULT_EMPS ← DEPT_MANAGERS - EMPS_WITH_DEPENDENTS RESULT ← Π LNAME,FNAME (EMPLOYEE * RESULT_EMPS) Result: LNAME FNAME Borg James 2. (a) R1 ←STUDENT S.SSN=E.SSN ENROLL R2 ← Π COURSE# σσσσ NAME=’JOHN SMITH’AND QUARTER=’W99’(R1)) Answer: R2 (b) R1← COURSE C.COURSE#=B.COURSE# BOOK_ADOPTION R2(COURSE#, BOOK_NO) ← COURSE # ℑ COUNT BOOK_ISBN (R1) R3←σσσσCNAME=’CS’AND BOOK_NO>2 (COURSE C.COURSE#=R2.COURSE# R2) R4← Π COURSE#, BOOK_ISBN (R3 R3.COURSE#=B.COURSE# BOOK_ADOPTION) R5← Π COURSE#, BOOK_ISBN, BOOK_TITLE (R4 R4.BOOK_ISBN=T.BOOK_ISBN TEXT) Answer: R5 (c) R1← Π COURSE#, PUBLISHER (BOOK_ADOPTION B.BOOK_ISBN=T.BOOK_ISBN TEXT) R2←σσσσ COUNT_PUB=’1’ (COURSE# ℑ COUNT PUBLISHER (R1)) R3←σσσσ PUBLISHER=’AWL Publishing’ (R2 R2.COURSE#=R1.COURSE# R1) R4← Π CNAME (COURSE C.COURSE#=R3.COURSE# R3) Answer: R45 3. Answer: (a) P Q R A B C 10 a 5 10 b 6 10 a 5 10 b 5 25 a 6 25 c 3 (b) P Q R A B C 15 b 8 10 b 6 15 b 8 10 b 5 (c) P Q R A B C 10 a 5 10 b 6 10 a 5 10 b 5 15 b 8 null null null 25 a 6 25 c 3 (d) P Q R A B C 15 b 8 10 b 6 null null null 25 c 3 15 b 8 10 b 5 (e) P Q R 10a 5 15 b 8 25 a 6 10b 6 25 c 3 10b 5 (f) P Q R A B C 10 a 5 10 b 5 4. Specify queries (a), (b), (c), (e), (f), (i), and (j) of Question 1 in both the tuple relational calculus and the domain relational calculus. Answer: (a) Retrieve the names of employees in department 5 who work more than 10 hours per week on the 'ProductX' project. Tuple relational Calculus: { e.LNAME, e.FNAME | EMPLOYEE(e) AND e.DNO=5 AND (∃ p) (∃ w) (WORKS_ON(w) AND PROJECT(p) AND e.SSN=w.ESSN AND w.PNO=p.PNUMBER AND p.PNAME='ProductX' AND w.HOURS>10 ) } Domain relational Calculus:6 { qs | EMPLOYEE(qrstuvwxyz) AND z=5 AND (∃ a) (∃ b) (∃ e) (∃ f) (∃ g) ( WORKS_ON(efg) AND PROJECT(abcd) AND t=e AND f=b AND a='ProductX' AND g>10 ) } (b) List the names of employees who have a dependent with the same first name as themselves. Tuple relational Calculus: { e.LNAME, e.FNAME | EMPLOYEE(e) AND (∃ d) ( DEPENDENT(d) AND e.SSN=d.ESSN AND e.FNAME=d.DEPENDENT_NAME ) } Domain relational Calculus: { qs | (∃ t) (∃ a) (∃ b) ( EMPLOYEE(qrstuvwxyz) AND DEPENDENT(abcde) AND a=t AND b=q ) } (c) Find the names of employees that are directly supervised by 'Franklin Wong'. Tuple relational Calculus: { e.LNAME, e.FNAME | EMPLOYEE(e) AND (∃ s) ( EMPLOYEE(s) AND s.FNAME='Franklin' AND s.LNAME='Wong' AND e.SUPERSSN=s.SSN ) } Domain relational Calculus: { qs | (∃ y) (∃ a) (∃ c) (∃ d) ( EMPLOYEE(qrstuvwxyz) AND EMPLOYEE(abcdefghij) AND a='Franklin' AND c='Wong' AND y=d ) } (e) Retrieve the names of employees who work on every project. Tuple relational Calculus: { e.LNAME,
View Full Document