Sie sind auf Seite 1von 4

Chapter 8

SQL Coding Exercises and Questions 1. The payroll department needs to regularly access information about employee salary information. The DBA of the company has directed you to create a view based on the employee table named vwSalary. This view should include the employee identifying number, employee last and first names (LastName and FirstName), and the salary for each employee . Name the columns of the view as follows: EmpID, EmpLastName, EmpFirstName, and EmpSalary. Write the SQL code needed to create this view. Write a SELECT statement to display rows from the view for employees with salaries at or above $30,000. Format all output appropriately. 2. Replace the view named vwSalary created in question 1 with a new view (same name) that also includes the DepartmentNumber column. Name this column Department in the new view. Write a SELECT statement to display rows from the view where employees are in department 8 and their salary is at or above $10,000. Format all output appropriately. 3. Clerical employees in the human resources department only need read-access to information about employee dependents. Create a read only view named vwDependent that has the same structure as the dependent table. Name the read-only constraint vwDependentReadOnly. Use the DESCribe command to describe the vwDependent object and list the description. 4. Create a materialized view named vwProjectHours that will be used by the senior project manager to access information about work hours that have been reported for different projects. The view should join the project and projectAssignment tables. The view should show each project's title as well as the average hours worked on each project. Name the columns Project and AverageHours in the view. The rows in the view should be grouped by the project name. The data stored in the view should refresh every seven days with the date of view creation as the start date. Write a SELECT statement to display projects where the average hours is at or greater than 15. 5. The Company's senior project manager needs to access information about departments that manage projects for a specific set of projects, namely those located in either Maryville or Alton. Create a view named vwDepartmentProjects that includes the DepartmentNumber and DepartmentName columns from the department table and the ProjectTitle and Location columns from the project table. The view should only reference rows for projects that are located in either Maryville or Alton. The columns in the view should be named DeptNo, Department, Project, and Location, respectively. Write a SELECT statement to display all of the rows that are accessible through the view. Format the output columns of the SELECT statement as A25 for Project and Department, and A15 for Location. 6. The senior project manager has requested that the vwDepartmentProjects view also include the project number. Replace the vwDepartmentProjects view described in question 5 with a new view that includes the ProjectNumber column from the project table. Remove the department number (DeptNo) column from the view.. Name this column ProNo in the view. All other requirements remain unchanged. Write a SELECT statement to display the rows that are accessible through the view for the Location value of Alton. Use appropriate formatting for columns. 7. Demonstrate the use of the view named vwDepartmentProjects for the senior project manager by writing a SELECT command to query the department_projects view created in question 6 to display all row information for project 4.
Revised 10-25-2007 8- 1

Chapter 8 8. The senior project manager no longer needs the view named vwDepartmentProjects or the materialized view named vwProjectHours. Write statements to drop these views. 9. You wish to provide location transparency security for the table named service and have decided to do this by creating a public synonym named svc. Write the statement to create this public synonym. Write a SELECT statement to display the ServiceID and Description columns for the service with a ServiceID of 10061 by using the svc synonym. Write the statement to drop the public synonym. Note: You can only complete this exercise if your instructor has granted you the CREATE PUBLIC SYNONYM privilege. 10. You've decided to create a synonym to be used personally to assist you in referencing the table named projectAssignment. The synonym will be named simply pa. Write the command to create this synonym. Write a command to rename the pa synonym to have a new name of assign. Write a SELECT statement to display all column values by using the assign synonym where the HoursWorked column value is greater than or equal to 30 hours. The result table does not require formatting. 11. The senior vice president for resource management desires to number new departments within the company sequentially beginning with number 40. The department numbering will be in increments of 1. In order to improve system performance, you have recommended that 5 department numbers be cached and this has been approved. No other options are to be set for the new sequence. Name the sequence DepartmentSequence. Write the code needed to create this sequence. 12. Write an INSERT command to insert two new rows into the department table by using the DepartmentSequence created in question 12. The other information to be inserted is shown below. DepartmentName = 'Medical Surgical Ward 2 DepartmentName = Gerontology ManagerID = NULL for both new rows ManagerAssignedDate = NULL for both new rows Write a SELECT statement to display all rows from the department table sorted by DepartmentNumber for department numbers greater than or equal to 8. 13. Write a command to insert a new project into the project table. This project will be controlled by the Gerontology department. Use the DepartmentSequence created and used in questions 12 and 13 to insert the new row into the project table. The other information to be inserted is shown below. Write a SELECT statement to display the row for project 55. Compare it to the department numbers listed in question 12. Ensure that the correct pro_dept_number value is stored to the new project row. ProjectNumber = 55 ProjectTitle = 'New Inventory Sys' Location = 'Alton' DepartmentNumber = (generate this from the sequence) 14. The pharmacy regularly accesses information about medicines that are stocked. The hospital DBA has directed you to create a view based on the medicine table named vwMedicine. This view should include the medicine code (MedicineCode) common medicine name
Revised 10-25-2007 8- 2

