8.24 Solution
Short Description
Download 8.24 Solution...
Description
Chapter 7 SQL - A Relational Database Language
7 . 1 9 Write statements to create indexes on the database schema shown in Figure 2.1 on the following attributes: Answers: (a) A unique, clustering index on the StudentNumber attribute of STUDENT. CREATE UNIQUE INDEX STUDENT_NUM_INDEX ON STUDENT ( StudentNumber ) CLUSTER ;
(b) A clustering index on the StudentNumber attribute of TRANSCRIPT. CREATE INDEX STUD_NUM_INDEX ON TRANSCRIPT ( StudentNumber ) CLUSTER ;
(c) An index on the Major attribute of STUDENT. CREATE INDEX MAJOR_INDEX ON STUDENT ( Major ) ;
7 . 2 0 What are the types of queries that would become more efficient for each of the indexes specified in Exercise 7.19? Answer: For the STUDENT_NUM_INDEX on STUDENT, selecting a STUDENT record based on the value of StudentNumber would be very efficient. In addition, retrieving all STUDENT records in order of StudentNumber would be efficient, as would performing a JOIN operation with another file where StudentNumber is the join attribute. For the STUD_NUM_INDEX clustering index on TRANSCRIPT, selecting all the TRANSCRIPT records that have a given value of StudentNumber (all TRANSCRIPT records for a particular student) would be very efficient. In addition, performing a JOIN operation with another file where StudentNumber is the join attribute would be very efficient. For the MAJOR_INDEX on STUDENT, selecting all the STUDENT records that have a given value of Major (all STUDENTs majoring in a particular department) would be relatively efficient.
7 . 2 1 Specify the following views in SQL on the COMPANY database schema shown in Figure 6.5. Answers: (a) A view that has the department name, manager name, and manager salary for every department. CREATE VIEW DEPT_INFO (DEPT_NAME, MGR_LAST_NAME, MGR_FIRST_NAME, MGR_SALARY) AS SELECT DNAME, LNAME, FNAME, SALARY FROM DEPARTMENT, EMPLOYEE WHERE MGRSSN=SSN ;
- 96 -
Chapter 7 SQL - A Relational Database Language
Note: This query will only retrieve a DEPARTMENT that currently has a manager. If it is possible that a DEPARTMENT may not have a manager (say, for a brief period of time), than an outer join would be necessary to retrieve a DEPARTMENT that is not currently related to a manager. In this case, we could use LEFT OUTER JOIN (see Section 7.2.8) as follows: CREATE VIEW DEPT_INFO (DEPT_NAME, MGR_LAST_NAME, MGR_FIRST_NAME, MGR_SALARY) AS SELECT DNAME, LNAME, FNAME, SALARY FROM (DEPARTMENT LEFT OUTER JOIN EMPLOYEE ON MGRSSN=SSN) ;
(b) A view that has the employee name, supervisor name, employee salary for each employee who works in the 'Research' department. CREATE VIEW RESEARCH_EMP_INFO (LNAME, FNAME, SUPERVISOR_NAME, SALARY) AS SELECT E.LNAME, E.FNAME, S.LNAME, E.SALARY FROM EMPLOYEE E, EMPLOYEE S, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=E.DNO AND E.SUPERSSN=S.SSN ;
Note: Again here, if we want every EMPLOYEE, regardless of whether that EMPLOYEE currently has a SUPERVISOR or not, we would use the LEFT OUTER JOIN as follows: CREATE VIEW RESEARCH_EMP_INFO (LNAME, FNAME, SUPERVISOR_NAME, SALARY) AS SELECT E.LNAME, E.FNAME, S.LNAME, E.SALARY FROM (EMPLOYEE E LEFT OUTER JOIN EMPLOYEE S ON E.SUPERSSN=S.SSN), DEPARTMENT WHERE DNAME='Research' AND DNUMBER=E.DNO ;
(c) A view that has project name, controlling department name, number of employees, and total hours worked per week on the project for each project. CREATE VIEW PROJ_INFO (PROJ_NAME, DNAME, NUMBER_OF_EMPS, TOTAL_HRS_PER_WEEK) AS SELECT PNAME, DNAME, COUNT(*), SUM(HOURS) FROM PROJECT, DEPARTMENT, WORKS_ON WHERE PNUMBER=PNO AND DNUM=DNUMBER GROUP BY PNAME, DNAME ;
Note: If we want every PROJECT, regardless of whether that PROJECT currently has a controlling depertment or not, we would use the LEFT OUTER JOIN as follows: CREATE VIEW PROJ_INFO (PROJ_NAME, DNAME, NUMBER_OF_EMPS, TOTAL_HRS_PER_WEEK) AS SELECT PNAME, DNAME, COUNT(*), SUM(HOURS) FROM (PROJECT LEFT OUTER JOIN DEPARTMENT ON DNUM=DNUMBER), WORKS_ON WHERE PNUMBER=PNO GROUP BY PNAME, DNAME ;
(d) A view that has project name, controlling department name, number of employees, and total hours worked per week on the project for each project with more than one employee working on it. CREATE VIEW PROJ_INFO (PROJ_NAME, DNAME, NUMBER_OF_EMPS, TOTAL_HRS_PER_WEEK) AS SELECT PNAME, DNAME, COUNT(*), SUM(HOURS) FROM PROJECT, DEPARTMENT, WORKS_ON WHERE PNUMBER=PNO AND DNUM=DNUMBER GROUP BY PNAME, DNAME HAVING COUNT(*) > 1 ; - 97 -
View more...
Comments