From 44b78215949b8ddf37a1c75983288a61124d495a Mon Sep 17 00:00:00 2001 From: zaeba Date: Thu, 19 Dec 2024 18:36:46 +0200 Subject: [PATCH] 2 --- task.sql | 61 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/task.sql b/task.sql index cc65344..d141d59 100644 --- a/task.sql +++ b/task.sql @@ -1,33 +1,56 @@ --- Create database and tables - +-- Drop and recreate the database +DROP DATABASE IF EXISTS ShopDB; CREATE DATABASE ShopDB; USE ShopDB; +-- Create Countries table CREATE TABLE Countries ( - ID INT, + ID INT AUTO_INCREMENT, Name VARCHAR(50), PRIMARY KEY (ID) ); +-- Create Products table +CREATE TABLE Products ( + ID INT AUTO_INCREMENT, + Name VARCHAR(50), + PRIMARY KEY (ID) +); + +-- Create Warehouses table +CREATE TABLE Warehouses ( + ID INT AUTO_INCREMENT, + Name VARCHAR(50), + Address VARCHAR(100), + CountryID INT, + PRIMARY KEY (ID), + FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION +); + +-- Create ProductInventory table CREATE TABLE ProductInventory ( - ID INT, - ProductName VARCHAR(50), + ID INT AUTO_INCREMENT, + ProductID INT, + WarehouseID INT, WarehouseAmount INT, - WarehouseName VARCHAR(50), - WarehouseAddress VARCHAR(50), - CountryID INT, - FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, - PRIMARY KEY (ID) + PRIMARY KEY (ID), + FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE CASCADE, + FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE CASCADE ); -- 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); +-- Countries +INSERT INTO Countries (Name) VALUES ('Country1'); +INSERT INTO Countries (Name) VALUES ('Country2'); + +-- Products +INSERT INTO Products (Name) VALUES ('AwersomeProduct'); + +-- Warehouses +INSERT INTO Warehouses (Name, Address, CountryID) VALUES ('Warehouse-1', 'City-1, Street-1', 1); +INSERT INTO Warehouses (Name, Address, CountryID) VALUES ('Warehouse-2', 'City-2, Street-2', 2); + +-- ProductInventory +INSERT INTO ProductInventory (ProductID, WarehouseID, WarehouseAmount) VALUES (1, 1, 2); +INSERT INTO ProductInventory (ProductID, WarehouseID, WarehouseAmount) VALUES (1, 2, 5);