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.
- 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.
- 97 -