SQL lab 1 Create tables, insert data, and query

advertisement
SQL Lab 2 – Assignment 4
Overview:
Total 100 points.
There are three tasks in this assignment: A) create tables, B) insert data, and C) query single table. For
each task, you need to write SQL command and run it in Oracle SQLPlus Worksheet. What you need
to submit are:
1. the SQL command;
2. the results copy and paste from Oracle SQLPlus Worksheet result window.
A. Create Tables
5 points for each table. The logical schema is shown as below:
Employee
FName
Minit
LName
SSN
BDate
Address
Sex
Salary
SuperSSN
DepNo
Department
DName DepNo MgrSSN
MgrSDate
Dept_Locations
DepNo
DLocation
Project
PName
PNumber
PLocation
DepNo
Works_On
ESSN
PNo
Hours
Dependent
ESSN
Dependent_Name
Sex
BDate
Relationship
Hint: you have two ways to define foreign key, one is to define foreign key within the CREATE
TABLE statement as in SQL Lab 1, for example:
CREATE TABLE products
(product_id numeric(10) not null,
supplier_id numeric(10),
1
CONSTRAINT fk_supplier FOREIGN KEY (supplier_id) REFERENCES
supplier(supplier_id) ON DELETE SET NULL);
Another way is to create table without defining foreign key and add foreign key later using the ALTER
TABLE statement (sometimes you might have to do like this), for example:
ALTER TABLE Products
ADD CONSTRAINT fk_supplier FOREIGN KEY (supplier_id) REFERENCES
supplier(supplier_id) ON DELETE SET NULL;
Table Name: employee
Attribute
First Name
Mid Name
Last Name
SSN Number
Birthday
Address
Sex
Data Type
VARCHAR(15)
CHAR
VARCHAR(15)
CHAR(9)
DATE
VARCHAR(50)
CHAR
Salary
DECIMAL(10,2)
Supervisor SSN
CHAR(9)
Department Number
INT
Primary
Foreign
Constraint
NOT NULL
NOT NULL
NOT NULL

Sex IN ('M',
'F', 'm', 'f')
DEFAULT
'800'
employee (SSN) ON
DELETE SET NULL
Table Name: department
Attribute
Department Name
Department Number
Manager SSN
Data Type
VARCHAR(15)
INT
CHAR(9)
Manage Start Date
DATE
Primary
UNIQUE

Foreign
Employee (SSN) ON
DELETE SET NULL
Constraint
NOT NULL
NOT NULL
NOT NULL
Table Name: dept_locations
Attribute
Data Type
Primary
Department Number
INT

Department Location
VARCHAR(15)

Foreign
Constraint
Department (DepNo) ON
NOT NULL
DELETE CASCADE
NOT NULL
2
Table Name: project
Attribute
Project Name
Project Number
Project Location
Department Number
Data Type
VARCHAR(15)
INT
VARCHAR(15)
INT
Primary
UNIQUE

Attribute
Employee SSN
Data Type
CHAR(9)
Primary
Project Number
INT
Hours
DECIMAL(3, 1)
Foreign
Constraint
NOT NULL
NOT NULL
Department (DepNo) ON
DELETE SET NULL
Table Name: works_on


Foreign
Constraint
Employee (SSN) ON NOT NULL
DELETE CASCADE
Project (PNumber) ON NOT NULL
DELETE CASCADE
NOT NULL
Table Name: dependent
Attribute
Employee SSN
Data Type
CHAR(9)
Dependent Name
Sex
VARCHAR(15)
CHAR
Birthday
Relationship
DATE
VARCHAR(8)
Primary

Foreign
Employee (SSN) ON
DELETE CASCADE

Constraint
NOT NULL
NOT NULL
Sex IN ('M',
'F', 'm', 'f')
B. Insert Data
5 points for each table.
Table Name: Employee
Mini
LName
SSN
t
Doug
E Gilbert 554433221
Joyce
PAN
543216789
Frankin
T Wong
333445555
Jennifer
S Wallace 987654321
John
B Smith
123456789
Ramesh
K Narayan 666884444
Joyce
A English 453453453
James
E Borg
888665555
Alicia
J Zelaya 999887777
Ahmad
V Jabbar 987987987
FName
BDate
Address
09-JUN-60
07-FEB-78
08-DEC-45
20-JUN-31
09-JAN-55
15-SEP-52
31-JUL-62
10-NOV-27
19-JUL-58
29-MAR-59
11 S 59 E, Salt Lake City, UT
35 S 18 E, Salt Lake City, UT
638 Voss, Houston, TX
291 Berry, Bellaire, TX
731 Fondren, Houston, TX
975 Fire Oak, Humble, TX
5631 Rice, Houston, TX
450 Stone, Houston, TX
3321 Castle, Spring, TX
980 Dallas, Houston, TX
Sex Salary SuperSSN DepNo
M
F
M
F
M
M
F
M
F
M
80000
70000
40000
43000
30000
38000
25000
55000
25000
25000
NULL
NULL
554433221
554433221
333445555
333445555
333445555
543216789
987654321
987654321
3
2
5
4
5
5
5
1
4
4
3
Table Name: Department
DName
Manufacture
Administration
Headquarter
Finance
Research
DepNo
1
2
3
4
5
MgrSSN
888665555
543216789
554433221
987654321
333445555
MgrDate
19-JUN-71
04-JAL-99
22-SEP-55
01-JAN-85
22-MAY-78
Table Name: Dept_Locations
DepNo
1
1
2
2
3
4
4
5
5
DLocation
Houston
Chicago
New York
San Francisco
Salt Lake City
Stafford
Bellaire
Sugarland
Houston
Table Name: Project
PName PNumber
Plocation
ProjectA
3388
Houston
ProjectB
1945
Salt Lake City
ProjectC
6688
Houston
ProjectD
24
Bellaire
ProjectE
77
Sugarland
ProjectF
1
Salt Lake City
ProjectG
12
New York
ProjectH
34
Stafford
ProjectI
43
Chicago
ProjectJ
22
San Francisco
DepNo
1
3
5
4
5
3
2
4
1
2
Table Name: Works_On
ESSN
123456789
123456789
666884444
453453453
453453453
PNo
3388
1945
3388
77
22
Hours
32.5
7.5
40.0
20.0
20.0
4
333445555
333445555
333445555
333445555
999887777
999887777
543216789
554433221
77
6688
43
22
1
12
22
1945
10.0
10.0
35.0
28.5
11.5
13.0
17.0
21.5
Table Name: Dependent
ESSN
333445555
333445555
333445555
987654321
123456789
123456789
123456789
Dependent_Name
Alice
Theodore
Joy
Abner
Michael
Alice
Elizabeth
Sex
F
M
F
M
M
F
F
BDate
05-APR-76
25-OCT-73
03-MAY-48
29-FEB-32
01-JAN-78
31-DEC-78
05-MAY-57
Relationship
Daughter
Son
Spouse
Spouse
Son
Daughter
Spouse
C. Query Single Table
4 points each.
1. List the names of all employees who work in department 5.
2. List names and salaries of all employee ordered by salary.
3. List the name of employees whose salary is between 30000 and 50000.
4. List the name and address of employees who lives in Houston.
5. List the name of employees who doesn't has supervisor.
6. List department number and number of employees in each department, ordered by number of
employees in each department.
7. List department number and number of employees in departments that have more than 2
employees, ordered by department number.
8. List the ESSN of employees who works on project 3388 or project 1945.
9. List the location of department 1, 3, and 5.
10. List the name of all female employees.
5
Download