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
64 changes: 41 additions & 23 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;

CREATE DATABASE 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.

According to the requirements, the script should first delete the ShopDB database if it already exists. Please add DROP DATABASE IF EXISTS ShopDB; at the very beginning of this file, before the CREATE DATABASE statement.

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 script will fail because you're trying to use the ShopDB database before it has been created. You've dropped it on line 1, but you need to add a CREATE DATABASE ShopDB; command before this line.


USE ShopDB;

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

Create Table Countries (
ID INT AUTO_INCREMENT,
PRIMARY KEY (ID),
NAME VARCHAR(50)
);

CREATE TABLE ProductInventory (
ID INT,
ProductName VARCHAR(50),
WarehouseAmount INT,
CREATE TABLE Warehouses (
ID INT AUTO_INCREMENT,
PRIMARY KEY (ID),
WarehouseName VARCHAR(50),
WarehouseAddress VARCHAR(50),
WarehouseAddress VARCHAR(50),
CountryID INT,
FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION,
PRIMARY KEY (ID)
FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION
);

-- 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);
CREATE TABLE Product (
ID INT AUTO_INCREMENT,
PRIMARY KEY (ID),
Name VARCHAR(50)
);
CREATE TABLE ProductInventory (
ID INT AUTO_INCREMENT,
PRIMARY KEY (ID),
Amount INT,
WarehouseID INT,
FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION,
ProductId INT,
FOREIGN KEY (ProductId) REFERENCES Product(ID) ON DELETE CASCADE
);


INSERT INTO Countries (NAME)
VALUES ('Country1'), ('Country2');


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

INSERT INTO Product (Name)
VALUES ('Product-1'), ('Product-2');

INSERT INTO ProductInventory (WarehouseID, ProductID, Amount)
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 INSERT statement creates two identical records for ProductID 1 in WarehouseID 1. This is redundant. The purpose of this table is to store the inventory amount for a specific product in a specific warehouse, so you should have only one record for each unique product-warehouse combination.

Consider populating the inventory with more varied data using the other product and warehouse you created. For example:
VALUES (1, 1, 2), (2, 2, 5);

VALUES (1, 1, 2), (2, 2, 5);
Loading