Lab5 Chapter08
Lab5 Chapter08
ASSIGNMENT
For MiniMun online supermarket management system, there is a relational database schema as follows:
Description:
- PK are fields participating in the primary key of the table
- CUSTOMER: stores customer information.
- ORDER: stores order information for each customer order.
- ORDERDETAILS: stores detailed information about items for each order, the
QuantityOrder column stores information about the number of items for each order.
- PRODUCT: stores information of each item in the system.
- DISCOUNT: stores information about promotions for each specific time.
Students are only allowed to write 1 SQL statement for each of the following requirements:
1. Perform the following request:
a. Create a view named V_Customer to see information of all DateOrder orders smaller
than 06/15/2022 of customers in the city "Da Nang".
b. Through the V_Customer view, update DateOrder to 06/15/2023 for customers who
placed orders on 06/15/2020.
2. Create 2 procedures:
a. Procedure Sp_1: Used to delete information about products with ProductId passed as a
parameter of the procedure.
b. Procedure Sp_2: Used to add new records to the ORDERDETAILS table (Sp_2 must
check the validity of the added data to ensure it does not duplicate the primary key and
ensure reference integrity to related tables).
3. Write 2 triggers for the ORDERDETAILS table according to the following requirements:
a. Trigger_1: When registering a new order for a customer, update the number of products
in the product table (the number of products remaining after being sold).
Event traps only handle 1 record.
b. Trigger_2: When updating the product quantity that the customer has ordered, check to
see if the updated quantity is appropriate (Quantity must be greater than 1 and less than
100).
- If the data is valid, update is allowed.
- Otherwise, display the message "The number of products ordered must be between 1 and
100" and rollback the transaction.
4. Create a user-defined function to calculate the discount percentage for orders when
making purchases in the year 2023 of the customer, CustomerId will be passed through
the input parameter of function.
As follows:
- If the total amount the customer has paid for all orders is less than 10,000,000, the result
is that the customer will receive a 20% discount on each subsequent order purchased in
2024.
- If the total amount the customer has paid for all orders is from 20,000,000 onwards, the
result is that the customer will receive a 30% discount on each next order purchased in
2024.
The function must display CustomerId information, number of orders purchased, Total
amount corresponding to each order, Discount percentage.
5. Create the Sp_Product procedure to find products that have been ordered by customers 2
or more times to delete information about those products in the PRODUCT table and
delete information about orders related to those products ( that is, you must delete the
records in the ORDER and ORDERDETAILS tables related to those products).
-------------------------------END-------------------------------