8
SQL – Assignment 1 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: 1

SQL Assignment 1

Embed Size (px)

Citation preview

Page 1: SQL Assignment 1

SQL – Assignment 1

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:

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

Page 2: SQL Assignment 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 ProductsADD CONSTRAINT fk_supplier FOREIGN KEY (supplier_id) REFERENCES supplier(supplier_id) ON DELETE SET NULL;

Table Name: employee

Attribute Data Type Primary Foreign ConstraintFirst Name VARCHAR(15) NOT NULLMid Name CHARLast Name VARCHAR(15) NOT NULLSSN Number CHAR(9) NOT NULLBirthday DATEAddress VARCHAR(50)Sex CHAR Sex IN ('M',

'F', 'm', 'f')Salary DECIMAL(10,2) DEFAULT

'800'Supervisor SSN CHAR(9) employee (SSN) ON

DELETE SET NULLDepartment Number INT

Table Name: department

Attribute Data Type Primary Foreign ConstraintDepartment Name VARCHAR(15) UNIQUE NOT NULLDepartment Number INT NOT NULLManager SSN CHAR(9) Employee (SSN) ON

DELETE SET NULLNOT NULL

Manage Start Date DATE

Table Name: dept_locations

Attribute Data Type Primary Foreign Constraint

Department Number INT Department (DepNo) ON DELETE CASCADE

NOT NULL

Department Location VARCHAR(15) NOT NULL

2

Page 3: SQL Assignment 1

Table Name: project

Attribute Data Type Primary Foreign ConstraintProject Name VARCHAR(15) UNIQUE NOT NULLProject Number INT NOT NULLProject Location VARCHAR(15)Department Number INT Department (DepNo) ON

DELETE SET NULL

Table Name: works_on

Attribute Data Type Primary Foreign ConstraintEmployee SSN CHAR(9) Employee (SSN) ON

DELETE CASCADENOT NULL

Project Number INT Project (PNumber) ON DELETE CASCADE

NOT NULL

Hours DECIMAL(3, 1) NOT NULL

Table Name: dependent

Attribute Data Type Primary Foreign ConstraintEmployee SSN CHAR(9) Employee (SSN) ON

DELETE CASCADENOT NULL

Dependent Name VARCHAR(15) NOT NULLSex CHAR Sex IN ('M',

'F', 'm', 'f')Birthday DATERelationship VARCHAR(8)

B. Insert Data 5 points for each table.

Table Name: Employee

FNameMini

tLName SSN BDate Address Sex Salary SuperSSN DepNo

Doug E Gilbert 554433221 09-JUN-60 11 S 59 E, Salt Lake City, UT M 80000 NULL 3Joyce PAN 543216789 07-FEB-78 35 S 18 E, Salt Lake City, UT F 70000 NULL 2Frankin T Wong 333445555 08-DEC-45 638 Voss, Houston, TX M 40000 554433221 5Jennifer S Wallace 987654321 20-JUN-31 291 Berry, Bellaire, TX F 43000 554433221 4John B Smith 123456789 09-JAN-55 731 Fondren, Houston, TX M 30000 333445555 5Ramesh K Narayan 666884444 15-SEP-52 975 Fire Oak, Humble, TX M 38000 333445555 5Joyce A English 453453453 31-JUL-62 5631 Rice, Houston, TX F 25000 333445555 5James E Borg 888665555 10-NOV-27 450 Stone, Houston, TX M 55000 543216789 1Alicia J Zelaya 999887777 19-JUL-58 3321 Castle, Spring, TX F 25000 987654321 4Ahmad V Jabbar 987987987 29-MAR-59 980 Dallas, Houston, TX M 25000 987654321 4

3

Page 4: SQL Assignment 1

Table Name: Department

DName DepNo MgrSSN MgrDateManufacture 1 888665555 19-JUN-71Administration 2 543216789 04-JAL-99Headquarter 3 554433221 22-SEP-55Finance 4 987654321 01-JAN-85Research 5 333445555 22-MAY-78

Table Name: Dept_Locations

DepNo DLocation1 Houston1 Chicago2 New York2 San Francisco3 Salt Lake City4 Stafford4 Bellaire5 Sugarland5 Houston

Table Name: Project

PName PNumber Plocation DepNoProjectA 3388 Houston 1ProjectB 1945 Salt Lake City 3ProjectC 6688 Houston 5ProjectD 24 Bellaire 4ProjectE 77 Sugarland 5ProjectF 1 Salt Lake City 3ProjectG 12 New York 2ProjectH 34 Stafford 4ProjectI 43 Chicago 1ProjectJ 22 San Francisco 2

Table Name: Works_On

ESSN PNo Hours123456789 3388 32.5123456789 1945 7.5666884444 3388 40.0453453453 77 20.0453453453 22 20.0333445555 77 10.0

4

Page 5: SQL Assignment 1

333445555 6688 10.0333445555 43 35.0333445555 22 28.5999887777 1 11.5999887777 12 13.0543216789 22 17.0554433221 1945 21.5

Table Name: Dependent

ESSN Dependent_Name Sex BDate Relationship333445555 Alice F 05-APR-76 Daughter333445555 Theodore M 25-OCT-73 Son333445555 Joy F 03-MAY-48 Spouse987654321 Abner M 29-FEB-32 Spouse123456789 Michael M 01-JAN-78 Son123456789 Alice F 31-DEC-78 Daughter123456789 Elizabeth F 05-MAY-57 Spouse

C. Query Single Table4 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