Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter Three – Structured Query Language Database Concepts Seventh Edition David M. Kroenke • David J. Auer Instructor’s Manual Prepared by David J. Auer & Robert J. Mills CHAPTER THREE STRUCTURED QUERY LANGUAGE Page 1 Chapter Three – Structured Query Language 3.1 If you haven’t done so, create the WPC database, tables, and relationships described in this chapter, using the SQL DBMS of your choice. Be sure to populate the tables with the data shown in Figure 3-2. See Figure 3-1 for the SQL statements to create the database tables. See Figure 3-2 for the SQL statements to populate the database. Note that the data used in these exercises in the original data before the SQL commands in Chapter 3 were run. If you have already run those commands, you will get slightly different results. /***** DEPARTMENT DATA SELECT * FROM DEPARTMENT; /***** EMPLOYEE DATA SELECT * FROM EMPLOYEE; /***** PROJECT DATA SELECT * FROM PROJECT; ***********************************/ *************************************/ **************************************/ Page 2 Chapter Three – Structured Query Language /***** ASSIGMENT DATA *************************************/ SELECT * FROM ASSIGNMENT; 3.2 Using the SQL DBMS of your choice, create and run queries to answer the questions in exercise AW.3.1. Using [Access] SQL, create and run queries to answer the questions that follow. Save each query using the query name format SQLQuery-AWE-3-1-## where the ## sign is replaced by the letter designator of the question. For example, the first query will be saved as SQLQueryAWE-3-1-A. For SQL Server: A. What projects are in the PROJECT table? Show all information for each project. /***** Question A – SQL-Query-AWE-3-1-A.sql SELECT * FROM PROJECT; Page 3 ***************/ Chapter Three – Structured Query Language B. What are the ProjectID, ProjectName, StartDate, and EndDate values of projects in the PROJECT table? /***** SELECT FROM C. What projects in the PROJECT table started before August 1, 2014? Show all the information for each project. /***** SELECT FROM WHERE D. Question C – SQL-Query-AWE-3-1-C.sql * PROJECT StartDate <'01-AUG-14'; ***************/ What projects in the PROJECT table have not been completed? Show all the information for each project. /***** SELECT FROM WHERE E. Question B – SQL-Query-AWE-3-1-B.sql ***************/ ProjectID, ProjectName, StartDate, EndDate PROJECT; Question D – SQL-Query-AWE-3-1-D.sql * PROJECT EndDate IS NULL; ***************/ Who are the employees assigned to each project? Show ProjectID, EmployeeNumber, LastName, FirstName, and Phone. /***** SELECT Phone FROM Question E – SQL-Query-AWE-3-1-E.sql **************/ ProjectID, E.EmployeeNumber, LastName, FirstName, ASSIGNMENT AS A JOIN EMPLOYEE AS E ON A.EmployeeNumber=E.EmployeeNumber; Page 4 Chapter Three – Structured Query Language F. Who are the employees assigned to each project? Show ProjectID, ProjectName, and Department. Show EmployeeNumber, LastName, FirstName, and Phone. /***** SELECT FROM Question F – SQL-Query-AWE-3-1-F.sql *************/ P.ProjectID, ProjectName, P.Department, E.EmployeeNumber, LastName, FirstName, Phone ASSIGNMENT AS A JOIN EMPLOYEE AS E ON A.EmployeeNumber=E. EmployeeNumber JOIN PROJECT AS P ON A.ProjectID=P.ProjectID; Page 5 Chapter Three – Structured Query Language G. Who are the employees assigned to each project? Show ProjectID, ProjectName, Department, and Department Phone. Show EmployeeNumber, LastName, FirstName, and Employee Phone. Sort by ProjectID in ascending order. /***** SELECT Question G – SQL-Query-AWE-3-1-G.sql *************/ P.ProjectID, ProjectName, D.DepartmentName, D.Phone AS DepartmentPhone, E.EmployeeNumber, LastName, FirstName, E.Phone AS EmployeePhone FROM ASSIGNMENT AS A JOIN EMPLOYEE AS E ON A.EmployeeNumber=E. EmployeeNumber JOIN PROJECT AS P ON A.ProjectID=P.ProjectID JOIN DEPARTMENT AS D ON P.Department=D.DepartmentName ORDER BY P.ProjectID; H. Who are the employees assigned to projects run by the Marketing Department? Show ProjectID, ProjectName, Department, and Department Phone. Show EmployeeNumber, LastName, FirstName, and Employee Phone. Sort by ProjectID in ascending order. /***** Question H – SQL-Query-AWE-3-1-H.sql SELECT ***************/ P.ProjectID, ProjectName, D.DepartmentName, D.Phone AS DepartmentPhone, E.EmployeeNumber, LastName, FirstName, E.Phone AS EmployeePhone FROM ASSIGNMENT AS A JOIN EMPLOYEE AS E ON A.EmployeeNumber=E. EmployeeNumber JOIN PROJECT AS P ON A.ProjectID=P.ProjectID JOIN DEPARTMENT AS D ON P.Department=D.DepartmentName WHERE DepartmentName='Marketing' ORDER BY P.ProjectID; Page 6 Chapter Three – Structured Query Language I. How many projects are being run by the Marketing Department? Be sure to assign an appropriate column name to the computed results. /***** SELECT FROM WHERE J. What is the total MaxHours of projects being run by the Marketing Department? Be sure to assign an appropriate column name to the computed results. /***** SELECT FROM WHERE K. Question J – SQL-Query-AWE-3-1-J.sql **************/ SUM(MaxHours) AS TotalMaxHoursForMarketingDeptProjects PROJECT Department='Marketing'; What is the average MaxHours of projects being run by the Marketing Department? Be sure to assign an appropriate column name to the computed results. /***** SELECT FROM WHERE L. Question I – SQL-Query-AWE-3-1-I.sql **************/ COUNT(*) AS NumberOfMarketingDeptProjects PROJECT Department='Marketing'; Question K – SQL-Query-AWE-3-1-K.sql **************/ AVG(MaxHours) AS AverageMaxHoursForMarketingDeptProjects PROJECT Department='Marketing'; How many projects are being run by each department? Be sure to display each DepartmentName and to assign an appropriate column name to the computed results. /***** SELECT FROM GROUP BY Question L – SQL-Query-AWE-3-1-L.sql **************/ Department, COUNT(*) AS NumberOfDeptProjects PROJECT Department; Page 7 Chapter Three – Structured Query Language 3.3 Using the SQL DBMS of your choice, complete steps A through E in exercise AW.3.3, but exclude step F. WPC has decided to keep track of computers used by the employees. In order to do this, two new tables will be added to the database. The schema for these tables, as related to the existing EMPLOYEE table is: EMPLOYEE (EmployeeNumber, FirstName, LastName, Department, Phone, Email) COMPUTER (SerialNumber, Make, Model, ProcessorType, ProcessorSpeed, MainMemory, DiskSize) COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, DateAssigned, DateReassigned) The referential integrity constraints are: Serial Number in COMPUTER_ASSIGNMENT must exist in SerialNumber in COMPUTER EmployeeNumber in COMPUTER_ASSIGNMENT must exist in EmployeeNumber in EMPLOYEE EmployeeNumber is a surrogate key and never changes. Employee records are never deleted from the database. SerialNumber is not a surrogate key because it is not generated by the database. However, a computer’s SerialNumber never changes, and, therefore, there is no need to cascade updates. When a computer is at its end of life, the record in COMPUTER for that computer and all associated records in COMPUTER_ASSIGNMENT are deleted from the database. For SQL Server 2014 the questions and their solutions are as follows: A. Figure 3-23 shows the column characteristics for the WPC COMPUTER table. Using the column characteristics, use Access SQL to create the COMPUTER table and its associated constraints in the WPC.accdb database. Are there any table characteristics that cannot be created in SQL? If so, what are they? Use the Access GUI to finish setting table characteristics, if necessary. Page 8 Chapter Three – Structured Query Language The required work here is to create the COMPUTER table in the DBMS of choice using SQL. Although Access SQL has limitations, there will be no SQL limitations in SQL Server 2014 Express or MySQL. CREATE TABLE COMPUTER( SerialNumber Int NOT NULL, Make Char(12) NOT NULL, Model Char(24) NOT NULL, ProcessorType Char(24) NULL, ProcessorSpeed Numeric(3,2) NOT NULL, MainMemory Char(15) NOT NULL, DiskSize Char(15) NOT NULL, CONSTRAINT COMPUTER_PK PRIMARY KEY(SerialNumber), CONSTRAINT MAKE_CHECK CHECK (Make IN ('Dell', 'Gateway', 'HP', 'Other')), CONSTRAINT SPEED_CHECK CHECK (ProcessorSpeed BETWEEN 1.0 AND 4.0) ); B. The data for the COMPUTER table are in Figure 3-23. Use Access SQL to enter these data into your COMPUTER table. Page 9 Chapter Three – Structured Query Language INSERT INTO COMPUTER VALUES( 9871234, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 9871245, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 9871256, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 9871267, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 9871278, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 9871289, 'HP', 'Pavilion 500-210qe', 'Intel i5-4530', 3.00, '6.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541001, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541002, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541003, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541004, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541005, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); INSERT INTO COMPUTER VALUES( 6541006, 'Dell', 'OptiPlex 9020', 'Intel i7-4770', 3.40, '8.0 GBytes', '1.0 TBytes'); SELECT FROM ORDER BY * COMPUTER Make DESC, SerialNumber ASC; Page 10 Chapter Three – Structured Query Language C. Figure 3-24 shows the column characteristics for WPC COMPUTER_ASSIGNMENT table. Using the column characteristics, use Access SQL to create the COMPUTER_ASSIGNMENT table and the associated constraints in the WPC.accdb database. Are there any table or relationship settings or characteristics that cannot be created in SQL? If so, what are they? Use the Access GUI to finish setting table characteristics and relationship settings if necessary. CREATE TABLE COMPUTER_ASSIGNMENT( SerialNumber Int NOT NULL, EmployeeNumber Int NOT NULL, DateAssigned Date NOT NULL, DateReassigned Date NULL, CONSTRAINT COMP_ASSIGN_PK PRIMARY KEY(SerialNumber, DateAssigned), CONSTRAINT COMP_ASSIGN_EMPLOYEE FOREIGN KEY(EmployeeNumber) REFERENCES EMPLOYEE(EmployeeNumber) ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT COMP_ASSIGN_COMPUTER FOREIGN KEY(SerialNumber) REFERENCES COMPUTER(SerialNumber) ON UPDATE NO ACTION ON DELETE CASCADE ); Page 11 Chapter Three – Structured Query Language D. The data for the COMPUTER_ASSIGNMENT table are in Figure 3-26. Use Access SQL to enter these data into your COMPUTER_ASSIGNMENT table. INSERT INTO COMPUTER_ASSIGNMENT VALUES( 9871234, 11, '15-Sep-14', '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT VALUES( 9871245, 12, '15-Sep-14', '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871256, 4, '15-Sep-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871267, 5, '15-Sep-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871278, 8, '15-Sep-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871289, 9, '15-Sep-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541001, 11, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541002, 12, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541003, 1, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541004, 2, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541005, 3, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(6541006, 6, '21-Oct-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871234, 7, '05-Nov-14'); INSERT INTO COMPUTER_ASSIGNMENT (SerialNumber, EmployeeNumber, VALUES(9871245, 10, '05-Nov-14'); Page 12 DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) DateAssigned) Chapter Three – Structured Query Language SELECT FROM ORDER BY E. * COMPUTER_ASSIGNMENT DateAssigned ASC, SerialNumber ASC; Who is currently using which computer at WPC? Create an appropriate SQL query to answer this question. Show SerialNumber, Make, and Model. Show EmployeeID, LastName, FirstName, Department, and Employee Phone. Sort first by Department and then by employee LastName. Save this query using the query naming rules in exercise AW.3.1. /***** SELECT FROM WHERE ORDER BY Question E – SQL-Query-AWE-3-3-E.sql ************************/ C.SerialNumber, Make, Model, ProcessorType, ProcessorSpeed, E.EmployeeNumber, LastName, FirstName, Department, Phone COMPUTER AS C JOIN COMPUTER_ASSIGNMENT AS CA ON C.SerialNumber=CA.SerialNumber JOIN EMPLOYEE AS E ON CA.EmployeeNumber=E. EmployeeNumber DateReassigned IS NULL Department, LastName, DateAssigned; Page 13