Sunteți pe pagina 1din 9

CST363 Introduction to Database Systems

Final exam, Summer 2016

Name: Austin Hasemeyer

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
10021 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 because there is no foreign key.
Yes, because each primary key has its
own unique identity and contains no
STUDENT
nulls.

Yes, because every class_code has a


Yes, because each compound key is corresponding value in the Class table.
ENROLL
unique and contains no nulls.

N/A because there is no foreign key.


Yes, because instance of the primary
key has its own unique identity and
CLASS
contains no nulls.

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


1. Student product Grades tableA;
2. Select from tableA where Student.stu_num = Enroll.stu_num tableB;
3. Project stu_lname, class_code, enroll_grade

Find course code, class time, and classroom.


1. Select from Class;
2. Project crs_code, class_time, class_room;

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


1. Student product Enroll tableA;
2. Select from tableA where Student.stu_num = Enroll.stu_num;
3. Project stu_lname, enroll_grade, stu_num, class_code tableB;
4. tableB product Class tableC;
5. Select from tableC where tableB.class_code = enroll.class_code;
6. Project stu_lname, crs_code, class_time, enroll_grade from tableC;

2
Question 2 (50 points in total)
Items Starbucks Order
Item# Itemname UnitPrice Starbucks# City M# Order# Item# Starbucks# UnitPrice Qty
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
o Item#
o (Item#, Itemname)
Starbucks
o Starbucks#
o (Starbucks#, M#)
Order
o (Order#, Starbucks#)
o (Order#, Starbucks#, Item#)
Employee
o ID#
o (ID#, Name)

all candidate keys

3
Items
o Item#
o (Itemname, UnitPrice)
Starbucks
o Starbucks#
o (City, M#)
Oder
o Order#
o (Item#, Starbucks#, UnitPrice, Qty)
Employee
o ID#
o (Name, Starbucks#)
all foreign keys, and the attributes they reference in another table.
Items
o No foreign key
Starbucks
o No foreign key
Order
o Order.Item# references Items.Item#
o Order.UnitPrice references Items.UnitPrice
o Order.Starbucks# references Starbucks.Starbucks#
Employee
o 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 to_char
(sysdate, 'mm-dd-yyyy') "Today's date: "
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)

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

SELECT Order#, ItemName, UnitPrice, Qty, Qty * UnitPrice 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)

Item# Item Name OrderCount TotalAmount


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

SELECT item#, Item Name


FROM Order, Items
WHERE ITEMS.Item# = Order.Item#
AND (SELECT (SELECT SUM(Qty) DISTINCT Order# FROM Order) *
(SELECT UnitPrice From Order) Total Amount);

SQL> /*Display starbucks store number, city, and the name of its manager */ (5 points)

SELECT 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, Starbucks#, City


From Starbucks, Employee
Where Starbucks.Stackbucks# = Employee.Starbucks#
AND SELECT Name Manager Name
FROM Employee
WHERE Starbucks.M# = Employee.ID#;

SQL> /*Display the item with the highest order quantity. */ (5 points)

5
SELECT TOP(5) Item#, Sum(Qty) As TotalQuantity
FROM Orders
GROUP BY Item#
Order by TotalQuantity DESC;

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

INSERT INTO ITEMS


VALUES(400, chiaMocha, 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)

ACCEPT ID# PROMPT Please enter employee ID:

SELECT ID#, Name, Starbucks#


FROM EMPLOYEE
WHERE ID# = &ID#;

Please enter employee ID: 001

ID# Name Starbucks#


001 Jason 1

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)

6
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.

BOLD UNDERLINE = primary key

A B G H I

A C D

B E F

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

A B G
G H I

A C D

B E F

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.


StayID

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

Checkin Price, Transitive dependence


StayID Num, CheckIn, CheckOut partial dependence
Num Name parial dependent

8
9

S-ar putea să vă placă și