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
20 changes: 19 additions & 1 deletion 10_stored_routines_as_functions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
-- Write your SQL code here
-- Count the total number of orders handled by the employee
DELIMITER //

CREATE FUNCTION calc_total_orders_by_employee(employee_id INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE total_orders INT;


SELECT COUNT(*) INTO total_orders
FROM Order
WHERE employee_id = employee_id;

RETURN total_orders;
END;
//

DELIMITER ;
15 changes: 14 additions & 1 deletion 11_views/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
-- see the order details along with customer information(history):

CREATE VIEW ClientOrderHistory AS
SELECT c.customer_name, o.order_id, o.order_date, o.order_quantity, p.product_name
FROM Order o
JOIN Customer c ON o.customer_id = c.customer_id
JOIN Product p ON o.product_id = p.product_id;

-- View to show employee performance overview
CREATE VIEW EmployeePerformanceOverview AS
SELECT e.employee_name, e.employee_role, e.performance_rating, d.department_name
FROM Employee e
JOIN Department d ON e.department_id = d.department_id;
11 changes: 10 additions & 1 deletion 12_database_transactions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
-- Write your SQL code here
-- Create a transaction that handles order placement
START TRANSACTION;

INSERT INTO Order (order_id, order_quantity, order_date, customer_id)
VALUES (101, 2, '2024-11-20', 1);

INSERT INTO Payments (payment_id, order_id, amount_paid)
VALUES (1001, 101, 200);

COMMIT;
24 changes: 23 additions & 1 deletion 3_normal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
-- Write your SQL code here
-- Trigger to set order status to "Pending Payment" after a new order is created
DELIMITER //
CREATE TRIGGER trg_set_order_status
AFTER INSERT ON Order
FOR EACH ROW
BEGIN
UPDATE Order
SET order_status = 1 -- Pending Payment
WHERE order_id = NEW.order_id;
END //
DELIMITER ;

-- Trigger to validate employee performance rating before insert/update
DELIMITER //
CREATE TRIGGER trg_check_employee_performance
BEFORE INSERT OR UPDATE ON Employee
FOR EACH ROW
BEGIN
IF NEW.performance_rating > 5 OR NEW.performance_rating < 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Performance rating must be between 1 and 5';
END IF;
END //
DELIMITER ;
56 changes: 55 additions & 1 deletion 4_check_constraints/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
-- Write your SQL code here
--Tables Without Check Constraints
CREATE TABLE Department (
department_id INT AUTO_INCREMENT PRIMARY KEY,
department_name VARCHAR(100) NOT NULL,
location VARCHAR(100) NOT NULL,
department_head VARCHAR(100) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE Employee (
employee_id INT AUTO_INCREMENT PRIMARY KEY,
employee_name VARCHAR(100) NOT NULL,
employee_role VARCHAR(100) NOT NULL,
department_id INT NOT NULL,
FOREIGN KEY (department_id) REFERENCES Department(department_id),
INDEX (department_id)
) ENGINE=InnoDB;

CREATE TABLE Customer (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(100) NOT NULL,
customer_email VARCHAR(100) NOT NULL UNIQUE
) ENGINE=InnoDB;

CREATE TABLE Order (
order_id INT AUTO_INCREMENT PRIMARY KEY,
order_quantity INT NOT NULL,
order_date DATETIME NOT NULL,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
) ENGINE=InnoDB;

CREATE TABLE Product (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
product_quantity INT NOT NULL,
product_price DECIMAL(10, 2)
) ENGINE=InnoDB;


--Tables With Check Constraints

CREATE TABLE Product (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
product_quantity INT NOT NULL CHECK (product_quantity >= 0),
product_price DECIMAL(10, 2) NOT NULL CHECK (product_price > 0)
) ENGINE=InnoDB;

CREATE TABLE Order(
order_id INT AUTO_INCREMENT PRIMARY KEY,
order_quantity INT NOT NULL CHECK (order_quantity > 0),
order_date DATE NOT NULL,
customer_id INT NOT NULL,
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id),
) ENGINE=InnoDB;
25 changes: 24 additions & 1 deletion 5_temporal_triggers/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
-- Trigger to update inventory levels after order dispatch
DELIMITER //
CREATE TRIGGER trg_update_inventory_after_dispatch
AFTER UPDATE ON `Order`
FOR EACH ROW
BEGIN
IF NEW.dispatch_date IS NOT NULL AND OLD.dispatch_date IS NULL THEN
UPDATE Product
SET product_quantity = product_quantity - NEW.order_quantity
WHERE product_id = NEW.product_id;
END IF;
END //
DELIMITER ;

-- Trigger to archive old orders before deletion
DELIMITER //
CREATE TRIGGER trg_archive_old_orders
BEFORE DELETE ON `Order`
FOR EACH ROW
BEGIN
INSERT INTO OrderArchive SELECT * FROM `Order` WHERE order_id = OLD.order_id;
END //
DELIMITER ;
25 changes: 24 additions & 1 deletion 9_stored_routines_as_procedures/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
-- Write your SQL code here
-- Procedure to generate a monthly sales report
DELIMITER //
CREATE PROCEDURE GenerationOfMonthlySalesReport()
BEGIN
SELECT
p.product_name,
SUM(o.order_quantity) AS total_sales,
SUM(o.order_quantity * p.product_price) AS total_revenue
FROM Order o
JOIN Product p ON o.product_id = p.product_id
WHERE o.order_date BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
GROUP BY p.product_name;
END //
DELIMITER ;

-- Procedure to retrieve employee performance by department
DELIMITER //
CREATE PROCEDURE RetrievalOfEmployeePerformance(department_id INT)
BEGIN
SELECT employee_name, performance_rating
FROM Employee
WHERE department_id = department_id;
END //
DELIMITER ;
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[![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 |
Expand Down