You are on page 1of 9

CST363 Introduction to Database Systems

Final exam, Summer 2016

Name: Huy Nguyen

Question 1 (20 points in total)

STUDENT (primary key: stu_num)


stu_num stu_lname
321452 Bowser
324257 Smith

ENROLL(primary key: class_code+stu_num; foreign key: class_code references CLASS.class_code,


stu_num references STUDENT.stu_num)
class_code stu_num enroll_grade
10014 321452 C
10014 324257 B
10018 321452 A
10018 324257 B
10012 321452 C
10021 324257 C

CLASS (Primary key: class_code)


class_code crs_code class_time class_room prof_num
10014 ACCT-211 TTH2:30-3:45 BUS252 342
PM.
10018 CIS-220 MWF 9:00- KLR211 114
9:50 AM
10021 QM-261 MWF 8:00- KLR200 114
8:50 AM

1
1.1 Given the table structures, answer the following questions (5 points):

Entity Integrity? Referential Integrity?


Why or Why not? Why or why not?
N/A - does not contain FK.

Yes because stu_num can uniquely


STUDENT
identify each row and has no NULL
values.

Yes, class_code+stu_num serve as a No, class_code is a foreign key


unique identifier. referencing Class.class_code.
But Enroll.class_code
ENROLL
contains a row with 10012 which is
not in Class.class_code.

N/A - does not contain FK.


Yes the primary key class_code serves
CLASS as a unique identified and is not NULL.

1.2 Use relational operators: union, difference, intersect, product, select, project to find out the
following information (15 points):

Find name of students, class code, and grade


- Student PRODUCT Enroll -> tableA
- SELECT from tableA where Student.stu_num = Enroll.stu_num -> tableB
- PROJECT stu_lname, class_code, enroll_grade from tableB -> tableC

Find course code, class time, and classroom.


- PROJECT crs_code, class_time, class_room from Class ->tableA

Find name of student, course code, class time, and grade.


- Student PRODUCT Enroll -> tableA
- SELECT from tableA where Student.stu_num = Enroll.stu_num -> tableB
- tableB PRODUCT Enroll -> tableC
- SELECT from tableC where tableB.class_code = Enroll.class_code -> tableD
- PROJECT stu_lname, crs_code, class_time, enroll_grade from tableD -> tableE

Question 2 (50 points in total)


Items Starbucks Order
2
Item# Itemname UnitPric Starbucks# Cit M# Order# Item# Starbucks# UnitPric Qt
e y e y
100 Tea 1.85 1 SJ 001 0001 100 1 1.85 2
200 Latte 2.85 2 SJ 006 0002 100 2 1.85 1
300 Mocha 3.15 3 SD 004 0003 200 3 2.85 2
4 SD 005 0004 200 3 2.85 1
5 SF 003 0005 300 4 3.15 1
0006 300 5 3.15 2

Employee
ID# Name Starbucks
#
001 Jason 1
002 Will 1
003 Lisa 5
004 Mary 3
005 Mark 4
006 Holly 2
007 Grace 2
008 Taylor 3

Business rules and assumption:

There are more than one Starbucks in a city.


Every Starbucks has a manager who is also an employee
One manager can manage one Starbucks.
Each time a customer orders only one item

2.1 Locate the following information for each table (10 points)

two super keys


Items:
- Item# + Itemname
- Item# + UnitPrice
Starbucks:
- Starbucks# + City
- M# + Starbucks#
Order:
- Order# + Starbucks#
- Order#+Item#
Employee:
- ID# + Name
- ID# + Starbucks#

all candidate keys


Items:
- Item#
- Itemname
- Unitprice
Starbucks:
3
- Starbucks#
- M#
Order:
- Order#
Employee:
- ID#
- Name
all foreign keys, and the attributes they reference in another table.
Items:
(None)
Starbucks:
- M# references Employee.ID#
Order:
- Item# references Items.Item#
- Starbucks# references Starbucks.Starbucks#
Employee:
- Starbucks# references Starbucks.Starbucks#

2.2 Write SQL statement (40 points in total)

SQL> /* Display todays date in the following format*/ (3 points)

Todays date: 04/19/2015

> select 'Today's date: ', to_char(sysdate, 'MM/DD/YYYY') from dual;

