From 80a8b3f008387d525afe9ce30add015d19ea6582 Mon Sep 17 00:00:00 2001 From: Yaroslava Date: Tue, 23 Sep 2025 13:05:21 +0300 Subject: [PATCH 1/2] homework --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 19 +++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/rda_task_4_database_normalization.iml | 8 ++ .idea/vcs.xml | 6 ++ task.sql | 76 +++++++++++++------ 8 files changed, 110 insertions(+), 23 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/rda_task_4_database_normalization.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..eaf91e2 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..4e58d99 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cf5c904 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..d4d7d12 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/rda_task_4_database_normalization.iml b/.idea/rda_task_4_database_normalization.iml new file mode 100644 index 0000000..d9e6024 --- /dev/null +++ b/.idea/rda_task_4_database_normalization.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/task.sql b/task.sql index cc65344..a386870 100644 --- a/task.sql +++ b/task.sql @@ -1,33 +1,63 @@ --- Create database and tables +-- Drop the existing database if it exists +DROP DATABASE IF EXISTS ShopDB; +-- Create a new database CREATE DATABASE ShopDB; + +-- Use the new database USE ShopDB; +-- Create the normalized tables +-- Table for countries (already normalized) CREATE TABLE Countries ( - ID INT, - Name VARCHAR(50), - PRIMARY KEY (ID) + ID INT AUTO_INCREMENT PRIMARY KEY, + Name VARCHAR(50) NOT NULL +); + +-- Normalized Products table to hold product details +CREATE TABLE Products ( + ID INT AUTO_INCREMENT PRIMARY KEY, + Name VARCHAR(100) NOT NULL ); -CREATE TABLE ProductInventory ( - ID INT, - ProductName VARCHAR(50), - WarehouseAmount INT, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), +-- Normalized Warehouses table to remove transitive dependencies +-- This table stores all warehouse information in one place +CREATE TABLE Warehouses ( + ID INT AUTO_INCREMENT PRIMARY KEY, + Name VARCHAR(100) NOT NULL, + Address VARCHAR(255) NOT NULL, CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, - PRIMARY KEY (ID) + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE SET NULL +); + +-- Normalized ProductInventories table to link products and warehouses +-- This is a join table that resolves a many-to-many relationship and stores the quantity +CREATE TABLE ProductInventories ( + ProductID INT NOT NULL, + WarehouseID INT NOT NULL, + WarehouseAmount INT NOT NULL, + PRIMARY KEY (ProductID, WarehouseID), + FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE CASCADE, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE CASCADE ); --- 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); +-- Populate the normalized tables with test data +-- Insert countries +INSERT INTO Countries (Name) VALUES +('Ukraine'), +('USA'); + +-- Insert products +INSERT INTO Products (Name) VALUES +('AwesomeProduct'), +('CoolProduct'); + +-- Insert warehouses +INSERT INTO Warehouses (Name, Address, CountryID) VALUES +('Warehouse-1', 'City-1, Street-1', 1), +('Warehouse-2', 'City-2, Street-2', 2); + +-- Insert product inventories +INSERT INTO ProductInventories (ProductID, WarehouseID, WarehouseAmount) VALUES +(1, 1, 2), +(1, 2, 5); From 3632216ee33ee765cfc8476d0b2c6c831dedf867 Mon Sep 17 00:00:00 2001 From: Yaroslava Date: Tue, 23 Sep 2025 13:11:07 +0300 Subject: [PATCH 2/2] homework --- task.sql | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/task.sql b/task.sql index a386870..3e466cd 100644 --- a/task.sql +++ b/task.sql @@ -60,4 +60,6 @@ INSERT INTO Warehouses (Name, Address, CountryID) VALUES -- Insert product inventories INSERT INTO ProductInventories (ProductID, WarehouseID, WarehouseAmount) VALUES (1, 1, 2), -(1, 2, 5); +(1, 2, 5), +(2, 1, 3), +(2, 2, 7); \ No newline at end of file