Comp 1630 - Relational Database & SQL Term Project: Instructions
Comp 1630 - Relational Database & SQL Term Project: Instructions
Term Project
INSTRUCTIONS
Follow the instructions given in part A to first create and then build the data base by running the
SQL script provided. Using Microsoft SQL Server, create the SQL scripts for the tasks in each
part of the project (B, C, and D) that are necessary to generate the required result sets, clearly
identifying your answers.
In separate PDF files, using your Student Id, submit each part of the project to the appropriate
folder in share IN, for example A00123456_Project B.pdf.
MARKING GUIDE
Although a result set may match the one provided, marks will be deducted for the following:
Page 1
Comp 1630 - Relational Database & SQL
Term Project
ENTITY RELATIONSHIP DIAGRAM
A2. Run the script ProjectData.sql to create the tables listed below, and to populate the tables
with data.
Customers 91 rows
Employees 9 rows
Shippers 3 rows
Suppliers 15 rows
Products 77 rows
Orders 1078 rows
OrderDetails 2820 rows
Page 2
Comp 1630 - Relational Database & SQL
Term Project
PART B - SQL Statements
B1. List the order details where the quantity is between 65 and 70. Display the order id and
quantity from the OrderDetails table, the product id and reorder level from the Products
table, and the supplier id from the Suppliers table. Order the result set by the order id. The
query should produce the result set listed below.
B2. List the product id, product name, English name, and unit price from the Products table
where the unit price is less than $8.00. Order the result set by the product id. The query
should produce the result set listed below.
(6 row(s) affected)
B3. List the customer id, company name, country, and phone from the Customers table where
the country is equal to Canada or USA. Order the result set by the customer id. The query
should produce the result set listed below.
Page 3
Comp 1630 - Relational Database & SQL
Term Project
B4. List the products where the reorder level is equal to the units in stock. Display the
supplier id and supplier name from the Suppliers table, the product name, reorder level,
and units in stock from the Products table. Order the result set by the supplier id. The query
should produce the result set listed below.
(4 row(s) affected)
st
B5. List the orders where the shipped date is greater than or equal to 1 of Jan 1994, and
st
calculate the length in years from the shipped date to 1 of Jan 2009. Display the
order id, and the shipped date from the Orders table, the company name, and the contact
name from the Customers table, and the calculated length in years for each order. Display
the shipped date in the format MMM DD YYYY. Order the result set by order id and the
calculated years. The query should produce the result set listed below.
st th
B6. List all the orders where the order date is between 1 of Jan and 30 of Mar 1992, and
the cost of the order is greater than or equal to $1500.00. Display the order id, order
date, and a new shipped date calculated by adding 10 days to the shipped date from the
Orders table, the product name from the Products table, the company name from the
Customer table, and the cost of the order. Format the date order date and the shipped date
as MON DD YYYY. Use the formula (OrderDetails.Quantity * Products.UnitPrice) to
calculate the cost of the order. Order the result set by order id. The query should produce
the result set listed below.
Page 4
Comp 1630 - Relational Database & SQL
Term Project
B7. List all the orders with a shipping city of Vancouver. Display the order from the Orders
table, and the unit price and quantity from the OrderDetails table. Order the result set by
the order id. The query should produce the result set listed below.
(8 row(s) affected)
B8. List all the orders that have not been shipped (shipped date is null). Display the customer
id, company name and fax number from the Customers table, and the order id and order
date from the Orders table. Order the result set by the customer id and order date. The
query should produce the result set listed below.
B9. List the products which contain choc or tofu in their name. Display the product id, product
name, quantity per unit and unit price from the Products table. Order the result set by
product id. The query should produce the result set listed below.
(4 row(s) affected)
Page 5
Comp 1630 - Relational Database & SQL
Term Project
B10. List the number of products and their names beginning with each letter of the alphabet.
Only display the letter and count if there are at least three product names begin with the
letter. The query should produce the result set listed below.
ProductName Total
----------------- -----------
C 9
G 11
I 3
L 5
M 5
N 3
P 3
R 6
S 9
T 6
Page 6
Comp 1630 - Relational Database & SQL
Term Project
C1. Create a view called vw_supplier_items listing the distinct suppliers and the items they
have shipped. Display the supplier id and name from the Suppliers table, and the product id
and product name from the Products table. Use the following query to test your view to
produce the result set listed below.
SELECT *
FROM vw_supplier_items
ORDER BY Name, ProductID
C2. Create a view called vw_employee_info to list all the employees in the Employee table.
Display the employee id, last name, first name, and birth date. Format the name as first
name followed by a space followed by the last name. Use the following query to test your
view to produce the result set listed below.
SELECT *
FROM vw_employee_info
WHERE EmployeeID IN ( 3, 6, 9 )
(3 row(s) affected
C3. Using the UPDATE statement, change the fax value to Unknown for all rows in the
Customers table where the current fax value is null (22 rows affected).
Page 7
Comp 1630 - Relational Database & SQL
Term Project
C4. Create a view called vw_order_cost to list the cost of orders. Display the order id and
order_date from the Orders table, the product id from the Products table, the company
name from the Customers table, and the order cost. To calculate the cost of the orders, use
the formula: (OrderDetails.Quantity * OrderDetails.UnitPrice).
Use the following query to test your view to produce the result set listed below.
SELECT *
FROM vw_order_cost
WHERE orderID BETWEEN 10100 AND 10200
ORDER BY ProductID
C5. Using the INSERT statement, add a row to the Suppliers table with a supplier id of 16 and
a name of ‘Supplier P’.
C6. Using the UPDATE statement, increase the unit price in the Products table by 15% for rows
with a current unit price less than $5.00 (2 rows affected).
C7. Create a view called vw_orders to list orders. Display the order id and shipped date from
the Orders table, and the customer id, company name, city, and country from the
Customers table. Use the following query to test your view to produce the result set listed
below.
SELECT *
FROM vw_orders
WHERE ShippedDate BETWEEN '1993-01-01' AND '1993-01-31'
ORDER BY CompanyName, Country
Page 8
Comp 1630 - Relational Database & SQL
Term Project
D1. Create a stored procedure called sp_emp_info to display the employee id, last name, first
name, and phone number from the Employees table for a particular employee. The
employee id will be an input parameter for the stored procedure. Use the following query to
test your stored procedure to produce the result set listed below.
EXEC sp_emp_info 7
(1 row(s) affected)
D2. Create a stored procedure called sp_orders_by_dates displaying the orders shipped
between particular dates. The start and end date will be input parameters for the stored
procedure. Display the order id, customer id, and shipped date from the Orders table, the
company name from the Customer table, and the shipper name from the Shippers table.
Use the following query to test your stored procedure to produce the result set listed below.
D3. Create a stored procedure called sp_products listing a specified product ordered during a
specified month and year. The product name, month, and year will be input parameters for
the stored procedure. Display the product name, unit price, and units in stock from the
Products table, and the supplier name from the Suppliers table. Use the following query to
test your stored procedure to produce the result set listed below.
(4 row(s) affected)
Page 9
Comp 1630 - Relational Database & SQL
Term Project
D4. Create a stored procedure called sp_unit_prices listing the products where the unit price
is between particular values. The two unit prices will be input parameters for the stored
procedure. Display the product id, product name, English name, and unit price from the
Products table. Use the following query to test your stored procedure to produce the result
set listed below.
(4 row(s) affected)
D5. Create a stored procedure called sp_customer_city displaying the customers living in a
particular city. The city will be an input parameter for the stored procedure. Display the
customer id, company name, address, city and phone from the Customers table. Use the
following query to test your stored procedure to produce the result set listed below.
(2 row(s) affected)
D6. Create a stored procedure called sp_reorder_qty to show when the reorder level
subtracted from the units in stock is less than a specified value. The unit value will be an
input parameter for the stored procedure. Display the product id, product name, units in
stock, and reorder level from the Products table, and the supplier name from the Suppliers
table. Use the following query to test your stored procedure to produce the result set listed
below.
EXEC sp_reorder_qty 9
Page 10
Comp 1630 - Relational Database & SQL
Term Project
D7. Create a stored procedure called sp_shipping_date where the shipped date is equal to the
order date plus 10 days. The shipped date will be an input parameter for the stored
procedure. Display the order id, order date and shipped date from the Orders table, the
company name from the Customers table, and the company name from the Shippers table.
Use the following query to test your stored procedure to produce the result set listed below.
(3 row(s) affected)
D8. Create a stored procedure called sp_del_inactive_cust to delete customers that have no
orders. Use the following query to test your procedure. The stored procedure should delete
1 row.
EXEC sp_del_inactive_cust
D9. Create an UPDATE trigger called tr_check_qty on the OrderDetails table to prevent the
updating of orders for products in the Products table that have units-in-stock less than the
quantity ordered. Use the following query to test your trigger.
UPDATE OrderDetails
SET Quantity = 40
WHERE OrderID = 10044
AND ProductID = 77
D10. Create an INSTEAD OF INSERT trigger called tr_insert_shippers on the Shippers table
preventing inserting a row with a company name which already exists. Use the following
query to test your trigger.
INSERT Shippers
VALUES ( 4, 'Federal Shipping' )
Page 11