From 0e2d7a26ef579c283c8c04651deb8167139122cd Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Mon, 28 Oct 2024 19:04:38 +0200 Subject: [PATCH 1/4] normalize database --- task.sql | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/task.sql b/task.sql index cc65344..c71c21a 100644 --- a/task.sql +++ b/task.sql @@ -4,30 +4,33 @@ CREATE DATABASE ShopDB; USE ShopDB; CREATE TABLE Countries ( - ID INT, - Name VARCHAR(50), - PRIMARY KEY (ID) + ID INT PRIMARY KEY, + Name VARCHAR(50) +); + +CREATE TABLE Warehouses ( + WarehouseID INT PRIMARY KEY, + WarehouseName VARCHAR(50), + WarehouseAddress VARCHAR(50), + CountryID INT, + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ); CREATE TABLE ProductInventory ( - ID INT, + ID INT PRIMARY KEY, ProductName VARCHAR(50), WarehouseAmount INT, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), - CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, - PRIMARY KEY (ID) + WarehouseID INT, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ); -- Populate test data +INSERT INTO Warehouses (WarehouseID, WarehouseName, WarehouseAddress, CountryID) + VALUES (1, 'Warehouse-1', 'City-1, Street-1', 1); +INSERT INTO Warehouses (WarehouseID, WarehouseName, WarehouseAddress, CountryID) + VALUES (2, 'Warehouse-2', 'City-2, Street-2', 2); -INSERT INTO Countries (ID,Name) - VALUES (1, 'Country1'); -INSERT INTO Countries (ID,Name) - VALUES (2, 'Country2'); - -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, ProductName, WarehouseAmount, WarehouseID) + VALUES (1, 'AwesomeProduct', 2, 1); +INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) + VALUES (2, 'AwesomeProduct', 5, 2); From 0f53be604702b24dd28c6c2cf1fd26df8b8a9546 Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Mon, 28 Oct 2024 19:45:06 +0200 Subject: [PATCH 2/4] change reference id --- task.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task.sql b/task.sql index c71c21a..8f97cce 100644 --- a/task.sql +++ b/task.sql @@ -21,7 +21,7 @@ CREATE TABLE ProductInventory ( ProductName VARCHAR(50), WarehouseAmount INT, WarehouseID INT, - FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID) ); -- Populate test data From 7e37247fa2e41d36a13b866bd6217a2747ab9b4c Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Mon, 28 Oct 2024 19:51:32 +0200 Subject: [PATCH 3/4] change database structure --- task.sql | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/task.sql b/task.sql index 8f97cce..b472d8b 100644 --- a/task.sql +++ b/task.sql @@ -16,21 +16,31 @@ CREATE TABLE Warehouses ( FOREIGN KEY (CountryID) REFERENCES Countries(ID) ); +CREATE TABLE Products ( + ProductID INT PRIMARY KEY, + ProductName VARCHAR(50) UNIQUE +); + CREATE TABLE ProductInventory ( ID INT PRIMARY KEY, - ProductName VARCHAR(50), - WarehouseAmount INT, + ProductID INT, WarehouseID INT, - FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID) + WarehouseAmount INT, + FOREIGN KEY (ProductID) REFERENCES Products(ProductID), + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID), + UNIQUE (ProductID, WarehouseID) ); -- Populate test data +INSERT INTO Countries (ID, Name) + VALUES (1, 'Country1'), (2, 'Country2'); + INSERT INTO Warehouses (WarehouseID, WarehouseName, WarehouseAddress, CountryID) - VALUES (1, 'Warehouse-1', 'City-1, Street-1', 1); -INSERT INTO Warehouses (WarehouseID, WarehouseName, WarehouseAddress, CountryID) - VALUES (2, 'Warehouse-2', 'City-2, Street-2', 2); + VALUES (1, 'Warehouse-1', 'City-1, Street-1', 1), + (2, 'Warehouse-2', 'City-2, Street-2', 2); + +INSERT INTO Products (ProductID, ProductName) + VALUES (1, 'AwesomeProduct'); -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) - VALUES (1, 'AwesomeProduct', 2, 1); -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) - VALUES (2, 'AwesomeProduct', 5, 2); +INSERT INTO ProductInventory (ID, ProductID, WarehouseAmount, WarehouseID) + VALUES (1, 1, 2, 1), (2, 1, 5, 2); From 9e85a8f7a307deafb8fb5956ae7f2f17db867305 Mon Sep 17 00:00:00 2001 From: Nazar Makivchuk Date: Tue, 29 Oct 2024 15:04:22 +0200 Subject: [PATCH 4/4] updated as per notes --- task.sql | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/task.sql b/task.sql index b472d8b..fe09ee6 100644 --- a/task.sql +++ b/task.sql @@ -13,12 +13,12 @@ CREATE TABLE Warehouses ( WarehouseName VARCHAR(50), WarehouseAddress VARCHAR(50), CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION ); CREATE TABLE Products ( ProductID INT PRIMARY KEY, - ProductName VARCHAR(50) UNIQUE + ProductName VARCHAR(50) ); CREATE TABLE ProductInventory ( @@ -26,9 +26,8 @@ CREATE TABLE ProductInventory ( ProductID INT, WarehouseID INT, WarehouseAmount INT, - FOREIGN KEY (ProductID) REFERENCES Products(ProductID), - FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID), - UNIQUE (ProductID, WarehouseID) + FOREIGN KEY (ProductID) REFERENCES Products(ProductID) ON DELETE NO ACTION, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID) ON DELETE NO ACTION ); -- Populate test data