Uploaded by Zo. W

Database Lab Questions: SQL Table Creation & Manipulation

advertisement
Seminar 3 – Lab Questions
For this set of Lab Questions, we will create and use a database for the Review Wedgewood
Pacific Corporation (WPC. Founded in 1957 in Seattle, Washington, WPC has grown into an
internationally recognized organization. The company is located in two buildings. One building
houses the Administration, Accounting, Finance, and Human Resources departments, and the
second houses the Production, Marketing, and Information Systems departments. The company
database contains data about employees; departments; projects; assets, such as computer
equipment; and other aspects of company operations.
The database will be named WPC and will contain the following four tables:
DEPARTMENT (DepartmentName, BudgetCode, OfficeNumber, Phone)
EMPLOYEE (EmployeeNumber, FirstName, LastName, Department, Phone, Email)
PROJECT (ProjectID, Name, Department, MaxHours, StartDate, EndDate)
ASSIGNMENT (ProjectID, EmployeeNumber, HoursWorked)
EmployeeNumber is a surrogate key that starts at 1 and increments by 1. ProjectID is a
surrogate key that starts at 1000 and increases by 100. DepartmentName is the text name of
the department, and is therefore not a surrogate key.
The WPC database has the following referential integrity constraints:
Department in EMPLOYEE must exist in DepartmentName in DEPARTMENT
Department in PROJECT must exist in DepartmentName in DEPARTMENT
ProjectID in ASSIGNMENT must exist in ProjectID in PROJECT
EmployeeNumber in ASSIGNMENT must exist in EmployeeNumber in EMPLOYEE
The relationship from EMPLOYEE to ASSIGNMENT is 1:N, M-O and the relationship from
PROJECT to ASSIGNMENT is 1:N, M-O. The database also has the following business rules:
o
If an EMPLOYEE row is to be deleted and that row is connected to any
ASSIGNMENT, the EMPLOYEE row deletion will be disallowed.
o
If a PROJECT row is deleted, then all the ASSIGNMENT rows that are
connected to the deleted PROJECT row will also be deleted.
The business sense of these rules is as follows:
o
If an EMPLOYEE row is deleted (for example, if the employee is transferred),
then someone must take over that employee’s assignments. Thus, the
application needs someone to reassign assignments before deleting the
employee row.
o
If a PROJECT row is deleted, then the project has been canceled, and it is
unnecessary to maintain records of assignments to that project.
The column characteristics for these tables are shown in Figures 1-1 (DEPARTMENT), 1-2,
(EMPLOYEE), 1-3 (PROJECT), and 1.4 (ASSIGNMENT). The data for these tables are shown
in Figures 1-5 (DEPARTMENT), 1-6 (EMPLOYEE), 1-7 (PROJECT), and 1-8 (ASSIGNMENT).
Page 1-1
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
Fig 1-1
Start with 1,
increment by 1
Fig 1-2
Start with 1000-,
increment by 100
PROJECT
Surrogate Key
Fig 1-3
Page 1-2
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
ASSIGNMENT
Fig 1-4
DEPARTMENT
Fig 1-5
Page 1-3
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
EMPLOYEE
Fig1-6
PROJECT
Fig 1-7
Page 1-4
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
ASSIGNMENT
Fig 1-8
1.1
Write a CREATE TABLE statement for the DEPARTMENT table.
1.2
Write a CREATE TABLE statement for the EMPLOYEE table. Email is required and is
an alternate key, and the default value of Department is Human Resources. Cascade
updates but not deletions from DEPARTMENT to EMPLOYEE.
1.3
Write a CREATE TABLE statement for PROJECT table. The default value for MaxHours
is 100. Cascade updates but not deletions from DEPARTMENT to EMPLOYEE.
1.4
Write a CREATE TABLE statement for the ASSIGNMENT table. Cascade only deletions
from PROJECT to ASSIGNMENT; do not cascade either deletions or updates from
EMPLOYEE to ASSIGNMENT.
1.5
Modify the Project table to include the constraint that StartDate be prior to EndDate.
Page 1-5
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
1.6
Write an alternate SQL statement to make the relationship between EMPLOYEE and
ASSIGNMENT a 1:1 relationship.
1.7
Write an ALTER statement to add the column AreaCode to EMPLOYEE. Assume that
AreaCode is not required.
1.8
Write an ALTER statement to remove the column AreaCode from EMPLOYEE.
1.9
Write an ALTER statement to make Phone an alternate key in EMPLOYEE.
1.10
Write an ALTER statement to drop the constraint that Phone is an alternate key in
EMPLOYEE.
1.11
Write INSERT statements to add the data shown in Figure 1-5 to the DEPARTMENT
table. Run these statements to populate the DEPARTMENT table.
1.12
Write INSERT statements to add the data shown in Figure 1-6 to the EMPLOYEE table.
Run these statements to populate the EMPLOYEE table.
1.13
Write INSERT statements to add the data shown in Figure 1-7 to the PROJECT table.
Run these statements to populate the PROJECT table.
1.14
Write INSERT statements to add the data shown in Figure 1-8 to the ASSIGNMENT
table. Run these statements to populate the ASSIGNMENT table.
1.15
Why were the tables populated in the order Department, Employee, Project and finally
Assignment?
1.16
Assume that you have a table named NEW_EMPLOYEE that has the columns
Department, Email, FirstName, and LastName, in that order. Write an INSERT
statement to add all of the rows from the table NEW_EMPLOYEE to EMPLOYEE. Do
not attempt to run this statement since the table NEW_EMPLOYEE is non-existent!
1.17
Write an UPDATE statement to change the phone number of employee with
EmployeeNumber 11 to 360-287-8810.
1.18
Write an UPDATE statement to change the department of employee with
EmployeeNumber 5 to Finance.
1.19
Write an UPDATE statement to change the phone number of employee with
EmployeeNumber 5 to 360-287-8420.
1.20
Combine your answers to Review Questions 1.18 and 1.19 into one SQL statement.
UPDATE EMPLOYEE
1.21
Write an UPDATE statement to set HoursWorked to 60 for every row in ASSIGNMENT
having the value 10 for EmployeeNumber. Run this statement.
1.22
Assume that you have a table named NEW_EMAIL which has new values of Email for
some employees. NEW_EMAIL has two columns: EmployeeNumber and NewEmail.
Page 1-6
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Seminar 3 – Lab Questions
Write an UPDATE statement to change the values of Email in EMPLOYEE to those in
the NEW_EMAIL table.
1.23
Write one DELETE statement that will delete all data for project ‘2009 Q3 Product Plan’
and all of its rows in ASSIGNMENT.
1.24
Write a DELETE statement that will delete the row for the employee named “Smith”.
What happens if this employee has rows in ASSIGNMENT?
1.25
Write an SQL statement to join EMPLOYEE, ASSIGNMENT, and PROJECT using the
JOIN ON syntax.
1.26
Write an SQL statement to join EMPLOYEE and ASSIGNMENT and include all rows of
EMPLOYEE in your answer, regardless of whether they have an ASSIGNMENT.
Page 1-7
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall
Download