Skip to content
Open
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
109 changes: 80 additions & 29 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,84 @@
-- Create database and tables

-- 1. Пересоздаємо базу (строго за чеклістом)
DROP DATABASE ShopDB;
CREATE DATABASE ShopDB;
USE ShopDB;

-- 2. Нормалізована структура з усіма обмеженнями та InnoDB

-- 2.1 Countries
CREATE TABLE Countries (
ID INT,
Name VARCHAR(50),
PRIMARY KEY (ID)
);

CREATE TABLE ProductInventory (
ID INT,
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)
);

-- 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);
ID INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(50) NOT NULL UNIQUE,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2.2 Products
CREATE TABLE Products (
ID INT NOT NULL AUTO_INCREMENT,
ProductName VARCHAR(50) NOT NULL UNIQUE,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2.3 Warehouses
CREATE TABLE Warehouses (
ID INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(50) NOT NULL,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Name column for Warehouses is defined but there is no UNIQUE constraint. Checklist item 3.3 requires warehouse identity be uniquely defined to prevent duplicates/update anomalies. Add a uniqueness constraint — either on Name alone or as a composite with Address.

Example options to add inside the CREATE TABLE block:

  • UNIQUE KEY uq_warehouses_name (Name), -- if warehouse name alone is unique
  • UNIQUE KEY uq_warehouses_name_addr (Name, Address), -- if name+address defines uniqueness

Pick the option that matches your domain expectations and update the table DDL accordingly.

Address VARCHAR(100) NOT NULL,
CountryID INT NOT NULL,
PRIMARY KEY (ID),
CONSTRAINT fk_warehouses_country
FOREIGN KEY (CountryID) REFERENCES Countries(ID)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2.4 Inventory
CREATE TABLE Inventory (
ProductID INT NOT NULL,
WarehouseID INT NOT NULL,
Quantity INT UNSIGNED NOT NULL,
PRIMARY KEY (ProductID, WarehouseID),
Comment on lines +12 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primary keys are present but are not named with explicit constraint identifiers. Checklist item 4.3 recommends explicitly naming primary keys (where possible) to improve verifiability. Consider changing the anonymous PRIMARY KEY declarations to named constraints, for example:

  • CONSTRAINT pk_countries PRIMARY KEY (ID)
  • CONSTRAINT pk_products PRIMARY KEY (ID)
  • CONSTRAINT pk_warehouses PRIMARY KEY (ID)
  • CONSTRAINT pk_inventory PRIMARY KEY (ProductID, WarehouseID)

This is not required for execution but makes automated checks and reviews clearer.

CONSTRAINT fk_inventory_product
FOREIGN KEY (ProductID) REFERENCES Products(ID)
ON DELETE RESTRICT
ON UPDATE CASCADE,
CONSTRAINT fk_inventory_warehouse
FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID)
ON DELETE RESTRICT
ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. Тестові дані

INSERT INTO Countries (ID, Name) VALUES
(1, 'Country1'),
(2, 'Country2');

INSERT INTO Products (ID, ProductName) VALUES
(1, 'AwersomeProduct');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double-check the product name inserted here: AwersomeProduct. The checklist requires that the test data reconstruct the same logical dataset as the original ProductInventory. If the original name was AwesomeProduct (or another spelling), update this value to match the original to avoid test mismatches.


INSERT INTO Warehouses (ID, Name, Address, CountryID) VALUES
(1, 'Warehouse-1', 'City-1, Street-1', 1),
(2, 'Warehouse-2', 'City-2, Street-2', 2);

INSERT INTO Inventory (ProductID, WarehouseID, Quantity) VALUES
(1, 1, 2),
(1, 2, 5);

-- 4. VIEW ProductInventory для сумісності з тестовим скриптом
CREATE VIEW ProductInventory AS
SELECT
(@rownum := @rownum + 1) AS ID,
p.ProductName AS ProductName,
i.Quantity AS WarehouseAmount,
w.Name AS WarehouseName,
w.Address AS WarehouseAddress,
w.CountryID
FROM (SELECT @rownum := 0) AS init
JOIN Inventory i
ON TRUE
JOIN Products p
ON i.ProductID = p.ID
JOIN Warehouses w
ON i.WarehouseID = w.ID
ORDER BY i.ProductID, i.WarehouseID;
Loading