SQL> /* Display the order#, item#, item name, unit price, order quantity, sales amount of
all orders */ Note: use alias for output, use the following output as reference. (5 points)

Order# ITEM# ITEM NAME UNIT PRICE ORDER QUANTITY ORDER AMOUNT
0001 100 Tea 1.85 2 3.7

> select Order#, Item# as ITEM#, Itemname as ITEM NAME, Unitprice as


UNIT PRICE, Qty as ORDER QUANTITY, Unitprice*Qty as ORDER
AMOUNT from Items, Order where Items.Item# = Order.Item#;

SQL> /* Provide a summary of the sales (Display the total order quantity and amount of
each item, including the following information: Item#, Item Name, total order amount,
total sales amount of each item */ (5 points)

4
> select Item#, Itemname, OrderCount, TotalAmount from Items, Order
(select Order.Item#, sum(orders.Qty) as OrderCount,
sum(Order.Qty*Order.UnitPrice) as TotalAmount from Order)
where Item.Item# = Order.Item#;

Item# Item Name OrderCount TotalAmount


100 Tea 3 5.55
200 Latte 6 17.1
300 Mocha 9 28.35

SQL> /*Display starbucks store number, city, and the name of its manager */ (5 points)
> select Starbucks.Starbucks#, City, Name, from Starbucks, Employee where
Starbucks.M# = Employee.ID#

SQL> /*Display the name of the employee, the store number and city of the starbucks
he/she works for, and the managers name. (5 points)
> select Name, Employee.Starbucks#, City, Name, from Employee, Starbucks
(select Starbucks#, Starbucks.City, Employee.Name from Starbucks,
Employee where Starbucks.M# = Employee.ID#) where Employee.Starbucks# =
Starbucks.Starbucks#;

SQL> /*Display the item with the highest order quantity. */ (5 points)
> select Order.Item#, Itemname, from Items, Order where Qty = (select max(Oty)
from Order where Order.Item# = Item.Item#)

SQL>/* Add a new item to the Items table. (400, chaiMocha, 4.15) (2 points)
>Insert into Items values (400, chaiMocha, 4.15);

SQL> /* Write SQL commands to accept a user input ID# and display all information at
Employee table related to this EmployeeID, like the following */ (10 points)

>set echo off


>set verify off
>accept emp_ID prompt Please enter employee ID: ;
> select ID#, Name, Starbucks# from Employee where ID# = &emp_ID;

Please enter employee ID: 001

ID# Name Starbucks#


001 Jason 1
5
Question 3 (10 points in total)

Please draw ER diagram to define entities and relationships in terms of connectivity and cardinality. NO
need to include attributes of each entity.

3.1 Each employee must have one manager, each manager supervises at least one employee (3 points)

3.2 A certificate program has 5 instructors, each can teach 40 students per class maximum. There are 8
courses, each class is taught by one instructor. Each instructor can teach up to 2 classes, or no class at all.
Each student can take more than one class (7 points). Assumption: all eight classes are different.

Question 4 (20 points in total)


4.1 The following functional dependencies are identified. The primary key of this table is A+B. (10
points)

A+B C, D, E, F, G, H, I
A C, D
B -> E, F
G H, I

A B C D E F G H I

(1)Normalize the above table to 2NF. Mark the primary key of each table.

6
(2)Normalize the tables to 3NF. Mark the primary key of each table.

7
4.2

The boutique bed and breakfast has many guests. Each guest has a number (num) and name (name).
Number for each guest is unique, name of the guest is not unique since two people can have the same
name. Every time a guest stays at the bed and breakfast hotel, the stay is assigned by a unique stay ID
(stayID). When a guest frequents the hotel, he/she will have multiple stayID. Each stay has a date for
check in and check out. In each stay, the guest will stay more than one night. The price of the room
changes depending on the checkin date.

Num Name StayID CheckIn CheckOut Numofdays Price

1. Mark one primary key of this table.

- PK = StayID

8
2. Identify all the dependencies in this table. NO NEED to normalize. Feel free to use the
format: OtterID -> Name, status

- StayID -> Num, Name, CheckIn, CheckOut, Numbofdays, Price


- Checkin -> Price
- Num -> Name
- CheckIn + CheckOut -> Numofdays

You might also like