From c8db0ed801fd13eb3821e3e99b7f62c0b85456cc Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 12:22:21 +0000 Subject: [PATCH 01/16] Setting up GitHub Classroom Feedback From 2e6d61bbac8646bdcf0043fae5c80de62e5ce754 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 30 Sep 2024 12:22:24 +0000 Subject: [PATCH 02/16] add deadline --- README.md | 1 + 1 file changed, 1 insertion(+) 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 | From a3555b1da129d4c2a2f835c9a82cf01a83c12225 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 18:18:49 +0300 Subject: [PATCH 03/16] create database vodacom in script.sql --- 1_database/script.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1_database/script.sql b/1_database/script.sql index e3ee0ca..b09049b 100644 --- a/1_database/script.sql +++ b/1_database/script.sql @@ -1 +1,2 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +CREATE DATABASE vodacom; \ No newline at end of file From db08dc828d473f6dfb726ca7e7a0c05a1e55cd99 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 18:37:27 +0300 Subject: [PATCH 04/16] add SQL scripts for database tables including Network_Device, Server, Infrastructure, Purchase, Sales_Report, Employee, Department, and Customer --- 2_base_tables/script.sql | 63 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/2_base_tables/script.sql b/2_base_tables/script.sql index e3ee0ca..d556c6d 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, + Device_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 From 53c3593e24a7938bc70f6cf78695fc228792b89a Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 18:51:41 +0300 Subject: [PATCH 05/16] add SQL triggers for customer info update and purchase data validation --- 3_normal_triggers/script.sql | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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 From 6586fc510d3374b80118eafbf5b1bebde080a805 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 19:00:39 +0300 Subject: [PATCH 06/16] add SQL triggers to validate Customer ID and Purchase Amount --- 4_check_constraints/script.sql | 63 +++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/4_check_constraints/script.sql b/4_check_constraints/script.sql index e3ee0ca..f662fce 100644 --- a/4_check_constraints/script.sql +++ b/4_check_constraints/script.sql @@ -1 +1,62 @@ --- Write your SQL code here \ No newline at end of file +-- Write your SQL code here +DELIMITER $$ + +--Trigger to validate Customer ID +CREATE TRIGGER ValidateCustomerIDBeforeInsert +BEFORE INSERT ON Customer +FOR EACH ROW +BEGIN + --Check if Customer ID is NULL + IF NEW.Customer_ID IS NULL THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Customer ID cannot be NULL'; + END IF; + --Check if Customer ID already exists + IF EXISTS (SELECT 1 FROM Customer WHERE Customer_ID = NEW.Customer_ID) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Customer ID already exists'; + END IF; +END; $$ + +CREATE TRIGGER ValidateCustomerIDBeforeUpdate +BEFORE UPDATE ON Customer +FOR EACH ROW +BEGIN + + --Check if Customer ID is NULL + IF NEW.Customer_ID IS NULL THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Customer ID cannot be NULL'; + END IF; + + --Check if Customer ID already exists + IF EXISTS (SELECT 1 FROM Customer WHERE Customer_ID = NEW.Customer_ID) THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Customer ID already exists'; + END IF; +END; $$ + +--Trigger to validate Purchase Amount +CREATE TRIGGER ValidatePurchaseAmountBeforeInsert +BEFORE INSERT ON Purchase +FOR EACH ROW +BEGIN + --Check if Purchase Amount is less than zero + IF NEW.Purchase_Amount < 0 THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Purchase Amount must be graeter than zero'; + END IF; +END; $$ + +CREATE TRIGGER ValidatePurchaseAmountBeforeUpdate +BEFORE UPDATE ON Purchase +FOR EACH ROW +BEGIN + --Check if Purchase Amount is greater than zero + IF NEW.Purchase_Amount < 0 THEN + SIGNAL SQLSTATE '45000' + SET MESSAGE_TEXT = 'Purchase Amount must be greater than zero'; + END IF; +END; $$ + +DELIMITER ; \ No newline at end of file From b5bd4f6d180da4547603b572089323577d55bb1f Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 19:07:03 +0300 Subject: [PATCH 07/16] add SQL events for daily data backup and weekly data analysis --- 5_temporal_triggers/script.sql | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) 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 From 9725bd1495d8ecd71d72a6f7e6b20cd60e1cfa13 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 19:09:07 +0300 Subject: [PATCH 08/16] rename Device_Location column to Location in Network_Device table --- 2_base_tables/script.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2_base_tables/script.sql b/2_base_tables/script.sql index d556c6d..42b73f9 100644 --- a/2_base_tables/script.sql +++ b/2_base_tables/script.sql @@ -4,7 +4,7 @@ CREATE TABLE Network_Device( Device_ID INT PRIMARY KEY, Device_Type VARCHAR(50), IP_address VARCHAR(15) UNIQUE, - Device_Location VARCHAR(50) + Location VARCHAR(50) ) ENGINE=INNODB; --Server Table CREATE TABLE Server( From 36ee01fe8d8d2fbe44398e72c05b37531758ce0f Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 19:15:49 +0300 Subject: [PATCH 09/16] add SQL indexes for improved query performance on Network_Device, Server, Customer, and Purchase tables --- 7_indexes/script.sql | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 31c98abcb2a80a7c27989d87b3232c445d300ec7 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 19:40:08 +0300 Subject: [PATCH 10/16] add dummy data for Network_Device, Server, Customer, and Purchase tables --- 8_dummy_data/script.sql | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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 From 676f901e8ad963167a74610494788df382238aba Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 20:27:40 +0300 Subject: [PATCH 11/16] add stored procedures for Infrastructure Utilization and Customer Analytics reports --- 9_stored_routines_as_procedures/script.sql | 66 +++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) 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 From c40c4d3aece8ca62f49928ea3b1aa2311130e75e Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 21:19:54 +0300 Subject: [PATCH 12/16] add functions to calculate customer lifetime value and infrastructure efficiency --- 10_stored_routines_as_functions/script.sql | 65 +++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) 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 From acafb683b82479b5d81795837846276f53c9dee7 Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 21:37:48 +0300 Subject: [PATCH 13/16] add views for Customer Insight and Infrastructure Monitoring dashboards --- 11_views/script.sql | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) 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 From a823341a72e2f89d8522a0a4f03cb8d269984b4e Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 21:39:33 +0300 Subject: [PATCH 14/16] add database creation and selection for vodacom --- 1_database/script.sql | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1_database/script.sql b/1_database/script.sql index b09049b..9fbfe14 100644 --- a/1_database/script.sql +++ b/1_database/script.sql @@ -1,2 +1,3 @@ -- Write your SQL code here -CREATE DATABASE vodacom; \ No newline at end of file +CREATE DATABASE vodacom; +USE vodacom; \ No newline at end of file From 4fb7b1a90c469f07e29d90effe8c24b2cffa271d Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 21:59:04 +0300 Subject: [PATCH 15/16] add transaction handling for customer insertion and purchase recording --- 12_database_transactions/script.sql | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 From 55d759e3ea77754ab2887d84385ab47376935caf Mon Sep 17 00:00:00 2001 From: Fluffycoal Date: Thu, 21 Nov 2024 22:08:58 +0300 Subject: [PATCH 16/16] refactor constraints for Customer and Purchase tables using CHECK constraints --- 4_check_constraints/script.sql | 69 ++++------------------------------ 1 file changed, 8 insertions(+), 61 deletions(-) diff --git a/4_check_constraints/script.sql b/4_check_constraints/script.sql index f662fce..dfd5e37 100644 --- a/4_check_constraints/script.sql +++ b/4_check_constraints/script.sql @@ -1,62 +1,9 @@ -- Write your SQL code here -DELIMITER $$ - ---Trigger to validate Customer ID -CREATE TRIGGER ValidateCustomerIDBeforeInsert -BEFORE INSERT ON Customer -FOR EACH ROW -BEGIN - --Check if Customer ID is NULL - IF NEW.Customer_ID IS NULL THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Customer ID cannot be NULL'; - END IF; - --Check if Customer ID already exists - IF EXISTS (SELECT 1 FROM Customer WHERE Customer_ID = NEW.Customer_ID) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Customer ID already exists'; - END IF; -END; $$ - -CREATE TRIGGER ValidateCustomerIDBeforeUpdate -BEFORE UPDATE ON Customer -FOR EACH ROW -BEGIN - - --Check if Customer ID is NULL - IF NEW.Customer_ID IS NULL THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Customer ID cannot be NULL'; - END IF; - - --Check if Customer ID already exists - IF EXISTS (SELECT 1 FROM Customer WHERE Customer_ID = NEW.Customer_ID) THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Customer ID already exists'; - END IF; -END; $$ - ---Trigger to validate Purchase Amount -CREATE TRIGGER ValidatePurchaseAmountBeforeInsert -BEFORE INSERT ON Purchase -FOR EACH ROW -BEGIN - --Check if Purchase Amount is less than zero - IF NEW.Purchase_Amount < 0 THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Purchase Amount must be graeter than zero'; - END IF; -END; $$ - -CREATE TRIGGER ValidatePurchaseAmountBeforeUpdate -BEFORE UPDATE ON Purchase -FOR EACH ROW -BEGIN - --Check if Purchase Amount is greater than zero - IF NEW.Purchase_Amount < 0 THEN - SIGNAL SQLSTATE '45000' - SET MESSAGE_TEXT = 'Purchase Amount must be greater than zero'; - END IF; -END; $$ - -DELIMITER ; \ No newline at end of file +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