From aaa4413d48d9d642fc2774190883e16c733cf7db Mon Sep 17 00:00:00 2001 From: maxmlv Date: Mon, 22 Jun 2026 20:21:49 +0300 Subject: [PATCH 1/3] implement database normalization --- task.sql | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/task.sql b/task.sql index cc65344..7173dbb 100644 --- a/task.sql +++ b/task.sql @@ -9,14 +9,28 @@ CREATE TABLE Countries ( PRIMARY KEY (ID) ); +CREATE TABLE Warehouses ( + ID INT, + Name VARCHAR(50), + Address VARCHAR(50), + CountryID INT, + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE RESTRICT, + PRIMARY KEY (ID) +); + +CREATE TABLE Products ( + ID INT, + Name VARCHAR(50), + PRIMARY KEY (ID) +); + CREATE TABLE ProductInventory ( ID INT, - ProductName VARCHAR(50), + ProductID INT, + FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE CASCADE, WarehouseAmount INT, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), - CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, + WarehouseID INT, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE CASCADE, PRIMARY KEY (ID) ); @@ -26,8 +40,16 @@ INSERT INTO Countries (ID,Name) VALUES (1, 'Country1'); INSERT INTO Countries (ID,Name) VALUES (2, 'Country2'); + +INSERT INTO Warehouses (ID,Name,Address,CountryID) + VALUES (1, 'Warehouse-1', 'City-1, Street-1', 1); +INSERT INTO Warehouses (ID,Name,Address,CountryID) + VALUES (2, 'Warehouse-2', 'City-2, Street-2', 2); + +INSERT INTO Products (ID,Name) + VALUES (1, 'AwesomeProduct'); -INSERT INTO ProductInventory (ID,ProductName,WarehouseAmount,WarehouseName,WarehouseAddress,CountryID) - VALUES (1, 'AwersomeProduct', 2, 'Warehouse-1', 'City-1, Street-1',1); -INSERT INTO ProductInventory (ID,ProductName,WarehouseAmount,WarehouseName,WarehouseAddress,CountryID) - VALUES (2, 'AwersomeProduct', 5, 'Warehouse-2', 'City-2, Street-2',2); +INSERT INTO ProductInventory (ID,ProductID,WarehouseAmount,WarehouseID) + VALUES (1, 1, 2, 1); +INSERT INTO ProductInventory (ID,ProductID,WarehouseAmount,WarehouseID) + VALUES (2, 1, 5, 2); \ No newline at end of file From 26e5a84bac0b55cae5b7efff0dd59a382cfe8b91 Mon Sep 17 00:00:00 2001 From: maxmlv Date: Mon, 22 Jun 2026 20:27:41 +0300 Subject: [PATCH 2/3] fix: delete database before create --- task.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/task.sql b/task.sql index 7173dbb..626ee8c 100644 --- a/task.sql +++ b/task.sql @@ -1,3 +1,7 @@ +-- Delete ShopDB if it exists + +DROP DATABASE ShopDB; + -- Create database and tables CREATE DATABASE ShopDB; From e7fc1068252907c4d311d7f06dac2c6738271f54 Mon Sep 17 00:00:00 2001 From: maxmlv Date: Mon, 22 Jun 2026 20:32:20 +0300 Subject: [PATCH 3/3] fix: delete database if exists --- task.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task.sql b/task.sql index 626ee8c..8e6c2ac 100644 --- a/task.sql +++ b/task.sql @@ -1,6 +1,6 @@ -- Delete ShopDB if it exists -DROP DATABASE ShopDB; +DROP DATABASE IF EXISTS ShopDB; -- Create database and tables