diff --git a/10_stored_routines_as_functions/script.sql b/10_stored_routines_as_functions/script.sql index e3ee0ca..ac12fb3 100644 --- a/10_stored_routines_as_functions/script.sql +++ b/10_stored_routines_as_functions/script.sql @@ -1 +1,64 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here + +--Function to calculate customer lifetime value +DELIMITER // + +CREATE FUNCTION CalculateCustomerLifetimeValue(customerID INT) +RETURNS DECIMAL(10,2) +DETERMINISTIC +BEGIN + DECLARE total_spent DECIMAL(10,2); + DECLARE total_purchases INT; + + --Calculate total spent by the customer + SELECT SUM(Purchase_Amount) INTO total_spent + FROM Purchase p + + JOIN Purchase_Details pd ON p.Purchase_ID = pd.Purchase_ID + + WHERE p.Customer_ID = customer_id; + + --calculate total purchases made by the customer + SELECT COUNT(p.Purchase_ID) INTO total_purchases + + FROM Purchase p + + WHERE p.Customer_ID = customer_id; + + --Calculate customer lifetime value + RETURN IF(total_purchases > 0, total_spent / total_purchases, 0); +END // + +DELIMITER; + +-- Function to Calculate Infrastructure Efficiendy +DELIMITER // + +CREATE FUNCTION CalculateInfrastructureEfficiency(infrastructure_id INT) +RETURNS DECIMAL(5,2) +DETERMINISTIC +BEGIN + DECLARE total_capacity INT; + DECLARE used_resources INT; + + --Calculate total capacity of the infrastructure + SELECT ic.Capacity INTO total_capacity + + FROM Infrastructure i + + JOIN Infrastructure_Capacity ic ON i.Infrastructure_ID = ic.Infrastructure_ID + + WHERE i.Infrastructure_ID = infrastructure_id; + + --calculate number of used resources + SELECT COUNT(r.Resource_ID) INTO used_resources + + FROM Resource r + + WHERE r.Infrastructure_ID = infrastructure_id; + + --calculate Infrastructure efficiency + RETURN IF(total_capacity > 0, (used_resources / total_capacity) * 100, 0); +END // + +DELIMITER; \ No newline at end of file diff --git a/11_views/script.sql b/11_views/script.sql index e3ee0ca..98b46bb 100644 --- a/11_views/script.sql +++ b/11_views/script.sql @@ -1 +1,51 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--view 1: CustomerInsightDashboard +CREATE VIEW CustomerInsightDashboard AS +SELECT + c.Customer_ID, + c.Customer_Name, + c.Email, + c.Phone, + SUM(pd.Purchase_Amount) AS Total_Spent, + COUNT(p.Purchase_ID) AS Total_Purchases, + AVG(pd.Purchase_Amount) AS Average_Spent, + CASE + WHEN SUM(pd.Purchase_Amount) >= 1000 THEN 'High Value' + WHEN SUM(pd.Purchase_Amount) BETWEEN 500 AND 999 THEN 'Medium Value ' + ELSE 'Low Value' + END AS Customer_Segment + MAX(p.Purchase_Date) AS Last_Purchase_Date +FROM + Customers c +LEFT JOIN Purchases p ON c.Customer_ID = p.Customer_ID + +LEFT JOIN PurchaseDetails pd ON p.Purchase_ID = pd.Purchase_ID + +GROUP BY c.Customer_ID, c.Customer_Name, c.Email, c.Phone; + + +--view 2: InfrastructureMonitoringDashboard +CREATE VIEW InfrastructureMonitoringDashboard AS +SELECT + i.Infrastructure_ID, + i.Infrastructure_Type, + il.Location, + 1c.Capacity, + COUNT(r.Resource_ID) AS Used_Resources, + (ic.Capacity - COUNT(r.Resource_ID)) AS Available_Capacity, + CASE + WHEN (ic.Capacity - COUNT(r.Resource_ID)) <(ic.Capacity * 0.1) THEN 'Critical' + WHEN (ic.Capacity - COUNT(r.Resource_ID)) <(ic.Capacity * 0.3) THEN 'Warning' + ELSE 'Normal' + END AS Capacity_Status + AVG(r.Performance_Metric) AS Average_Performance + +FROM Infrastructure i + +JOIN Infrastructure_Location il ON i.Infrastructure_ID = il.Infrastructure_ID + +JOIN InfrastructureCapacity ic ON i.Infrastructure_ID = ic.Infrastructure_ID + +LEFT JOIN Resources r ON i.Infrastructure_ID = r.Infrastructure_ID + +GROUP BY i.Infrastructure_ID, i.Infrastructure_Type, il.Location, ic.Capacity; \ No newline at end of file diff --git a/12_database_transactions/script.sql b/12_database_transactions/script.sql index e3ee0ca..6e0311e 100644 --- a/12_database_transactions/script.sql +++ b/12_database_transactions/script.sql @@ -1 +1,23 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +START TRANSACTION; +-- Insert a new customer +INSERT INTO Customer (Customer_ID, Customer_Name, Customer_Address, Contact_Information) VALUES +(001, 'Jin woe', 'Tokyo', '011234444'); + +-- Get the ID of the newly inserted customer +SET @NewCustomerID = LAST_INSERT_ID(); + +--Insert a purchase of new customer +INSERT INTO Purchase (Purchase_ID, Customer_ID, Purchase_Date, Purchase_Amount) VALUES +(001, @NewCustomerID, '2024-11-01', 250.00); + +-- Check if the transaction was successful +IF ROW_COUNT() = 2 THEN + -- If successful, commit the transaction + COMMIT; + SELECT 'Transaction Successful'.AS Message; +ELSE + -- If not successful, rollback the transaction + ROLLBACK; + SELECT 'Transaction Failed. Changes have been rolled back'.AS Message; +END IF; \ No newline at end of file diff --git a/1_database/script.sql b/1_database/script.sql index e3ee0ca..9fbfe14 100644 --- a/1_database/script.sql +++ b/1_database/script.sql @@ -1 +1,3 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +CREATE DATABASE vodacom; +USE vodacom; \ No newline at end of file diff --git a/2_base_tables/script.sql b/2_base_tables/script.sql index e3ee0ca..42b73f9 100644 --- a/2_base_tables/script.sql +++ b/2_base_tables/script.sql @@ -1 +1,62 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +--Network Device table +CREATE TABLE Network_Device( + Device_ID INT PRIMARY KEY, + Device_Type VARCHAR(50), + IP_address VARCHAR(15) UNIQUE, + Location VARCHAR(50) +) ENGINE=INNODB; +--Server Table +CREATE TABLE Server( + Server_ID INT PRIMARY KEY, + Server_Type VARCHAR(50), + Operating_System VARCHAR(50), + Storage_Capacity INT +) ENGINE=INNODB; + +--Infrastructure Table +CREATE TABLE Infrastructure( + Infrastructure_ID INT PRIMARY KEY, + Infrastructure_Type VARCHAR(50), + Location VARCHAR(100), + Capacity INT +) ENGINE=INNODB; + +--Purchase Table +CREATE TABLE Purchase( + Purchase_ID INT PRIMARY KEY, + Customer_ID INT, + Purchase_Date DATE, + Purchase_Amount DECIMAL(10,2)'' + FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID) +) ENGINE=INNODB; + +--Sales Report Table +CREATE TABLE Sales_Report( + Sales_ID INT PRIMARY KEY, + Purchase_Date DATE, + Purchase_Amount DECIMAL(10,2), + FOREIGN KEY (Customer_ID) REFERENCES Customer(Customer_ID) +) ENGINE=MyISAM; + +--Employee Table +CREATE TABLE Employee( + Employee_ID INT PRIMARY KEY, + Employee_Name VARCHAR(50), + Department_ID VARCHAR(50), + FOREIGN KEY (Department_ID) REFERENCES Department(Department_ID) +) ENGINE=INNODB; + +--Department Table +CREATE TABLE Department( + Department_ID INT PRIMARY KEY, + Department_Name VARCHAR(100), +) ENGINE=INNODB; + +--Customer Table +CREATE TABLE Customer( + Customer_ID INT PRIMARY KEY, + Customer_Name VARCHAR(1000), + Customer_Address VARCHAR(255), + Contact_Information VARCHAR(15) +) ENGINE=INNODB; \ No newline at end of file diff --git a/3_normal_triggers/script.sql b/3_normal_triggers/script.sql index e3ee0ca..b0cadf7 100644 --- a/3_normal_triggers/script.sql +++ b/3_normal_triggers/script.sql @@ -1 +1,25 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +DELIMITER // +CREATE TRIGGER UpdateCustomerInfo +BEFORE UPDATE ON Customer +FOR EACH ROW +BEGIN + IF @is_authorized_user IS NULL OR @is_authorized_user = 0 THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'You are not authorized to update customer information'; + END IF; + +END; // +DELIMITER ; + +DELIMITER // +CREATE TRIGGER ValidatePurchaseDataBeforeInsert +BEFORE INSERT ON Purchase +FOR EACH ROW +BEGIN + IF NEW.Purchase_Date > CURDATE() THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Purchase date cannot be in the future'; + 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..dfd5e37 100644 --- a/4_check_constraints/script.sql +++ b/4_check_constraints/script.sql @@ -1 +1,9 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +ALTER TABLE Customer +ADD CONSTRAINT chk_CustomerID +CHECK (Customer_ID IS NOT NULL AND + Customer_ID NOT IN (SELECT Customer_ID FROM Customer WHERE Customer_ID IS NULL)); + +ALTER TABLE Purchase +ADD CONSTRAINT chk_PurchaseAmount +CHECK (Purchase_Amount > 0); \ No newline at end of file diff --git a/5_temporal_triggers/script.sql b/5_temporal_triggers/script.sql index e3ee0ca..1d4a6bb 100644 --- a/5_temporal_triggers/script.sql +++ b/5_temporal_triggers/script.sql @@ -1 +1,28 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +SET GLOBAL event_scheduler = ON; + +--Event for Daily Data Backup +CREATE EVENT DailyDataBackup +ON SCHEDULE EVERY 1 DAY +STARTS CURRENT_TIMESTAMP +DO +BEGIN + --Backup Product Table + INSERT INTO Customer_Backup SELECT * FROM Customer; + + --Backup Purchase Table + INSERT INTO Purchase_Backup SELECT * FROM Purchase; +END; + +--Event for Weekly Data Analysis +CREATE EVENT WeeklyDataAnalysis +ON SCHEDULE EVERY 1 WEEK +STARTS CURRENT_TIMESTAMP +DO +BEGIN + --Calculate total purchases per customer + INSERT INTO Weekly_Analysis (Customer_ID, Total_Purchases) + SELECT Customer_ID, COUNT(*) FROM Total_Purchases + FROM Purchase + GROUP BY Customer_ID; +END; \ No newline at end of file diff --git a/7_indexes/script.sql b/7_indexes/script.sql index e3ee0ca..de1c010 100644 --- a/7_indexes/script.sql +++ b/7_indexes/script.sql @@ -1 +1,5 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +CREATE INDEX idx_Device_Location ON Network_Device (Location); +CREATE INDEX idx_Server_type ON Server (Server_Type); +CREATE INDEX idx_Customer_Name ON Customer (Customer_Name); +CREATE INDEX idx_Purchase_Date ON Purchase (Purchase_Date); \ No newline at end of file diff --git a/8_dummy_data/script.sql b/8_dummy_data/script.sql index e3ee0ca..3b6102c 100644 --- a/8_dummy_data/script.sql +++ b/8_dummy_data/script.sql @@ -1 +1,16 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +INSERT INTO Network_Device (Device_ID, Device_Type, IP_Address,Location) VALUES +(1, 'Router', '192.168.1.1', 'office'); +(2, 'Switch', '192.168.1.2', 'office'); + +INSERT INTO Server (Server_ID, Server_Type, Operating_System, Storage_Capacity) VALUES +(1, 'Web Server', 'Linux', 1000); +(2, 'Database Server', 'Windows', 2000); + +INSERT INTO Customer (Customer_ID, Customer_Name, Address, Contact_Information) VALUES +(001, 'Thiery Henry','New York', '1234567890' ), +(002, 'David Beckham','Los Angeles', '0987654321'); + +INSERT INTO Purchase (Purchase_ID, Customer_ID, Purchase_Date, Purchase_Amount) VALUES +(1, 001, '2020-01-01', 150.00), +(2, 002, '2020-01-02', 200.00); \ 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..d9528cd 100644 --- a/9_stored_routines_as_procedures/script.sql +++ b/9_stored_routines_as_procedures/script.sql @@ -1 +1,65 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here + +--procedure to create Infeastructure Utilization Report +DELIMITER // + +CREATE PROCEDURE GenerateInfastructureUtilizationReport() +BEGIN + + --This procedure monitors infrastructure capacity utilization + SELECT + i.InfrastructureID, + i.InfrastructureType, + il.Location, + ic.Capacity + SUM(CASE WHEN r.Resource_ID IS NOT NULL THEN 1 ELSE 0 END) AS Used_Resources, + (ic.Capacity - SUM(CASE WHEN r.Resource_ID IS NOT NULL THEN 1 ELSE 0 END)) AS Available_Capacity, + CASE + WHEN(ic.Capacity - SUM(CASE WHEN r.Resource_ID IS NOT NULL THEN 1 ELSE 0 END)) < (ic.Capacity *0.1) THEN 'Critical' + WHEN(ic.Capacity - SUM(CASE WHEN r.Resource_ID IS NOT NULL THEN 1 ELSE 0 END)) < (ic.Capacity *0.3) THEN 'Warning' + ELSE 'Normal' + END AS Capacity_Status + FROM + Infrastructure i + JOIN Infrastructure_Location il ON i.InfrastructureID = il.InfrastructureID + + JOIN Infrastructure_Capacity ic ON i.InfrastructureID = ic.InfrastructureID + + LEFT JOIN Resource r ON i.InfrastructureID = r.InfrastructureID + + GROUP BY i.InfrastructureID, i.Infrastructure_Type, ic.Capacity; +END // + +DELIMITER; + +--procedure to Generate Customer Analytics Report +DELIMITER // + +CREATE PROCEDURE GenerateCustomerAnalyticsReport() +BEGIN + -- This procedure analyzes the customer purchasing behavior and segments them based on their total purchase amount + + SELECT + c.CustomerID, + c.CustomerName, + c.CustomerType, + COUNT(p.Purchase_ID) AS Total_Purchase, + SUM(pd.Purchase_Amount) AS Total_Spent, + AVG(pd.Purchase_Amount) AS AVG_Spent + CASE + WHEN SUM(pd.Purchase_Amount) > 10000 THEN 'High Value' + WHEN SUM(pd.Purchase_Amount) BETWEEN 500 AND 999 THEN 'Medium Value' + ELSE 'Low Value' + END AS Customer_Segement + FROM + Customer c + LEFT JOIN Purchase p ON c.CustomerID = p.CustomerID + + LEFT JOIN Purchase_Details pd ON p.Purchase_ID = pd.Purchase_ID + + GROUP BY c.CustomerID, c.CustomerName + + ORDER BY Total_Spent DESC; +END // + +DELIMITER; \ No newline at end of file diff --git a/README.md b/README.md index 8728c63..74c141e 100644 --- a/README.md +++ b/README.md @@ -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 |