diff --git a/10_stored_routines_as_functions/script.sql b/10_stored_routines_as_functions/script.sql index e3ee0ca..f6eceb2 100644 --- a/10_stored_routines_as_functions/script.sql +++ b/10_stored_routines_as_functions/script.sql @@ -1 +1,31 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--1.Calculate Total Cost for an Order +DELIMITER // + +CREATE FUNCTION calculate_order_total (order_id_in INT) +RETURNS DECIMAL(10, 2) +DETERMINISTIC +BEGIN + DECLARE total_cost DECIMAL(10, 2) DEFAULT 0; + SELECT SUM(m.price * oi.quantity) INTO total_cost + FROM Order_Item oi + INNER JOIN Menu m ON oi.dish_id = m.dish_id + WHERE oi.order_id = order_id_in; + RETURN total_cost; +END // + +DELIMITER ; + +--2.Calculate Average Menu Item Cost by Category +DELIMITER // + +CREATE FUNCTION calculate_average_menu_cost (category_in VARCHAR(50)) +RETURNS DECIMAL(10, 2) +DETERMINISTIC +BEGIN + DECLARE avg_cost DECIMAL(10, 2); + SELECT AVG(price) INTO avg_cost FROM Menu WHERE category = category_in; + RETURN avg_cost; +END // + +DELIMITER ; \ No newline at end of file diff --git a/11_views/script.sql b/11_views/script.sql index e3ee0ca..697e375 100644 --- a/11_views/script.sql +++ b/11_views/script.sql @@ -1 +1,26 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +-- Write your SQL code here + +CREATE VIEW ReservationSummary AS +SELECT + r.reservation_id, + c.customer_name, + c.customer_contact, + r.reservation_timeid +JOIN Tables t ON r.table_id = t.table_id; + + + +-- View: Customer Order Summary +CREATE VIEW CustomerOrderSummary AS +SELECT + c.customer_name, + c.customer_contact, + o.order_id, + o.order_timestamp, + o.total_amount, + p.payment_date, + p.amount, + p.payment_method -- Assuming PaymentMethod table linked to Payment +FROM Order_Table o +JOIN Customers c ON o.customer_id = c.customer_id diff --git a/12_database_transactions/script.sql b/12_database_transactions/script.sql index e3ee0ca..c8c99d8 100644 --- a/12_database_transactions/script.sql +++ b/12_database_transactions/script.sql @@ -1 +1,12 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +START TRANSACTION; + +-- Transaction 1: Add a new order +INSERT INTO `Order` (Order_ID, Customer_ID, Order_Date, Expected_Delivery_Date, Status, Total_Amount) +VALUES (1, 1, NOW(), DATE_ADD(NOW(), INTERVAL 7 DAY), 1, 100); + +--Transaction 2: Add a new payment +INSERT INTO Payment (Payment_ID, Payment_Date, Order_ID, Amount, Payment_Method, Payment_Status) +VALUES (1, NOW(), 1, 100, 'Credit Card', 1); + +COMMIT; \ No newline at end of file diff --git a/1_database/script.sql b/1_database/script.sql index e3ee0ca..e871619 100644 --- a/1_database/script.sql +++ b/1_database/script.sql @@ -1 +1,17 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +-- Database: cjs +-- DROP DATABASE IF EXISTS cjs; +CREATE DATABASE cjs WITH +OWNER = postgres +TEMPLATE = template0 +ENCODING = 'UTF8' +CONNECTION LIMIT = 10; + +-- SCHEMA: cjs_schema +-- DROP SCHEMA IF EXISTS cjs_schema; +CREATE SCHEMA cjs_schema + AUTHORIZATION postgres; +COMMENT ON SCHEMA cjs_schema + IS 'CJs schema as used by Advanced Databases group A7 (2024)'; +GRANT USAGE ON SCHEMA cjs_schema TO postgres; +GRANT ALL ON SCHEMA cjs_schema TO postgres; \ No newline at end of file diff --git a/2_base_tables/script.sql b/2_base_tables/script.sql index e3ee0ca..b7f5a20 100644 --- a/2_base_tables/script.sql +++ b/2_base_tables/script.sql @@ -1 +1,101 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--Tables without check constraints +CREATE TABLE Menu ( + Menu_ID INT PRIMARY KEY, + Description VARCHAR(100), + Cost INT, + Category VARCHAR(100) +) ENGINE=InnoDB; + +CREATE TABLE Menu_Ingredient ( + Menu_ID INT, + Ingredient VARCHAR(100), + PRIMARY KEY (Menu_ID, Ingredient), + FOREIGN KEY (Menu_ID) REFERENCES Menu(Menu_ID) +) ENGINE=InnoDB; + +CREATE TABLE Supplier ( + Supplier_ID INT PRIMARY KEY, + Item_Description VARCHAR(100) +) ENGINE=InnoDB; + +CREATE TABLE Supplier_Frequency ( + Supplier_ID INT, + Frequency TINYINT, + PRIMARY KEY (Supplier_ID, Frequency), + FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID) +) ENGINE=InnoDB; + +CREATE TABLE Supplier_Payment ( + Supplier_ID INT, + Payment_Method TINYINT, + Payment_Date TIMESTAMP, + Payment_Status TINYINT, + Order_Status TINYINT, + PRIMARY KEY (Supplier_ID, Payment_Date), + FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID) +) ENGINE=InnoDB; + +CREATE TABLE Reservation ( + Reservation_ID INT PRIMARY KEY, + Table_ID INT, + Customer_ID INT, + Customer_Name VARCHAR(100), + Customer_Contact VARCHAR(15), + Capacity INT, + Reservation_Date TIMESTAMP +) ENGINE=InnoDB; + +CREATE TABLE Department ( + Department_ID INT PRIMARY KEY, + Department_Name VARCHAR(100), + Head_of_Department_ID INT, + Budget_Allocated INT +) ENGINE=MyISAM; + +CREATE TABLE `Order` ( + Order_ID INT PRIMARY KEY, + Customer_ID INT, + Order_Date TIMESTAMP, + Expected_Delivery_Date TIMESTAMP, + Actual_Delivery_Date TIMESTAMP, + Status TINYINT, + Total_Amount INT +) ENGINE=InnoDB; + +CREATE TABLE Payment ( + Payment_ID INT PRIMARY KEY, + Payment_Date TIMESTAMP, + Order_ID INT, + Amount INT, + Payment_Method VARCHAR(50), + Payment_Status INT, + FOREIGN KEY (Order_ID) REFERENCES `Order`(Order_ID) +) ENGINE=InnoDB; + +CREATE TABLE Customer ( + Customer_ID INT PRIMARY KEY, + Customer_Name VARCHAR(100), + Customer_Contact VARCHAR(15), + Street_Address VARCHAR(255), + City VARCHAR(255), + Postal_Code VARCHAR(255) +) ENGINE=InnoDB; + +CREATE TABLE Delivery ( + Delivery_ID INT PRIMARY KEY, + Customer_ID INT, + Delivery_Type VARCHAR(30), + Item_ID INT, + Employee_ID INT, + Delivery_Status VARCHAR(20), + Scheduled_Date TIMESTAMP, + FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID) +) ENGINE=InnoDB; + +CREATE TABLE Employee ( + Employee_ID INT PRIMARY KEY, + Employee_Name VARCHAR(100), + Position_ID INT, + Hire_Date TIMESTAMP +) ENGINE=InnoDB; \ No newline at end of file diff --git a/3_normal_triggers/script.sql b/3_normal_triggers/script.sql index e3ee0ca..5624d80 100644 --- a/3_normal_triggers/script.sql +++ b/3_normal_triggers/script.sql @@ -1 +1,86 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +-- normal triggers + +--trigger 1 +DELIMITER // + +CREATE TRIGGER check_stock_before_order +BEFORE INSERT ON Orders +FOR EACH ROW +BEGIN + DECLARE insufficient_stock BOOLEAN DEFAULT FALSE; + DECLARE ingredient_stock INT; + + DECLARE done INT DEFAULT FALSE; + DECLARE cur CURSOR FOR + SELECT i.stock_level + FROM DishIngredients di + JOIN Ingredients i ON di.ingredient_id = i.ingredient_id + WHERE di.dish_id = NEW.dish_id; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + OPEN cur; + + read_loop: LOOP + FETCH cur INTO ingredient_stock; + IF done THEN + LEAVE read_loop; + END IF; + + -- Check if sufficient stock for each ingredient + IF ingredient_stock < (SELECT quantity FROM DishIngredients WHERE dish_id = NEW.dish_id AND ingredient_id = (SELECT ingredient_id FROM DishIngredients WHERE dish_id = NEW.dish_id LIMIT 1)) THEN + SET insufficient_stock = TRUE; + LEAVE read_loop; + END IF; + END LOOP; + + CLOSE cur; + + IF insufficient_stock THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insufficient stock for this order.'; + END IF; +END; // + +DELIMITER ; + +--trigger 2 +DELIMITER // + +CREATE TRIGGER check_stock_before_order_update +BEFORE UPDATE ON Orders +FOR EACH ROW +BEGIN + DECLARE insufficient_stock BOOLEAN DEFAULT FALSE; + DECLARE ingredient_stock INT; + + DECLARE done INT DEFAULT FALSE; + DECLARE cur CURSOR FOR + SELECT i.stock_level + FROM DishIngredients di + JOIN Ingredients i ON di.ingredient_id = i.ingredient_id + WHERE di.dish_id = NEW.dish_id; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + OPEN cur; + + read_loop: LOOP + FETCH cur INTO ingredient_stock; + IF done THEN + LEAVE read_loop; + END IF; + + -- Check if sufficient stock for each ingredient + IF ingredient_stock < (SELECT quantity FROM DishIngredients WHERE dish_id = NEW.dish_id AND ingredient_id = (SELECT ingredient_id FROM DishIngredients WHERE dish_id = NEW.dish_id LIMIT 1)) THEN + SET insufficient_stock = TRUE; + LEAVE read_loop; + END IF; + END LOOP; + + CLOSE cur; + + IF insufficient_stock THEN + SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Insufficient stock for this order.'; + END IF; +END; // + +DELIMITER ; \ No newline at end of file diff --git a/4_check_constraints/script.sql b/4_check_constraints/script.sql index e3ee0ca..5d1fa53 100644 --- a/4_check_constraints/script.sql +++ b/4_check_constraints/script.sql @@ -1 +1,102 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--Tables with check constraints + +CREATE TABLE Menu ( + Menu_ID INT PRIMARY KEY, + Description VARCHAR(100), + Cost INT CHECK (Cost >= 0), -- Ensure Cost is non-negative + Category VARCHAR(100) +) ENGINE=InnoDB; + +CREATE TABLE Menu_Ingredient ( + Menu_ID INT, + Ingredient VARCHAR(100), + PRIMARY KEY (Menu_ID, Ingredient), + FOREIGN KEY (Menu_ID) REFERENCES Menu(Menu_ID) +) ENGINE=InnoDB; + +CREATE TABLE Supplier ( + Supplier_ID INT PRIMARY KEY, + Item_Description VARCHAR(100) +) ENGINE=InnoDB; + +CREATE TABLE Supplier_Frequency ( + Supplier_ID INT, + Frequency TINYINT CHECK (Frequency >= 0), -- Ensure Frequency is non-negative + PRIMARY KEY (Supplier_ID, Frequency), + FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID) +) ENGINE=InnoDB; + +CREATE TABLE Supplier_Payment ( + Supplier_ID INT, + Payment_Method TINYINT CHECK (Payment_Method >= 0), -- Ensure Payment_Method is non-negative + Payment_Date TIMESTAMP, + Payment_Status TINYINT CHECK (Payment_Status >= 0), -- Ensure Payment_Status is non-negative + Order_Status TINYINT CHECK (Order_Status >= 0), -- Ensure Order_Status is non-negative + PRIMARY KEY (Supplier_ID, Payment_Date), + FOREIGN KEY (Supplier_ID) REFERENCES Supplier(Supplier_ID) +) ENGINE=InnoDB; + +CREATE TABLE Reservation ( + Reservation_ID INT PRIMARY KEY, + Table_ID INT CHECK (Table_ID > 0), -- Ensure Table_ID is positive + Customer_ID INT, + Customer_Name VARCHAR(100), + Customer_Contact VARCHAR(15), + Capacity INT CHECK (Capacity > 0), -- Ensure Capacity is positive + Reservation_Date TIMESTAMP +) ENGINE=InnoDB; + +CREATE TABLE Department ( + Department_ID INT PRIMARY KEY, + Department_Name VARCHAR(100), + Head_of_Department_ID INT, + Budget_Allocated INT CHECK (Budget_Allocated >= 0) -- Ensure Budget_Allocated is non-negative +) ENGINE=MyISAM; + +CREATE TABLE `Order` ( + Order_ID INT PRIMARY KEY, + Customer_ID INT, + Order_Date TIMESTAMP, + Expected_Delivery_Date TIMESTAMP, + Actual_Delivery_Date TIMESTAMP, + Status TINYINT CHECK (Status >= 0), -- Ensure Status is non-negative + Total_Amount INT CHECK (Total_Amount >= 0) -- Ensure Total_Amount is non-negative +) ENGINE=InnoDB; + +CREATE TABLE Payment ( + Payment_ID INT PRIMARY KEY, + Payment_Date TIMESTAMP, + Order_ID INT, + Amount INT CHECK (Amount >= 0), -- Ensure Amount is non-negative + Payment_Method VARCHAR(50), + Payment_Status INT CHECK (Payment_Status >= 0), -- Ensure Payment_Status is non-negative + FOREIGN KEY (Order_ID) REFERENCES `Order`(Order_ID) +) ENGINE=InnoDB; + +CREATE TABLE Customer ( + Customer_ID INT PRIMARY KEY, + Customer_Name VARCHAR(100), + Customer_Contact VARCHAR(15), + Street_Address VARCHAR(255), + City VARCHAR(255), + Postal_Code VARCHAR(255) +) ENGINE=InnoDB; + +CREATE TABLE Delivery ( + Delivery_ID INT PRIMARY KEY, + Customer_ID INT, + Delivery_Type VARCHAR(30), + Item_ID INT, + Employee_ID INT, + Delivery_Status VARCHAR(20), + Scheduled_Date TIMESTAMP, + FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID) +) ENGINE=InnoDB; + +CREATE TABLE Employee ( + Employee_ID INT PRIMARY KEY, + Employee_Name VARCHAR(100), + Position_ID INT, + Hire_Date TIMESTAMP +) ENGINE=InnoDB; \ No newline at end of file diff --git a/5_temporal_triggers/script.sql b/5_temporal_triggers/script.sql index e3ee0ca..2f43389 100644 --- a/5_temporal_triggers/script.sql +++ b/5_temporal_triggers/script.sql @@ -1 +1,50 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +-- Temporal triggers +--Trigger for Expiring Reservations +DELIMITER // + +CREATE PROCEDURE expire_reservations() +BEGIN + UPDATE Reservations + SET reservation_status = 'expired' + WHERE reservation_status = 'active' + AND reservation_time < DATE_SUB(NOW(), INTERVAL 30 MINUTE); +END // + +DELIMITER ; + +CREATE EVENT expire_reservations_event +ON SCHEDULE EVERY 15 MINUTE +STARTS CURRENT_TIMESTAMP +DO CALL expire_reservations(); + +--Trigger for Low Stock Notification +DELIMITER // + +CREATE PROCEDURE check_low_stock() +BEGIN + DECLARE done INT DEFAULT FALSE; + DECLARE ingredient_id INT; + DECLARE stock_level INT; + DECLARE cur CURSOR FOR SELECT ingredient_id, stock_level FROM Ingredients WHERE stock_level < 10; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; + + OPEN cur; + + read_loop: LOOP + FETCH cur INTO ingredient_id, stock_level; + IF done THEN + LEAVE read_loop; + END IF; + INSERT INTO Notifications (message) VALUES (CONCAT('Low stock alert: Ingredient ID ', ingredient_id, ' has only ', stock_level, ' units remaining.')); + END LOOP; + + CLOSE cur; +END // + +DELIMITER ; + +CREATE EVENT check_stock_event +ON SCHEDULE EVERY 1 DAY +STARTS CURRENT_TIMESTAMP +DO CALL check_low_stock(); \ No newline at end of file diff --git a/6_storage_engines/script.sql b/6_storage_engines/script.sql index e3ee0ca..f181307 100644 --- a/6_storage_engines/script.sql +++ b/6_storage_engines/script.sql @@ -1 +1,2 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +-- All the tables use InnoDB except Delivery table which uses the MyISAM storage engine (shown when declaring the tables) \ No newline at end of file diff --git a/7_indexes/script.sql b/7_indexes/script.sql index e3ee0ca..3116efb 100644 --- a/7_indexes/script.sql +++ b/7_indexes/script.sql @@ -1 +1,80 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +Menu: +Menu_ID (PK): B+ tree index +Description: B+ tree index +Cost: B+ tree index +Category: B+ tree index + + Menu_Ingredient: +Menu_ID (FK): B+ tree index +Ingredient: B+ tree index + +Supplier: +Supplier_ID (FK): B+ tree index +Item_Description: B+ tree index + +Supplier_Frequency: +Supplier_ID (FK): B+ tree index +Frequency: Hash index + +Supplier_Payment: +Supplier_ID (FK): B+ tree index +Payment_Method: Hash index +Payment_Date: B+ tree +Payment_Status: Hash index +Order_Status: Hash index + +Reservation: +Reservation_ID (PK): B+ tree +Table_ID: B+ tree +Customer_ID: B+ tree +Customer_Name: B+ tree +Customer_Contact: B+ tree +Capacity: B+ tree +Reservation_Date: B+ tree + +Department: +Department_ID (PK): B+ tree +Department_Name: B+ tree +Head_of_Department_ID: B+ tree +Budget_Allocated: B+ tree + +Order: +Order_ID (PK): B+ tree +Customer_ID: B+ tree +Order_Date: B+ tree +Expected_Delivery_Date: B+ tree +Actual_Deliveru_Date: B+ tree +Status: Hash +Total_Amount: B+ tree + +Payment: +Payment_ID (PK): B+ tree +Payment_Date: B+ tree +Order_ID: B+ tree +Amount: B+ tree +Payment_Method: Hash +Payment_Status: Hash + +Customer: +Customer_ID (PK): B+ tree +Customer_Name: B+ tree +Customer_Contact: B+tree +Street_Address: B+ tree +City: B+ tree +Postal_Code: B+ tree + +Delivery: +Delivery_ID (PK): B+ tree +Customer_ID: B+ tree +Delivery_Type: Hash +Item_ID: B+ tree +Employee_ID: B+ tree +Delivery_Status: Hash +Scheduled_Date: B+ tree + +Employee: +Employee_ID (PK): B+ tree +Employee_Name: B+ tree +Position_ID: B+ tree +Hire_Date: B+ tree diff --git a/8_dummy_data/script.sql b/8_dummy_data/script.sql index e3ee0ca..a52147e 100644 --- a/8_dummy_data/script.sql +++ b/8_dummy_data/script.sql @@ -1 +1,81 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--Dummy Data + +-- Insert data into Menu +INSERT INTO Menu (Menu_ID, Description, Cost, Category) VALUES +(1, 'Cheeseburger', 10, 'Main Course'), +(2, 'Caesar Salad', 7, 'Salad'), +(3, 'Margherita Pizza', 12, 'Main Course'), +(4, 'Chocolate Cake', 5, 'Dessert'); + +-- Insert data into Menu_Ingredient +INSERT INTO Menu_Ingredient (Menu_ID, Ingredient) VALUES +(1, 'Beef Patty'), +(1, 'Cheese'), +(1, 'Lettuce'), +(2, 'Romaine Lettuce'), +(2, 'Caesar Dressing'), +(3, 'Pizza Dough'), +(3, 'Tomato Sauce'), +(3, 'Mozzarella Cheese'), +(4, 'Flour'), +(4, 'Cocoa Powder'); + +-- Insert data into Supplier +INSERT INTO Supplier (Supplier_ID, Item_Description) VALUES +(1, 'Beef'), +(2, 'Vegetables'), +(3, 'Dairy'), +(4, 'Baking Supplies'); + +-- Insert data into Supplier_Frequency +INSERT INTO Supplier_Frequency (Supplier_ID, Frequency) VALUES +(1, 7), +(2, 14), +(3, 30), +(4, 60); + +-- Insert data into Supplier_Payment +INSERT INTO Supplier_Payment (Supplier_ID, Payment_Method, Payment_Date, Payment_Status, Order_Status) VALUES +(1, 1, '2024-11-01 10:00:00', 1, 1), +(2, 2, '2024-11-05 10:00:00', 1, 1), +(3, 1, '2024-11-10 10:00:00', 1, 1), +(4, 2, '2024-11-15 10:00:00', 1, 1); + +-- Insert data into Reservation +INSERT INTO Reservation (Reservation_ID, Table_ID, Customer_ID, Customer_Name, Customer_Contact, Capacity, Reservation_Date) VALUES +(1, 1, 1, 'John Doe', '1234567890', 4, '2024-11-20 19:00:00'), +(2, 2, 2, 'Jane Smith', '0987654321', 2, '2024-11-21 18:30:00'), +(3, 3, 3, 'Alice Johnson', '5555555555', 6, '2024-11-22 20:00:00'); + +-- Insert data into Department +INSERT INTO Department (Department_ID, Department_Name, Head_of_Department_ID, Budget_Allocated) VALUES +(1, 'Kitchen', NULL, 5000), +(2, 'Service', NULL, 3000), +(3, 'Management', NULL, 10000); + +-- Insert data into `Order` +INSERT INTO `Order` (Order_ID, Customer_ID, Order_Date, Expected_Delivery_Date, Actual_Delivery_Date, Status, Total_Amount) VALUES +(1, 1, '2024-11-15 10:00:00', '2024-11-20 10:00:00', NULL, 1, 50), +(2, 2, '2024-11-16 10:00:00', '2024-11-21 10:00:00', NULL, 1, 30); + +-- Insert data into Payment +INSERT INTO Payment (Payment_ID, Payment_Date, Order_ID, Amount, Payment_Method, Payment_Status) VALUES +(1, '2024-11-15 11:00:00', 1, 50, 'Credit Card', 1), +(2, '2024-11-16 11:00:00', 2, 30, 'Cash', 1); + +-- Insert data into Customer +INSERT INTO Customer (Customer_ID, Customer_Name, Customer_Contact, Street_Address, City, Postal_Code) VALUES +(1, 'John Doe', '1234567890', '123 Elm St', 'Springfield', '12345'), +(2, 'Jane Smith', '0987654321', '456 Oak St', 'Springfield', '12345'), +(3, 'Alice Johnson', '5555555555', '789 Pine St', 'Springfield', '12345'); + +-- Insert data into Delivery +INSERT INTO Delivery (Delivery_ID, Customer_ID, Delivery_Type, Item_ID, Employee_ID, Delivery_Status, Scheduled_Date) VALUES +(1, 1, 'Standard', 1, 1, 'Pending', '2024-11-20 10:00:00'), +(2, 2, 'Express', 2, 2, 'Pending', '2024-11-21 10:00:00'); + +-- Insert data into Employee +INSERT INTO Employee (Employee_ID, Employee_Name, Position_ID, Hire_Date) VALUES +(1, 'Bob Brown', 1, '2024-01-01'), +(2, 'Sara White', 2, '2024-02-01'); \ No newline at end of file diff --git a/9_stored_routines_as_procedures/script.sql b/9_stored_routines_as_procedures/script.sql index e3ee0ca..03ea9c9 100644 --- a/9_stored_routines_as_procedures/script.sql +++ b/9_stored_routines_as_procedures/script.sql @@ -1 +1,30 @@ --- Write your SQL code here \ No newline at end of file + +--Trigger to Retrieve Upcoming Reservations +DELIMITER $$ + +CREATE PROCEDURE GetUpcomingReservations ( + IN startDate DATE, + IN endDate DATE +) +BEGIN + SELECT * + FROM Reservation + WHERE Reservation_Date BETWEEN startDate AND endDate + ORDER BY Reservation_Date; +END // + +DELIMITER ; + +-- Trigger to Retrieve Total Cost of Order +DELIMITER $$ + +CREATE PROCEDURE GetTotalCostOfOrder ( + IN orderId INT +) +BEGIN + SELECT Total_Amount + FROM `Order` + WHERE Order_ID = orderId; +END // + +DELIMITER ; \ No newline at end of file diff --git a/README.md b/README.md index 8728c63..989b92c 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,140 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/fbNyN1as) # BBT3104: Advanced Database Sytems Semester Project | **Key** | Value | |---------------|---------------------------------------------------------| -| **Name of Business Chosen for the Case Study** | ? | -| **Industry** | ? | -| **Group Name** | ? | +| **Name of Business Chosen for the Case Study** | CJ's Restaurant| +| **Industry** | Hospitality Industry| +| **Group Name** | A7 | | **Semester Duration** | 19th August - 25th November 2024 | # Cause-and-Effect Diagram +![Screenshot 2024-11-21 205533](https://github.com/user-attachments/assets/45a4aade-6b2a-4270-868a-ad869626df15) # Enhanced Entity Relationship Diagram (EERD) +![image](https://github.com/user-attachments/assets/0cc5482c-2b05-4162-875c-0c0e824435d1) +![image](https://github.com/user-attachments/assets/31934031-c0e7-4f06-b797-d8ec2c60be10) # Database Schema +1NF + +Removal of repeating groups Menu Entity Menu_ID (PK): INTEGER Description: VARCHAR(100) Cost: INTEGER Ingredient: VARCHAR(100) Category: VARCHAR(10) Supplier Entity Supplier_ID (PK): INTEGER Item_Description: VARCHAR(100) Supply_Frequency: TINYINT Payment_Method: VARCHAR(50) Payment_Date: TIMESTAMP Payment_Status: INTEGER Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + +2NF Remove partial dependencies + +Menu Entity Menu_ID (PK): VARCHAR Description: VARCHAR(100) Cost: INTEGER Ingredient: VARCHAR(100) Category: VARCHAR(10) Supplier Entity Supplier_ID (PK): VARCHAR(20) Item_Description: VARCHAR(100) Supply_frequence: TINYINT Payment_method: TINYINT Payment_date: TIMESTAMP Payment_Status: TINYINT Order_Status: TINYINT Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP + +Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER + +Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER + +Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) + +Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP + +Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + +3NF Remove transitive dependencies (Affected relation: Supplier) Menu Entity Menu_ID (PK): INTEGER Description: VARCHAR(100) Cost: INTEGER Ingredient: VARCHAR(100) Category: VARCHAR(10) + +Supplier Entity Supplier_ID (PK): INTEGER Item_Description: VARCHAR (100) + +Supplier_Frequency Supplier_ID (FK): INTEGER Frequency: TINYINT + +Supplier_Payment Supplier_ID (FK): INTEGER Payment_Method: TINYINT Payment_Date: TIMESTAMP Payment_Status: TINYINT Order_Status: TINYINT + +Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP + +Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER + +Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER + +Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER + +Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) + +Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP + +Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + +BCNF Ensure all determinants are candidate keys (No changes made from 3NF) Menu Entity Menu_ID (PK): INTEGER Description: VARCHAR(100) Cost: INTEGER Ingredient: VARCHAR(100) Category: VARCHAR(10) + +Supplier Entity Supplier_ID (PK): INTEGER Item_Description: VARCHAR (100) + +Supplier_Frequency Supplier_ID (FK): INTEGER Frequency: TINYINT + +Supplier_Payment Supplier_ID (FK): INTEGER Payment_Method: TINYINT Payment_Date: TIMESTAMP Payment_Status: TINYINT Order_Status: TINYINT + +Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP + +Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER + +Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER + +Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER + +Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) + +Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP + +Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + +4NF Ensure every multi-determinant in a multi-valued dependency is a candidate key (Affected relation: Menu) + +Menu Entity Menu_ID (PK): INTEGER Description: VARCHAR(100) Cost: INTEGER Category: VARCHAR(100) + +Menu_Ingredient MEnu_ID(FK): INTEGER Ingredient: VARCHAR(100) + +Supplier Entity Supplier_ID (PK): INTEGER Item_Description: VARCHAR (100) + +Supplier_Frequency Supplier_ID (FK): INTEGER Frequency: TINYINT + +Supplier_Payment Supplier_ID (FK): INTEGER Payment_Method: TINYINT Payment_Date: TIMESTAMP Payment_Status: TINYINT Order_Status: TINYINT + +Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP + +Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER + +Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER + +Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER + +Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) + +Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP + +Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + +5NF Ensure each projection in a join dependency includes a candidate key of the original relation (No changes made from 4NF) Menu Entity Menu_ID (PK): INTEGER Description: VARCHAR(100) Cost: INTEGER Category: VARCHAR(100) + +Menu_Ingredient MEnu_ID(FK): INTEGER Ingredient: VARCHAR(100) + +Supplier Entity Supplier_ID (PK): INTEGER Item_Description: VARCHAR (100) + +Supplier_Frequency Supplier_ID (FK): INTEGER Frequency: TINYINT + +Supplier_Payment Supplier_ID (FK): INTEGER Payment_Method: TINYINT Payment_Date: TIMESTAMP Payment_Status: TINYINT Order_Status: TINYINT + +Reservation Entity Reservation_ID (PK): INTEGER Table_ID: INTEGER Customer_ID: INTEGER Customer_Name:VARCHAR(100) Customer_Contact:VARCHAR(15) Capacity: INTEGER Reservation_Date: TIMESTAMP + +Department Entity Department_ID (PK): INTEGER Department_Name: VARCHAR(100) Head_of_Department_ID: INTEGER Budget_Allocated: INTEGER + +Order Entity Order_ID (PK): INTEGER Customer_ID: INTEGER Order_Date: TIMESTAMP Expected_Delivery_Date: TIMESTAMP Actual_Delivery_Date: TIMESTAMP Status: TINYINT Total_Amount: INTEGER + +Payment Entity Payment_ID (PK): INTEGER Payment_Date: TIMESTAMP Order_ID: INTEGER Amount: INTEGER Payment_Method: VARCHAR(50) Payment_Status: INTEGER + +Customer Entity Customer_ID (PK): INTEGER Customer_Name: VARCHAR(100) Customer_Contact: VARCHAR(15) Street_Address: VARCHAR(255) City: VARCHAR(255) Postal_Code: VARCHAR(255) + +Delivery Entity Delivery_ID (PK): INTEGER Customer_ID: INTEGER Delivery_Type: VARCHAR(30) Item_ID: INTEGER Employee_ID: INTEGER Delivery_Status: VARCHAR(20) Scheduled_Date: TIMESTAMP + +Employee Entity Employee_ID (PK): INTEGER Employee_Name: VARCHAR(100) Position_ID: INTEGER Hire_Date: TIMESTAMP + + + # System Architecture +N-tier architecture +![Screenshot 2024-11-21 210515](https://github.com/user-attachments/assets/f279fb39-35b1-47b4-827d-3038d09f2908) +