Skip to content
14 changes: 13 additions & 1 deletion 10_stored_routines_as_functions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
-- Write your SQL code here
-- Write your SQL code here
DELIMITER $$

CREATE FUNCTION GetTotalStockValue()
RETURNS DECIMAL(10, 2)
DETERMINISTIC
BEGIN
DECLARE total DECIMAL(10, 2);
SELECT SUM(Price * Stock) INTO total FROM Product;
RETURN total;
END $$

DELIMITER ;
40 changes: 39 additions & 1 deletion 11_views/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE VIEW CustomerOrders AS
SELECT
c.customer_id,
CONCAT(c.first_name, ' ', c.last_name) AS customer_name,
o.order_id,
o.order_date,
o.total_amount,
o.status
FROM
Customer c
INNER JOIN
Orders o ON c.customer_id = o.customer_id;

CREATE VIEW ProductInventory AS
SELECT
p.product_id,
p.product_name,
p.price,
p.stock_quantity,
s.supplier_name,
p.category
FROM
Product p
LEFT JOIN
Supplier s ON p.supplier_id = s.supplier_id;


CREATE VIEW SupplierProducts AS
SELECT
s.supplier_id,
s.supplier_name,
p.product_name,
p.stock_quantity
FROM
Supplier s
INNER JOIN
Product p ON s.supplier_id = p.supplier_id;

71 changes: 70 additions & 1 deletion 12_database_transactions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
-- Write your SQL code here
-- Write your SQL code here
START TRANSACTION;

-- Step 1: Insert the order into the Orders table
INSERT INTO Orders (customer_id, order_date, total_amount, status)
VALUES (1, NOW(), 250.00, 'Pending Payment');

-- Retrieve the auto-generated order_id for the inserted order
SET @last_order_id = LAST_INSERT_ID();

-- Step 2: Insert order details into the OrderDetails table
INSERT INTO OrderDetails (order_id, product_id, quantity, unit_price)
VALUES
(@last_order_id, 101, 2, 100.00), -- Product ID 101, quantity 2
(@last_order_id, 102, 1, 50.00); -- Product ID 102, quantity 1

-- Step 3: Update the product inventory
UPDATE Product
SET stock_quantity = stock_quantity - 2
WHERE product_id = 101;

UPDATE Product
SET stock_quantity = stock_quantity - 1
WHERE product_id = 102;

-- If everything is successful, commit the transaction
COMMIT;


START TRANSACTION;

BEGIN TRY
-- Step 1: Insert a new customer
INSERT INTO Customer (first_name, last_name, email, contact_info, address)
VALUES ('John', 'Doe', 'johndoe@example.com', '123-456-7890', '123 Main Street');

-- Retrieve the customer_id for the inserted customer
SET @last_customer_id = LAST_INSERT_ID();

-- Step 2: Add a new order for the customer
INSERT INTO Orders (customer_id, order_date, total_amount, status)
VALUES (@last_customer_id, NOW(), 100.00, 'Pending Payment');

-- Commit if all operations succeed
COMMIT;

EXCEPTION
-- Rollback if any operation fails
ROLLBACK;
END TRY;


START TRANSACTION;

-- Step 1: Deduct payment from the customer's account balance
UPDATE Customer
SET account_balance = account_balance - 200
WHERE customer_id = 1;

-- Step 2: Update the order status to "Paid"
UPDATE Orders
SET status = 'Paid'
WHERE order_id = 1;

-- Check if both updates were successful
IF ROW_COUNT() > 0 THEN
COMMIT;
ELSE
ROLLBACK;
END IF;
25 changes: 24 additions & 1 deletion 2_base_tables/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE TABLE Employee (
employeeId INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(50),
lastName VARCHAR(50),
departmentId INT,
hireDate DATE,
performanceRating DECIMAL(3, 2),
INDEX (lastName),
INDEX (departmentId),
INDEX (hireDate)
) ENGINE=InnoDB;

CREATE TABLE `Order` (
orderId INT AUTO_INCREMENT PRIMARY KEY,
customerId INT,
employeeId INT,
orderDate DATE,
status ENUM('Pending', 'Shipped', 'Completed', 'Cancelled'),
totalAmount DECIMAL(10, 2),
INDEX (customerId),
INDEX (orderDate),
INDEX (status)
) ENGINE=InnoDB;
21 changes: 20 additions & 1 deletion 3_normal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE TRIGGER before_insert_employee_performance
BEFORE INSERT ON Employee
FOR EACH ROW
BEGIN
IF NEW.performanceRating > 5 OR NEW.performanceRating < 0 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Performance rating must be between 0 and 5.';
END IF;
END;

CREATE TRIGGER after_insert_order
AFTER INSERT ON `Order`
FOR EACH ROW
BEGIN
UPDATE `Order`
SET status = 'Pending'
WHERE orderId = NEW.orderId;
END;

23 changes: 22 additions & 1 deletion 4_check_constraints/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE TABLE Product (
productId INT AUTO_INCREMENT PRIMARY KEY,
productName VARCHAR(100),
supplierId INT,
category VARCHAR(50),
price DECIMAL(10, 2),
stockQuantity INT,
CHECK (price > 0),
CHECK (stockQuantity >= 0),
INDEX (category),
INDEX (price)
) ENGINE=InnoDB;

CREATE TABLE Supplier (
supplierId INT AUTO_INCREMENT PRIMARY KEY,
companyName VARCHAR(100),
contactInfo VARCHAR(50),
city VARCHAR(50),
INDEX (city)
) ENGINE=InnoDB;

