Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/rda_task_4_database_normalization.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 55 additions & 23 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,65 @@
-- 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),
(2, 1, 3),
(2, 2, 7);
Loading