-
Notifications
You must be signed in to change notification settings - Fork 155
DB Normalised #163
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?
DB Normalised #163
Changes from 3 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,5 +1,5 @@ | ||
| -- Create database and tables | ||
|
|
||
| DROP DATABASE IF EXISTS ShopDB; | ||
| CREATE DATABASE ShopDB; | ||
| USE ShopDB; | ||
|
|
||
|
|
@@ -9,25 +9,52 @@ CREATE TABLE Countries ( | |
| PRIMARY KEY (ID) | ||
| ); | ||
|
|
||
| CREATE TABLE Warehouses ( | ||
| ID INT, | ||
| Name VARCHAR(50), | ||
| Address VARCHAR(50), | ||
| CountryID INT, | ||
| FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, | ||
| PRIMARY KEY (ID) | ||
| ); | ||
|
|
||
| CREATE TABLE Products ( | ||
| ID INT, | ||
|
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 key ID columns (e.g. Products.ID) are declared without NOT NULL or AUTO_INCREMENT. Consider adding NOT NULL and AUTO_INCREMENT to primary key integer columns to ensure uniqueness and simplify inserts (optional but recommended). |
||
| Name VARCHAR(50), | ||
| Description VARCHAR(100), | ||
| Price INT, | ||
|
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. Price is currently declared as INT. For monetary values use a decimal type (for example DECIMAL(10,2)) and consider adding NOT NULL to enforce presence of a price. |
||
| PRIMARY KEY (ID) | ||
|
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. Products table is defined but there are no INSERT statements for Products. The ProductInventory rows reference ProductID = 1, so you must insert a matching product (e.g., ID = 1 with name/description/price) before inserting into ProductInventory. |
||
| ); | ||
|
|
||
| CREATE TABLE ProductInventory ( | ||
| ID INT, | ||
| ProductName VARCHAR(50), | ||
| ProductID INT, | ||
|
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. ProductInventory uses a surrogate ID as primary key (ID). To prevent duplicate inventory rows and better model the relationship, consider making (ProductID, WarehouseID) the primary key or at least a UNIQUE constraint on those two columns. |
||
| WarehouseAmount INT, | ||
| WarehouseName VARCHAR(50), | ||
| WarehouseAddress VARCHAR(50), | ||
| CountryID INT, | ||
| FOREIGN KEY (CountryID) REFERENCES Countries(ID) ON DELETE NO ACTION, | ||
| WarehouseID INT, | ||
| FOREIGN KEY (WarehouseID) REFERENCES Warehouses(ID) ON DELETE NO ACTION, | ||
| FOREIGN KEY (ProductID) REFERENCES Products(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); | ||
|
|
||
| INSERT INTO Products (ID,Name,Description,Price) | ||
| VALUES (1, 'Product1', 'Description1', 100); | ||
| INSERT INTO Products (ID,Name,Description,Price) | ||
| VALUES (2, 'Product2', 'Description2', 200); | ||
|
|
||
| INSERT INTO Warehouses (ID,Name,Address,CountryID) | ||
| VALUES (1, 'Warehouse1', 'Address1', 1); | ||
| INSERT INTO Warehouses (ID,Name,Address,CountryID) | ||
| VALUES (2, 'Warehouse2', 'Address2', 2); | ||
|
|
||
| INSERT INTO ProductInventory (ID,ProductID,WarehouseAmount,WarehouseID) | ||
| VALUES (1, 1, 2, 1); | ||
| INSERT INTO ProductInventory (ID,ProductID,WarehouseAmount,WarehouseID) | ||
| VALUES (2, 1, 5, 2); | ||
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 task (and the previous review) requires the DDL be ordered Countries -> Products -> Warehouses -> ProductInventory. Here Warehouses is created before Products which violates that requested order and may be flagged by the grader. Move the Products CREATE TABLE block so it appears before the Warehouses CREATE TABLE block.