22 changes: 21 additions & 1 deletion 5_temporal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE TRIGGER after_order_dispatch
AFTER UPDATE ON `Order`
FOR EACH ROW
BEGIN
IF NEW.status = 'Shipped' THEN
UPDATE Product
SET stockQuantity = stockQuantity - NEW.quantity
WHERE productId = NEW.productId;
END IF;
END;

CREATE TRIGGER before_delete_old_orders
BEFORE DELETE ON `Order`
FOR EACH ROW
BEGIN
IF DATEDIFF(CURRENT_DATE, OLD.orderDate) > 365 THEN
INSERT INTO ArchivedOrders SELECT * FROM `Order` WHERE orderId = OLD.orderId;
END IF;
END;

51 changes: 50 additions & 1 deletion 6_storage_engines/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE TABLE Product (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL CHECK (price > 0),
stock INT NOT NULL CHECK (stock >= 0),
supplier_id INT NOT NULL,
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
) ENGINE=InnoDB;

CREATE TABLE Customer (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(15) CHECK (CHAR_LENGTH(phone) BETWEEN 10 AND 15)
) ENGINE=InnoDB;

CREATE TABLE Supplier (
supplier_id INT AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(255) NOT NULL,
contact_number VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE Orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
total_amount DECIMAL(10, 2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
) ENGINE=InnoDB;

CREATE TABLE OrderDetails (
order_detail_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL CHECK (quantity > 0),
price DECIMAL(10, 2) NOT NULL CHECK (price > 0),
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (product_id) REFERENCES Product(product_id)
) ENGINE=InnoDB;

CREATE TABLE ProductLog (
log_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT NOT NULL,
old_price DECIMAL(10, 2),
new_price DECIMAL(10, 2),
change_date DATE NOT NULL,
FOREIGN KEY (product_id) REFERENCES Product(product_id)
) ENGINE=InnoDB;
28 changes: 27 additions & 1 deletion 7_indexes/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
-- Write your SQL code here
-- Write your SQL code
-- Add a unique index for the email column in the Customer table
CREATE UNIQUE INDEX idx_customer_email
ON Customer (email);

-- Add a foreign key index for the supplier_id column in the Product table
CREATE INDEX idx_product_supplier_id
ON Product (supplier_id);

-- Add a foreign key index for the customer_id column in the Orders table
CREATE INDEX idx_orders_customer_id
ON Orders (customer_id);

-- Add a foreign key index for the order_id and product_id columns in the OrderDetails table
CREATE INDEX idx_orderdetails_order_id
ON OrderDetails (order_id);

CREATE INDEX idx_orderdetails_product_id
ON OrderDetails (product_id);

-- Add an index for the price column in the Product table to speed up price-related queries
CREATE INDEX idx_product_price
ON Product (price);

-- Add an index for the change_date column in the ProductLog table to optimize date-based queries
CREATE INDEX idx_productlog_change_date
ON ProductLog (change_date);
43 changes: 42 additions & 1 deletion 8_dummy_data/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,42 @@
-- Write your SQL code here
-- Write your SQL code here
-- Insert dummy data into Product table
INSERT INTO Product (product_id, product_name, price, stock, supplier_id)
VALUES
(1, 'Laptop', 800.00, 50, 1),
(2, 'Smartphone', 500.00, 200, 2),
(3, 'Headphones', 100.00, 150, 3);

-- Insert dummy data into Customer table
INSERT INTO Customer (customer_id, customer_name, email, phone)
VALUES
(1, 'Alice Johnson', 'alice.johnson@example.com', '1234567890'),
(2, 'Bob Smith', 'bob.smith@example.com', '9876543210'),
(3, 'Cathy Brown', 'cathy.brown@example.com', '4567891230');

-- Insert dummy data into Supplier table
INSERT INTO Supplier (supplier_id, supplier_name, contact_number, address)
VALUES
(1, 'Tech Supply Co.', '1234567890', '123 Tech Street'),
(2, 'Mobile Distributors Ltd.', '9876543210', '456 Mobile Ave'),
(3, 'Gadget World', '4567891230', '789 Gadget Blvd');

-- Insert dummy data into Orders table
INSERT INTO Orders (order_id, customer_id, order_date, total_amount)
VALUES
(1, 1, '2024-11-20', 1300.00),
(2, 2, '2024-11-21', 500.00),
(3, 3, '2024-11-22', 300.00);

-- Insert dummy data into OrderDetails table
INSERT INTO OrderDetails (order_detail_id, order_id, product_id, quantity, price)
VALUES
(1, 1, 1, 1, 800.00),
(2, 1, 3, 5, 500.00),
(3, 2, 2, 1, 500.00),
(4, 3, 3, 3, 300.00);

-- Insert dummy data into ProductLog table
INSERT INTO ProductLog (log_id, product_id, old_price, new_price, change_date)
VALUES
(1, 1, 800.00, 850.00, '2024-11-01'),
(2, 2, 500.00, 550.00, '2024-11-10');
15 changes: 14 additions & 1 deletion 9_stored_routines_as_procedures/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
-- Write your SQL code here
-- Write your SQL code here
DELIMITER $$

CREATE PROCEDURE AddProduct (
IN p_ProductName VARCHAR(100),
IN p_Price DECIMAL(10, 2),
IN p_SupplierID INT
)
BEGIN
INSERT INTO Product (ProductName, Price, SupplierID)
VALUES (p_ProductName, p_Price, p_SupplierID);
END $$

DELIMITER ;
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[![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** | Del Monte|
| **Industry** | Del Monte Companies |
| **Group Name** | B4 |
| **Semester Duration** | 19<sup>th</sup> August - 25<sup>th</sup> November 2024 |

# Cause-and-Effect Diagram
Expand Down