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
27 changes: 22 additions & 5 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
-- Use our database
USE ShopDB;
USE ShopDB;

-- Some data should be created outside the transaction (here)

-- Start the transaction
START TRANSACTION;
-- Creating an order is a standalone action: a new order starts out empty,
-- so it does not need to be atomic with anything else.
INSERT INTO Orders (CustomerID, Date)
VALUES (1, '2023-01-01');

-- And some data should be created inside the transaction
SET @OrderID = LAST_INSERT_ID();

COMMIT;
-- Start the transaction
START TRANSACTION;

-- And some data should be created inside the transaction

-- Adding an order item and decreasing the corresponding product's
-- WarehouseAmount must happen together, or not at all, to keep stock
-- counts consistent with what has actually been ordered.
INSERT INTO OrderItems (OrderID, ProductID, Count)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You hard-code use of OrderID = 1 for the new order. This will only work if the newly created order’s ID is guaranteed to be 1, which the requirements do not state. To ensure the order item is attached to the order you just inserted, consider capturing the new order’s ID (e.g., via LAST_INSERT_ID() into a variable) and using that value in the OrderItems insert.

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 OrderItems table in the description includes an ID column. Depending on how create-database.sql defines this table, omitting ID here may cause an error if ID is not AUTO_INCREMENT. Check the table schema and adjust the insert to match it (either include ID explicitly or rely on auto-increment if available).

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 task description, the OrderItems table has columns ID, OrderID, and ProductID only; there is no Count column listed, so INSERT INTO OrderItems (OrderID, ProductID, Count) is likely to fail against the actual create-database.sql schema. Please verify the real table definition and adjust the column list and values to match it (and then update how you represent quantity in the order if needed).

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 OrderItems table in this task is defined with columns ID, OrderID, and ProductID; there is no Count column, so this INSERT will fail. Please open create-database.sql, confirm the actual OrderItems schema, and adjust the column list and values so they match it while still representing the quantity (e.g., using multiple rows or an existing quantity field, depending on the schema).

VALUES (@OrderID, 1, 1);

UPDATE Products
SET WarehouseAmount = WarehouseAmount - 1
Comment on lines +24 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You currently update WarehouseAmount using a hard-coded subtraction of 1 based on the assumption that the order item’s Count is 1. If you later extend this script to handle other counts, consider tying the warehouse update to the actual Count inserted for the item to fully follow the business rule that stock should decrease by the ordered quantity.

WHERE ID = 1;

COMMIT;
Loading