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
66 changes: 42 additions & 24 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,33 +1,51 @@
-- Create database and tables

DROP DATABASE IF EXISTS ShopDB;

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 script uses DROP DATABASE IF EXISTS ShopDB;. The task description explicitly mentions DROP DATABASE ShopDB;. IF EXISTS is safer for re-runs, but if the automated grader expects the exact statement without IF EXISTS, change this to match the grader. Otherwise IF EXISTS is fine.

CREATE DATABASE ShopDB;
USE ShopDB;


CREATE TABLE Countries (
ID INT,
Name VARCHAR(50),
PRIMARY KEY (ID)
ID INT AUTO_INCREMENT PRIMARY KEY,

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 task explicitly requires dropping any existing ShopDB before re-creating it. Add the statement DROP DATABASE ShopDB; at the very start of the script (before CREATE DATABASE ShopDB;). Without this the script does not meet the requirement to recreate the schema from scratch.

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.

Consider adding a UNIQUE (Name) constraint on Countries.Name if duplicate country names should be prevented in your domain. This is optional (low priority) but can help data quality.

);


CREATE TABLE Warehouses (
ID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Address VARCHAR(100),
CountryID INT NOT NULL,
FOREIGN KEY (CountryID) REFERENCES Countries(ID)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Foreign key is defined here; consider specifying explicit ON DELETE/ON UPDATE behavior (e.g., RESTRICT, CASCADE, or NO ACTION) to make referential actions explicit. This is optional but clarifies intended behavior.

);


CREATE TABLE Products (
ID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(50) NOT NULL
);


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)
ID INT AUTO_INCREMENT PRIMARY KEY,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prevent duplicate inventory rows for the same product+warehouse pair by enforcing a uniqueness constraint at the business-key level. Either replace the surrogate ID with a composite primary key (ProductID, WarehouseID) or add UNIQUE(ProductID, WarehouseID) to ProductInventory so each product/warehouse pair appears only once.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ID INT AUTO_INCREMENT PRIMARY KEY in ProductInventory allows duplicate (ProductID, WarehouseID) rows. Enforce one inventory row per product-per-warehouse by either: (a) replacing the auto-increment ID with a composite primary key PRIMARY KEY (ProductID, WarehouseID), or (b) keep the ID but add UNIQUE (ProductID, WarehouseID). This prevents duplicate inventory rows.

ProductID INT NOT NULL,
WarehouseID INT NOT NULL,
WarehouseAmount INT UNSIGNED NOT NULL DEFAULT 0,
FOREIGN KEY (ProductID) REFERENCES Products(ID),

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 foreign keys on ProductInventory are correct. As with the Warehouses FK, you may want to specify ON DELETE/ON UPDATE actions for these FKs to document expected cascade or restriction behavior. This is optional and not required by the checklist.

FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID),
UNIQUE (ProductID, WarehouseID)
);

-- 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);
INSERT INTO Countries (Name)
VALUES ('Country1'), ('Country2');

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

INSERT INTO Products (Name)
VALUES ('AwesomeProduct');

INSERT INTO ProductInventory (ProductID, WarehouseID, WarehouseAmount)
VALUES
(1, 1, 2),
(1, 2, 5);