From e62194f3b8a5f36c6167177e3313eecab3a63fdb Mon Sep 17 00:00:00 2001 From: ll221 Date: Tue, 16 Jun 2026 14:48:06 +0300 Subject: [PATCH 1/4] 1 --- task.sql | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/task.sql b/task.sql index cc65344..69e794b 100644 --- a/task.sql +++ b/task.sql @@ -1,33 +1,44 @@ --- Create database and tables - +-- Create database CREATE DATABASE ShopDB; USE ShopDB; +-- Table 1: Countries CREATE TABLE Countries ( ID INT, Name VARCHAR(50), PRIMARY KEY (ID) ); +-- Table 2: Warehouses (винесено окремо — 3НФ) +CREATE TABLE Warehouses ( + ID INT PRIMARY KEY, + WarehouseName VARCHAR(50), + WarehouseAddress VARCHAR(50) +); + +-- Table 3: ProductInventory (посилається на Countries та Warehouses) 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, + WarehouseAmount INT, + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION ); --- Populate test data +-- Populate: Countries +INSERT INTO Countries (ID, Name) VALUES (1, 'Country1'); +INSERT INTO Countries (ID, Name) VALUES (2, 'Country2'); + +-- Populate: Warehouses +INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress) + VALUES (1, 'Warehouse-1', 'City-1, Street-1'); +INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress) + VALUES (2, 'Warehouse-2', 'City-2, Street-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); +-- Populate: ProductInventory +INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID, CountryID) + VALUES (1, 'AwesomeProduct', 2, 1, 1); +INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID, CountryID) + VALUES (2, 'AwesomeProduct', 5, 2, 2); \ No newline at end of file From 0a6d71bd3fb26b6dc0bde114a746d9ecc44196e4 Mon Sep 17 00:00:00 2001 From: ll221 Date: Tue, 16 Jun 2026 14:51:15 +0300 Subject: [PATCH 2/4] 2 --- task.sql | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/task.sql b/task.sql index 69e794b..236c504 100644 --- a/task.sql +++ b/task.sql @@ -1,29 +1,29 @@ --- Create database +-- Drop and recreate database +DROP DATABASE IF EXISTS ShopDB; CREATE DATABASE ShopDB; USE ShopDB; -- Table 1: Countries CREATE TABLE Countries ( - ID INT, - Name VARCHAR(50), - PRIMARY KEY (ID) + ID INT PRIMARY KEY, + Name VARCHAR(50) ); --- Table 2: Warehouses (винесено окремо — 3НФ) +-- Table 2: Warehouses (містить CountryID — звідки товар) CREATE TABLE Warehouses ( ID INT PRIMARY KEY, WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50) + WarehouseAddress VARCHAR(50), + CountryID INT, + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION ); --- Table 3: ProductInventory (посилається на Countries та Warehouses) +-- Table 3: ProductInventory (посилається на Warehouses, без CountryID) CREATE TABLE ProductInventory ( ID INT PRIMARY KEY, ProductName VARCHAR(50), - CountryID INT, - WarehouseID INT, WarehouseAmount INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, + WarehouseID INT, FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION ); @@ -32,13 +32,13 @@ INSERT INTO Countries (ID, Name) VALUES (1, 'Country1'); INSERT INTO Countries (ID, Name) VALUES (2, 'Country2'); -- Populate: Warehouses -INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress) - VALUES (1, 'Warehouse-1', 'City-1, Street-1'); -INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress) - VALUES (2, 'Warehouse-2', 'City-2, Street-2'); +INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress, CountryID) + VALUES (1, 'Warehouse-1', 'City-1, Street-1', 1); +INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress, CountryID) + VALUES (2, 'Warehouse-2', 'City-2, Street-2', 2); -- Populate: ProductInventory -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID, CountryID) - VALUES (1, 'AwesomeProduct', 2, 1, 1); -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID, CountryID) - VALUES (2, 'AwesomeProduct', 5, 2, 2); \ No newline at end of file +INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) + VALUES (1, 'AwesomeProduct', 2, 1); +INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) + VALUES (2, 'AwesomeProduct', 5, 2); \ No newline at end of file From 5909a8138ecbf5f2f27b5a1197b9efead1811c08 Mon Sep 17 00:00:00 2001 From: ll221 Date: Tue, 16 Jun 2026 14:53:20 +0300 Subject: [PATCH 3/4] 3 --- task.sql | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/task.sql b/task.sql index 236c504..88d16c0 100644 --- a/task.sql +++ b/task.sql @@ -5,25 +5,25 @@ USE ShopDB; -- Table 1: Countries CREATE TABLE Countries ( - ID INT PRIMARY KEY, - Name VARCHAR(50) + ID INT NOT NULL PRIMARY KEY, + Name VARCHAR(50) NOT NULL ); --- Table 2: Warehouses (містить CountryID — звідки товар) +-- Table 2: Warehouses (містить CountryID — склад належить країні) CREATE TABLE Warehouses ( - ID INT PRIMARY KEY, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), - CountryID INT, + ID INT NOT NULL PRIMARY KEY, + WarehouseName VARCHAR(50) NOT NULL, + WarehouseAddress VARCHAR(50) NOT NULL, + CountryID INT NOT NULL, FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION ); -- Table 3: ProductInventory (посилається на Warehouses, без CountryID) CREATE TABLE ProductInventory ( - ID INT PRIMARY KEY, - ProductName VARCHAR(50), - WarehouseAmount INT, - WarehouseID INT, + ID INT NOT NULL PRIMARY KEY, + ProductName VARCHAR(50) NOT NULL, + WarehouseAmount INT NOT NULL, + WarehouseID INT NOT NULL, FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION ); From b5c8ea5312bfd78c3ea9cd75eba69f54ce225e6c Mon Sep 17 00:00:00 2001 From: ll221 Date: Tue, 16 Jun 2026 14:55:17 +0300 Subject: [PATCH 4/4] 4 --- task.sql | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/task.sql b/task.sql index 88d16c0..0e2d545 100644 --- a/task.sql +++ b/task.sql @@ -9,7 +9,7 @@ CREATE TABLE Countries ( Name VARCHAR(50) NOT NULL ); --- Table 2: Warehouses (містить CountryID — склад належить країні) +-- Table 2: Warehouses (belongs to a Country) CREATE TABLE Warehouses ( ID INT NOT NULL PRIMARY KEY, WarehouseName VARCHAR(50) NOT NULL, @@ -18,12 +18,19 @@ CREATE TABLE Warehouses ( FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION ); --- Table 3: ProductInventory (посилається на Warehouses, без CountryID) +-- Table 3: Products (new — extracted from ProductInventory) +CREATE TABLE Products ( + ID INT NOT NULL PRIMARY KEY, + ProductName VARCHAR(50) NOT NULL +); + +-- Table 4: ProductInventory (ID, ProductID, WarehouseAmount, WarehouseID — exactly 4 columns, 2 FKs) CREATE TABLE ProductInventory ( ID INT NOT NULL PRIMARY KEY, - ProductName VARCHAR(50) NOT NULL, + ProductID INT NOT NULL, WarehouseAmount INT NOT NULL, WarehouseID INT NOT NULL, + FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE NO ACTION, FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION ); @@ -37,8 +44,11 @@ INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress, CountryID) INSERT INTO Warehouses (ID, WarehouseName, WarehouseAddress, CountryID) VALUES (2, 'Warehouse-2', 'City-2, Street-2', 2); +-- Populate: Products +INSERT INTO Products (ID, ProductName) VALUES (1, 'AwesomeProduct'); + -- Populate: ProductInventory -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) - VALUES (1, 'AwesomeProduct', 2, 1); -INSERT INTO ProductInventory (ID, ProductName, WarehouseAmount, WarehouseID) - VALUES (2, 'AwesomeProduct', 5, 2); \ No newline at end of file +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