-
Notifications
You must be signed in to change notification settings - Fork 181
solution #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
solution #120
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
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'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double-check the product name inserted here: |
||
|
|
||
| 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; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Namecolumn 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 onNamealone or as a composite withAddress.Example options to add inside the CREATE TABLE block:
UNIQUE KEY uq_warehouses_name (Name),-- if warehouse name alone is uniqueUNIQUE KEY uq_warehouses_name_addr (Name, Address),-- if name+address defines uniquenessPick the option that matches your domain expectations and update the table DDL accordingly.