0% found this document useful (0 votes)
44 views9 pages

COMP 3278 Assignment 2 Incompleted

The document contains the SQL statements to create tables for an online photo printing application and insert sample data. It also contains SQL queries to retrieve data from the tables.

Uploaded by

jackysecruity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views9 pages

COMP 3278 Assignment 2 Incompleted

The document contains the SQL statements to create tables for an online photo printing application and insert sample data. It also contains SQL queries to retrieve data from the tables.

Uploaded by

jackysecruity
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

COMP 3278 Assignment 2

Full name: Law Tsz Kit

U.I.D.: 3035188033

1.

CREATE TABLE AppUser


(
user_id INT NOT NULL,
name VARCHAR(80) NOT NULL,
PRIMARY KEY(user_id)
);

CREATE TABLE Customer


(
user_id INT NOT NULL,
birthday DATE NOT NULL,
PRIMARY KEY(user_id),
FOREIGN KEY(user_id) REFERENCES AppUser(user_id)
);

CREATE TABLE CustomerPhone


(
user_id INT NOT NULL,
phone VARCHAR(20) NOT NULL,
PRIMARY KEY(user_id, phone),
FOREIGN KEY(user_id) REFERENCES Customer(user_id)
);
CREATE TABLE Admin
(
user_id INT NOT NULL,
supervisor_id INT,
PRIMARY KEY(user_id),
FOREIGN KEY(user_id) REFERENCES AppUser(user_id),
FOREIGN KEY(supervisor_id) REFERENCES Admin(user_id)
);

CREATE TABLE Album


(
album_id INT NOT NULL,
name VARCHAR(80) NOT NULL,
creation_date DATE NOT NULL,
user_id INT NOT NULL,
PRIMARY KEY(album_id),
FOREIGN KEY(user_id) REFERENCES AppUser(user_id)
);

CREATE TABLE Image


(
album_id INT NOT NULL,
image_id INT NOT NULL,
filename VARCHAR(80) NOT NULL,
PRIMARY KEY(album_id,image_id),
FOREIGN KEY(album_id) REFERENCES Album(album_id)
);
CREATE TABLE Product
(
product_id INT NOT NULL,
description TEXT NOT NULL,
price DECIMAL(10,2) NOT NULL,
PRIMARY KEY(product_id)
);

CREATE TABLE PrintOrder


(
order_id INT NOT NULL,
user_id INT NOT NULL,
status VARCHAR(80) NOT NULL,
pic_id INT,
PRIMARY KEY(order_id),
FOREIGN KEY(user_id) REFERENCES Customer(user_id),
FOREIGN KEY(pic_id) REFERENCES Admin(user_id)
);

CREATE TABLE Item


(
order_id INT NOT NULL,
item_id INT NOT NULL,
quantity INT NOT NULL,
product_id INT NOT NULL,
album_id INT NOT NULL,
image_id INT NOT NULL,
PRIMARY KEY(order_id,item_id),
FOREIGN KEY(order_id) REFERENCES PrintOrder(order_id),
FOREIGN KEY(product_id) REFERENCES Product(product_id),
FOREIGN KEY(album_id, image_id) REFERENCES Image(album_id, image_id)
);
2.
INSERT INTO AppUser VALUES(10060,"David Brook")
INSERT INTO AppUser VALUES(9054,"Miranda Spears")

INSERT INTO Customer VALUES(10060,"1989-11-20")

INSERT INTO CustomerPhone VALUES(10060,"960-0123")


INSERT INTO CustomerPhone VALUES(10060,"550-1234")

INSERT INTO Admin (user_id) VALUES (9054)

INSERT INTO Album VALUES(7,"Summer 2019","2019-10-22",10060)


INSERT INTO Image VALUES(7,6,"4x IMG_123.jpg")

INSERT INTO Product VALUES(3,"4R glossy print",1.1)

INSERT INTO PrintOrder VALUES(123,10060,"Printing in progress.",9054)

INSERT INTO Item VALUES(123,6,1,3,7,6)


3.
a)
SELECT product_id,description FROM Product ORDER BY price

b)
SELECT Customer.user_id,AppUser.name FROM Customer,AppUser
WHERE Customer.user_id=AppUser.user_id
c)
SELECT AppUser.name,CustomerPhone.phone,PrintOrder.order_id
FROM AppUser,CustomerPhone,PrintOrder
WHERE PrintOrder.status LIKE "%New order%" AND
CustomerPhone.user_id=AppUser.user_id AND
AppUser.user_id=PrintOrder.user_id

d)
SELECT
Item.item_id,Item.quantity,Product.description,(Product.price*Item.quan
tity) AS 'subtotal'
FROM Item,Product
WHERE Item.product_id=Product.product_id AND Item.order_id=3
e)

f)
SELECT AppUser.name
FROM AppUser
RIGHT OUTER JOIN Customer
on AppUser.user_id = Customer.user_id
RIGHT OUTER JOIN Admin
on AppUser.user_id = Admin.user_id

Therefore, the names of all users that is a customer and an admin user is Gebhard Nasato,
Sunitha Aslan and Adsila Fields.
g)
SELECT Admin.user_id
FROM Admin,AppUser
WHERE Admin.user_id=AppUser.user_id AND Admin.supervisor_id IS NULL

You might also like