Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
c8db0ed
Setting up GitHub Classroom Feedback
github-classroom[bot] Sep 30, 2024
2e6d61b
add deadline
github-classroom[bot] Sep 30, 2024
a3555b1
create database vodacom in script.sql
Fluffycoal Nov 21, 2024
db08dc8
add SQL scripts for database tables including Network_Device, Server,…
Fluffycoal Nov 21, 2024
53c3593
add SQL triggers for customer info update and purchase data validation
Fluffycoal Nov 21, 2024
6586fc5
add SQL triggers to validate Customer ID and Purchase Amount
Fluffycoal Nov 21, 2024
b5bd4f6
add SQL events for daily data backup and weekly data analysis
Fluffycoal Nov 21, 2024
9725bd1
rename Device_Location column to Location in Network_Device table
Fluffycoal Nov 21, 2024
36ee01f
add SQL indexes for improved query performance on Network_Device, Ser…
Fluffycoal Nov 21, 2024
31c98ab
add dummy data for Network_Device, Server, Customer, and Purchase tables
Fluffycoal Nov 21, 2024
676f901
add stored procedures for Infrastructure Utilization and Customer Ana…
Fluffycoal Nov 21, 2024
c40c4d3
add functions to calculate customer lifetime value and infrastructure…
Fluffycoal Nov 21, 2024
acafb68
add views for Customer Insight and Infrastructure Monitoring dashboards
Fluffycoal Nov 21, 2024
a823341
add database creation and selection for vodacom
Fluffycoal Nov 21, 2024
4fb7b1a
add transaction handling for customer insertion and purchase recording
Fluffycoal Nov 21, 2024
55d759e
refactor constraints for Customer and Purchase tables using CHECK con…
Fluffycoal Nov 21, 2024
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
65 changes: 64 additions & 1 deletion 10_stored_routines_as_functions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
-- Write your SQL code here
-- 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;
52 changes: 51 additions & 1 deletion 11_views/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
-- Write your SQL code here
-- 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;
24 changes: 23 additions & 1 deletion 12_database_transactions/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
-- Write your SQL code here
-- 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;
4 changes: 3 additions & 1 deletion 1_database/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
-- Write your SQL code here
-- Write your SQL code here
CREATE DATABASE vodacom;
USE vodacom;
63 changes: 62 additions & 1 deletion 2_base_tables/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
-- Write your SQL code here
-- 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;
26 changes: 25 additions & 1 deletion 3_normal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
-- Write your SQL code here
-- 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 ;
10 changes: 9 additions & 1 deletion 4_check_constraints/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
-- Write your SQL code here
-- 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);
29 changes: 28 additions & 1 deletion 5_temporal_triggers/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
-- Write your SQL code here
-- 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;
6 changes: 5 additions & 1 deletion 7_indexes/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
-- Write your SQL code here
-- 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);
17 changes: 16 additions & 1 deletion 8_dummy_data/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
-- Write your SQL code here
-- 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);
66 changes: 65 additions & 1 deletion 9_stored_routines_as_procedures/script.sql
Original file line number Diff line number Diff line change
@@ -1 +1,65 @@
-- Write your SQL code here
-- 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;
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