Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion 10_stored_routines_as_functions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
-- Write your SQL code here
-- 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 ;
27 changes: 26 additions & 1 deletion 11_views/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
-- Write your SQL code here
-- 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
13 changes: 12 additions & 1 deletion 12_database_transactions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
-- Write your SQL code here
-- 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;
18 changes: 17 additions & 1 deletion 1_database/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
-- Write your SQL code here
-- 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;
102 changes: 101 additions & 1 deletion 2_base_tables/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,101 @@
-- Write your SQL code here
-- 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;
87 changes: 86 additions & 1 deletion 3_normal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
-- Write your SQL code here
-- 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 ;
Loading