From f59f12b3161640b9a1de1db9e38ea6185800d7b3 Mon Sep 17 00:00:00 2001 From: NazarKulyk6 Date: Thu, 19 Mar 2026 10:38:47 +0100 Subject: [PATCH 1/2] Normalize ShopDB to 3NF: extract Products and Warehouses tables --- task.sql | 59 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/task.sql b/task.sql index cc65344..1d0fb36 100644 --- a/task.sql +++ b/task.sql @@ -3,31 +3,54 @@ CREATE DATABASE ShopDB; USE ShopDB; +-- Table 1: Countries (unchanged) CREATE TABLE Countries ( - ID INT, - Name VARCHAR(50), + ID INT NOT NULL, + Name VARCHAR(50) NOT NULL, PRIMARY KEY (ID) ); -CREATE TABLE ProductInventory ( - ID INT, - ProductName VARCHAR(50), - WarehouseAmount INT, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), +-- Table 2: Products (extracted from ProductInventory - 2NF/3NF: ProductName is not dependent on warehouse) +CREATE TABLE Products ( + ID INT NOT NULL, + Name VARCHAR(50) NOT NULL, + PRIMARY KEY (ID) +); + +-- Table 3: Warehouses (extracted from ProductInventory - 3NF: warehouse info is not dependent on product) +CREATE TABLE Warehouses ( + ID INT NOT NULL, + Name VARCHAR(50) NOT NULL, + Address VARCHAR(50) NOT NULL, CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, + PRIMARY KEY (ID) +); + +-- Table 4: ProductInventory (normalized - only 4 columns: ID, ProductID, WarehouseAmount, WarehouseID) +CREATE TABLE ProductInventory ( + ID INT NOT NULL, + ProductID INT, + WarehouseAmount INT NOT NULL, + WarehouseID INT, + FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE NO ACTION, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION, PRIMARY KEY (ID) ); -- Populate test data -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 Countries (ID, Name) VALUES (1, 'Country1'); +INSERT INTO Countries (ID, Name) VALUES (2, 'Country2'); + +INSERT INTO Products (ID, Name) VALUES (1, 'AwersomeProduct'); + +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 ProductInventory (ID, ProductID, WarehouseAmount, WarehouseID) + VALUES (1, 1, 2, 1); +INSERT INTO ProductInventory (ID, ProductID, WarehouseAmount, WarehouseID) + VALUES (2, 1, 5, 2); From 61033595494a4290b478bb7e5f860561fc39496a Mon Sep 17 00:00:00 2001 From: NazarKulyk6 Date: Thu, 19 Mar 2026 10:49:25 +0100 Subject: [PATCH 2/2] Fix: add DROP DATABASE IF EXISTS, set ProductID and WarehouseID as NOT NULL --- task.sql | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/task.sql b/task.sql index 1d0fb36..967aac2 100644 --- a/task.sql +++ b/task.sql @@ -1,5 +1,6 @@ -- Create database and tables +DROP DATABASE IF EXISTS ShopDB; CREATE DATABASE ShopDB; USE ShopDB; @@ -30,9 +31,9 @@ CREATE TABLE Warehouses ( -- Table 4: ProductInventory (normalized - only 4 columns: ID, ProductID, WarehouseAmount, WarehouseID) CREATE TABLE ProductInventory ( ID INT NOT NULL, - ProductID INT, + ProductID INT NOT NULL, WarehouseAmount INT NOT NULL, - WarehouseID INT, + WarehouseID INT NOT NULL, FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE NO ACTION, FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION, PRIMARY KEY (ID)