Professional Documents
Culture Documents
Appendix A
----------------------------------------------------------------------------------------------------------------
information stored in tables for the Company database. This is one of two databases that are
used with this textbook, the second being the Riverbend Hospital database described in
Appendix B.
Database Schema
Figure A.1 shows a diagram of the six tables that comprise the Company database. The
table names are underlined in the figure, for example employee, and the column names are
Figure A.1
Revised 7-16-02 1
Appendix A
Relationships between the tables are shown with connecting lines. Consider the line
connecting the employee and dependent tables. Figure A.2 shows you how to interpret the
Figure A.2
Reading from the employee towards the dependent table, the line has a circle and three-
legged crows foot meaning this is an optional relationship (circle) such that each employee
can optionally (circle) have no dependents. The three-legged crows foot means an employee
can have one or more dependents. Reading from the dependent towards the employee table, the
line is crossed by two short lines indicating that this relationship is mandatory in this direction
such that each dependent can belong to one and only one employee a dependent cannot exist
without an employee.
Figure A.2 shows two other symbol combinations. The mandatory relationship with one
or more dependent table rows per employee row this would be the situation if the Company
only hired employees who already had a dependent, but this is not the case so this symbol was
Revised 7-16-02 2
Appendix A
not used in Figure A.1. The other is the optional zero or one employee per dependent and this is
also not the case so this symbol was not used in Figure A.1. However, you may find other
Table Definitions
Department Table
The Company is organized into separate departments. Tables A.1 and A.2 describe the
columns in the department table and show the test data stored for each department. Each
department is identified by a unique department number (dpt_no) and has a department name.
Each department has a manager and date on which the manager started managing the
department. The manager's social security number (dpt_mgrssn) stored in the department table
provides a FOREIGN KEY link to the specific employee that manages a department.
Table A.1
TABLE NAME: department
Column Name Datatype Size Comments
dpt_no NUMBER 2 Primary Key. Number assigned by the company.
dpt_name VARCHAR2 20 Department name.
dpt_mgrssn CHAR 9 Foreign Key link to employee that manages this department.
dpt_mgr_start_date DATE Date a manager was assigned to supervise the department.
Table A.2
TABLE DATA: department
dpt_no dpt_name dpt_mgrssn dpt_mgr_start_date
7 Production 999444444 05-22-1998
3 Admin and Records 999555555 01-01-2001
1 Headquarters 999666666 06-19-1981
SQL Example A.1 gives the CREATE TABLE command for the department table. Two
constraints are named, one for the department primary key and the other to ensure that each
dpt_no NUMBER(2)
CONSTRAINT pk_department PRIMARY KEY,
dpt_name VARCHAR2(20)
CONSTRAINT nn_dpt_name NOT NULL,
dpt_mgrssn CHAR(9),
dpt_mgr_start_date DATE
);
Dept_Locations Table
Each department may have multiple city locations within the Company. In order to
provide for normalized data maintenance, a separate dept_locations table stores location
information. Tables A.3 and A.4 describe the columns and actual data stored in the
dept_locations table. The primary key of the dept_locations table is a composite of dpt_no and
dpt_location. There are no non-key columns in the table. The dpt_no column also serves as a
Table A.3
TABLE NAME: dept_locations
Column Name Datatype Size Comments
dpt_no NUMBER 2 Primary Key. Number assigned by the company.
Also Foreign Key link to department table.
dpt_location VARCHAR2 20 Primary Key. Department name.
Table A.4
TABLE DATA: dept_locations
dpt_no dpt_location
1 Edwardsville
3 Marina
7 St. Louis
7 Collinsville
7 Edwardsville
SQL Example A.2 shows the code to create dept_locations. As you will learn from your
study of Chapter 2, the constraint to create the primary key is a special form of the PRIMARY
KEY clause that is used to create composite keys from two or more columns. There is also a
FOREIGN KEY clause to specify that dpt_no references the department column.
Revised 7-16-02 4
Appendix A
Employee Table
Employee information is stored in the employee table. Tables A.5 and A.6 describe the
columns in the employee table and the data stored in the table. Each employee is identified by
Table A.5
TABLE NAME: employee
Column Name Datatype Size Comments
emp_ssn CHAR 9 Primary Key. Employee social security number.
emp_last_name VARCHAR2 25 Employee last name.
emp_first_name VARCHAR2 25 Employee first name.
emp_middle_name VARCHAR2 25 Employee middle name.
emp_address VARCHAR2 50 Employee street address.
emp_city VARCHAR2 25 City where employee resides.
emp_state CHAR 2 Two-character abbreviation of the state of residence
emp_zip CHAR 9 Zip code for mailing.
emp_date_of_birth DATE Employee date of birth.
emp_salary NUMBER (7,2) Employee monthly salary.
emp_parking_space NUMBER 4 Number of parking space allocated to the employee.
emp_gender CHAR 1 Code M=male; F=female.
emp_dpt_number NUMBER 2 Foreign Key link to department table. Department to
which an employee is assigned.
emp_superssn CHAR 9 Foreign Key link to employee record supervising this
employee.
Table A.6
emp_ssn emp_last emp_first emp_ emp_address emp_city emp_
_name _name middle_ state
name
999666666 Bordoloi Bijoy South Main #12 Edwardsville IL
999555555 Joyner Suzanne A 202 Burns Farm Marina CA
999444444 Zhu Waiman Z 303 Lindbergh St. Louis MO
999887777 Markis Marcia M High St. #14 Monterey CA
999222222 Amin Hyder S. Seaside Apt B Marina CA
999111111 Bock Douglas B #2 Mont Verd Dr. St. Louis MO
999333333 Joshi Dinesh #10 Oak St Collinsville IL
999888888 Prescott Sherri C Overton Way #4 Edwardsville IL
Revised 7-16-02 5
Appendix A
Employees work in departments. Employees also manage departments. Thus, Figure A.1
shows these two relationships: Manage-Dept and Works-Here. The Manage-Dept relationship
links each row in the department table to the employee table row for the employee that manages
the department. The Works-Here relationship links the employee table to the department table
You should interpret the Works-Here relationship as meaning that a department can have
zero, one, or more than one assigned employees (crows foot), and that each employee is
assigned to one and only one (mandatory) department. SQL Example A.3 shows the CREATE
TABLE command for the employee table. The Works-Here relationship is implemented by the
The Manage-Dept relationship is interpreted as each department has one and only one
manager, and any given employee may optionally manage a department. Of course, most
employees will not manage a department. You will not find the FOREIGN KEY constraint to
enforce the Manage-Dept relationship in the CREATE TABLE command for either the employee
or department tables. This is because a constraint cannot be created for a non-existent table and
one of the two tables has to be created first! Instead, this constraint is added to the database after
between rows within a single table and is shown as the Supervise relationship in Figure A.1. The
Revised 7-16-02 6
Appendix A
that links a given employee to their supervisor; thus, the employee supervisors social security
number (emp_superssn) is stored as a column in the table. This relationship is covered in detail
in Chapter 6.
Projects Table
Projects for the Company are controlled or supervised by departments. Each project is
identified by a project number (pro_number) and the firm tracks each project by name, and
location. Tables A.7 and A.8 show the columns and data for the project table. Figure A.1 shows
the Controls-Proj relationship. A department may have zero, one or more than one active
Revised 7-16-02 7
Appendix A
Table A.7
TABLE NAME: project
Column Name Datatype Size Comments
pro_number NUMBER 2 Primary Key. Number assigned by the company.
pro_name VARCHAR2 25 Project name.
pro_location VARCHAR2 25 Project location.
pro_dept_number NUMBER 2 Foreign Key link to department controlling the project.
Table A.8
TABLE DATA: project
pro_number pro_name pro_location pro_dept_number
1 Order Entry St. Louis 7
2 Payroll Collinsville 7
3 Receivables Edwardsville 7
10 Inventory Marina 3
20 Personnel Edwardsville 1
30 Pay Benefits Marina 3
SQL Example A.3 gives the CREATE TABLE command for the project table. The
Assignment Table
Each employee is assigned to work on zero, one or more projects. This is reflected in
Figure A.1 by the assignment table and two relationships: Works-On and Has-Employee. The
assignment table is an association table because it associates the employee and project tables.
Tables A.9 and A.10 show the columns and data for the assignment table. The primary key for
assignment is a composite of the primary key from the employee table combined with the
primary key of the project table: the work_emp_ssn and work_pro_number columns.
Revised 7-16-02 8
Appendix A
Table A.9
TABLE NAME: assignment
Column Name Datatype Size Comments
work_emp_ssn CHAR 9 Primary Key. Employee social security number. Also Foreign
Key link to employee table.
work_pro_number NUMBER 2 Primary Key. Project number. Also Foreign Key link to the project
table.
work_hours NUMBER (5,1) Number of hours an employee has worked on a project.
work_hours_planned NUMBER (5,1) Number of planned (estimated) hours an employee will work on a
project in order to complete their tasking.
Table A.10
TABLE DATA: assignment
work_emp_ssn work_pro_number work_hours
999111111 1 31.4
999111111 2 8.5
999333333 3 42.1
999888888 1 21.0
999888888 2 22.0
999444444 2 12.2
999444444 3 10.5
999444444 1
999444444 10 10.1
999444444 20 11.8
999887777 30 30.8
999887777 10 10.2
999222222 10 34.5
999222222 30 5.1
999555555 30 19.2
999555555 20 14.8
999666666 20
SQL Example A.5 gives the CREATE TABLE command to create the assignment table.
The PRIMARY KEY clause implements the primary key in the same fashion as was done earlier
for the dept_locations table. Two FOREIGN KEY constraints, fk_work_emp and
back to the respective employee and project tables. You will learn about the ON DELETE
Revised 7-16-02 9
Appendix A
CONSTRAINT pk_assignment
PRIMARY KEY ( work_emp_ssn, work_pro_number ),
CONSTRAINT fk_work_emp
FOREIGN KEY (work_emp_ssn) REFERENCES employee
ON DELETE CASCADE,
CONSTRAINT fk_work_pro_number
FOREIGN KEY (work_pro_number) REFERENCES project
ON DELETE CASCADE
) ;
Dependent Table
Figure A.1 shows the Has-Dependent relationship between the employee and dependent
tables. Tables A.11 and A.12 describe the columns and data for the dependent table. An
employee may have zero, one or more dependents, and a dependent belongs to one and only one
employee. The firm only tracks minimal information about dependents in order to satisfy
governmental reporting requirements for taxation and education purposes. Each dependent table
row must reference an existing employee row in the employee table. The primary key of
dependent is a composite of dep_emp_ssn and dep_name. The dep_emp_ssn column also serves
Table A.11
TABLE NAME: dependent
Column Name Datatype Size Comments
dep_emp_ssn CHAR 9 Primary Key. Employee social security number for this dependent.
Also Foreign Key link to employee table.
dep_name VARCHAR2 50 Primary Key. Dependent name.
dep_gender CHAR 1 Dependent gender coded: M=male; F=female.
dep_date_of_birth DATE Dependent date of birth.
dep_relationship VARCHAR2 10 Relationship of dependent to employee, e.g., daughter, spouse, son.
Table A.12
TABLE DATA: dependent
dep_emp_ssn dep_name dep_gender dep_date_of_birth dep_relationship
999444444 Jo Ellen F 04-05-1996 DAUGHTER
999444444 Andrew M 10-25-1998 SON
999444444 Susan F 05-03-1975 SPOUSE
999555555 Allen M 02-29-1968 SPOUSE
999111111 Jeffery M 01-01-1978 SON
999111111 Deanna F 12-31-1978 DAUGHTER
999111111 Michelle F 05-05-1957 SPOUSE
Revised 7-16-02 10
Appendix A
SQL Example A.6 gives the CREATE TABLE command for the dependent table. The
PRIMARY KEY constraint named pk_dependent enforces the composite primary key. The
Equipment Table
Equipment is acquired from a local equipment rental company for projects. When
projects are completed, the equipment is returned; thus, the Company has no need to maintain a
stock of equipment. A project can have zero, one or more items of equipment as reflected by the
Located-At relationship in Figure A.1. Equipment items are identified by an equipment number
(eqp_no), have a description, value, and quantity on hand. The eqp_pro_number column in the
table is a FOREIGN KEY link to the project table for the project that is using the equipment.
Table A.13 and A.14 describe the columns and data for the equipment table.
work_pro_number NUMBER(2),
Table A.13
TABLE NAME: equipment
Column Name Datatype Size Comments
eqp_no CHAR 4 Primary Key. Equipment number.
eqp_desc VARCHAR2 15 Equipment description
eqp_value NUMBER (10,2) Value of one equipment item.
eqp_qty_on_hand NUMBER 3 Quantity of this item on hand for the project.
eqp_pro_number NUMBER 2 Foreign key link to project table
Table A.14
TABLE DATA: equipment
eqp_no eqp_desc eqp_value eqp_qty_on_hand eqp_pro_number
4321 Computer, PC 1100 2 3
Revised 7-16-02 11
Appendix A
Recall that earlier we discussed the Manage-Dept relationship between the department
and employee tables. The FOREIGN KEY constraint for this relationship can be added to the
department table definition now that the employee table has been created. You will study how to
The CreateCompanyDatabase.sql script code creates the Oracle tables and indexes for
the Company database. The script also populates the tables with data. The script assigns explicit
names to all table constraints and indexes. When you study Chapter 12, this will make it easier
for you to examine the data dictionary tables and views that store constraint and index
The script first drops each table to facilitate re-creating the tables and indexes if
necessary. This script can be accessed from the textbook website and is stored in a script file
Revised 7-16-02 12
Appendix A
the procedure you need to follow in order to create your own copy of the Company database.
REM CreateCompanyDatabase.sql
REM Script to create the Company database to
REM accompany Bordoloi/Bock -- An Introduction to SQL
Revised 7-16-02 14
Appendix A
COMMIT;
REM Update department rows to add manager ssn and start date.
UPDATE department SET dpt_mgrssn = '999444444',
dpt_mgr_start_date = TO_DATE('05-22-1998', 'MM-DD-YYYY')
WHERE dpt_no = '7';
UPDATE department SET dpt_mgrssn = '999555555',
dpt_mgr_start_date = TO_DATE('01-01-2001', 'MM-DD-YYYY')
WHERE dpt_no = '3';
UPDATE department SET dpt_mgrssn = '999666666',
dpt_mgr_start_date = TO_DATE('06-19-1981', 'MM-DD-YYYY')
WHERE dpt_no = '1';
COMMIT;
Revised 7-16-02 17