Chapter 8 (CommonName), scientific medicine name (ScientificName), quantity in stock (QuantityOnHand), and unit of measure (UnitOfMeasure) for each medicine. Use the same names for the view columns as are used for the medicine table. Write a SELECT statement to display rows from the view for medicines with a quantity on hand level that is at or below 1,000. 15. Replace the view named vwMedicine created in question 14 with a new view (same name) that also includes the Dosage column. All other view requirements remain the same. Write a SELECT statement to display rows from the view where medicine code is between 9999010 and 9999013. List the medicine code, common name, and dosage for the medicines. 16. Employees in the patient services department only need limited access to information about patients. Create a read-only view named vwPatientReadOnly that has the same structure as the patient table, but includes only the PatientID, LastName, FirstName, and BirthDate columns. The view column names should be PatientID, PatLastName, PatFirstName, and PatBirthDate. Write a SELECT statement to display information from vwPatientReadOnly where the patient identification value is greater than or equal to '600000'. 17. Create a view named vwPatientRecord that can be used by hospital physicians and nurses to access information about patients and the notes recorded for patients. The view should join the patient and patientNote tables. The view should include the PatientID, LastName and FirstName (concatenated as a single column), BedNo (bed number) columns from the patient table and the NoteID and PhysicianComment columns from the patientNote table. Name the columns in the view appropriate names. Write a SELECT statement to display the patient record where the PatientID is 100031. 18. The hospital pharmacy directory needs a view created named vwPrescription that has the following information from the prescription, patient, medicine and employee tables: prescription number, prescription date, dosage prescribed, dosage directions, medicine common name, patient name (last and first as a single column limited to 20 characters), and employee identification (EmployeeID) and name (LastName) of the employee that wrote the prescription. Name the columns in the view appropriate names. Write a SELECT statement to display prescription information for prescription 755444020. 19. The directory of patient treatment planning needs a view created named vwTreatment that has the following information from the treatment, service, patient, and employee tables: treatment number, service identifier, service description, treatment date, patient name (last and first as a single column), employee identifier, employee last name that provided the treatment, actual charge for the treatment, and comments on the treatment. Name the columns in the view appropriate names. Write a SELECT statement to display treatment information for patient receiving treatment 23limit the result table to the treatment number, patient name, employee last name, and comments on the treatment . 20. The chief of physicians requests a view named vwDoctor that lists employees with the title 'M.D.' or a title that begins with 'M.D.'. The view should list each employee's identifier, name (last and first as a single column), department number, department name, work telephone number (WorkPhone), and phone extension. Name the columns in the view appropriate names. The work telephone number should be stored in the view with the format (999) 9999999. Write a SELECT statement to display the doctor's name, department name, work telephone number, and phone extension information for doctors assigned to the Radiology department.

Revised 10-25-2007

8- 3

Chapter 8 21. The hospitals management no longer needs the views named vwTreatment or vwDoctor. Write commands to drop these views. 22. Create a synonym for your personal use for the specialty table name the synonym sp. Select the SpecialtyID and Title columns by using the synonym. Limit the result table to SpecialtyID values that begin with the letter 'S'. 23. Rename the synonym you created for question 22 to medSpec and write a SELECT statement to display all information about the SpecialtyID of 'ONC' by using the synonym name in your query. 24. Create a view of the patient table that restricts access to the following columns: PatientID, SSN, LastName, and FirstName. Name the view vwPatientLimited. Use the same column names for view columns as the column names in the patient table. 25. The hospital chief of administration wants to assign patient identifiers for new patients arriving at the hospital numbers sequentially beginning with '700000'. The patient numbering will be in increments of 1. Cache 10 sequence numbers. Write the statement necessary to create the sequence with the name PatientSequence. 26. Write INSERT statements to insert two new rows into the patient table; however, your update to the table must be through the view named vwPatientLimited that you created for question 24, and the INSERT statements must use the PatientSequence to generate values for the PatientID column. Note that the sequence will create values of data type NUMBER; however, the PatientID column is of data type CHAR(6). The patient SSN, last, and first name values are shown below. All other columns of the patient table for these two new rows will be NULL. Write a SELECT statement to display all column values from the patient table for patients with a PatientID that is greater than or equal to '7000000.'
Patient SSN (SSN) 900-00-0000 900-00-0001 Last Name (LastName) Zucker Zucker First Name (FirstName) Zina Zachary

Revised 10-25-2007

8- 4

Das könnte Ihnen auch gefallen