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
46 changes: 36 additions & 10 deletions task.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,29 @@ CREATE TABLE Countries (
PRIMARY KEY (ID)
);

CREATE TABLE Warehouse(
ID INT AUTO_INCREMENT,
WarehouseName VARCHAR(50),
WarehouseAddressCity VARCHAR(50),
WarehouseAddressStreet VARCHAR(50),
CountriesID INT,
FOREIGN KEY (CountriesID) REFERENCES Countries(ID) ON DELETE NO ACTION,
PRIMARY KEY (ID)
);
Comment thread
OlehHavelia1994 marked this conversation as resolved.

CREATE TABLE Products(
ID INT AUTO_INCREMENT,
ProductName VARCHAR(50),
PRIMARY KEY (ID)
);

CREATE TABLE ProductInventory (
ID INT,
ProductName VARCHAR(50),
WarehouseID INT,
ProductID INT,
WarehouseAmount INT,
WarehouseName VARCHAR(50),
WarehouseAddress VARCHAR(50),
CountryID INT,
FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION,
FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE NO ACTION,
FOREIGN KEY (WarehouseID) REFERENCES Warehouse(ID) ON DELETE NO ACTION,
PRIMARY KEY (ID)
);
Comment thread
OlehHavelia1994 marked this conversation as resolved.

Expand All @@ -26,8 +41,19 @@ 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 Warehouse (ID, WarehouseName, WarehouseAddressCity, WarehouseAddressStreet, CountriesID)

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 ID column in the Warehouse table has AUTO_INCREMENT enabled. It's a best practice to let the database manage these values. You can do this by omitting the ID column from your INSERT statement and the corresponding value from the VALUES clause. The same applies to the Products table.

VALUES (1, 'Warehouse-1', 'City-1', 'Street-1', 1);
INSERT INTO Warehouse (ID, WarehouseName, WarehouseAddressCity, WarehouseAddressStreet, CountriesID)
VALUES (2, 'Warehouse-2', 'City-2', 'Street-2', 2);

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

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 Products table should contain unique product entries to avoid redundancy. Inserting 'AwersomeProduct' a second time creates a different product record with the same name. To represent a single product type stored in multiple locations, there should be only one record for 'AwersomeProduct' in this table. This INSERT statement should be removed.

VALUES (2, 'AwersomeProduct');


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This inventory record is for 'AwersomeProduct'. Since there should only be one record for this product in the Products table (with ID 1), this ProductID should be 1 instead of 2 to correctly link to it.