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
6 changes: 5 additions & 1 deletion task.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ USE ShopDB;

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

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 comment incorrectly suggests Orders should be created outside the transaction. Based on the business rules, all three operations (Orders, OrderItems, Products update) must succeed or fail together. The START TRANSACTION should come immediately after USE ShopDB;.


INSERT INTO Orders (CustomerID, Date)
VALUES (1, "2023-01-01");
Comment on lines +6 to +7

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Move this INSERT INTO Orders statement inside the transaction. It must come after START TRANSACTION (line 9) and before the OrderItems insert. Currently, if the OrderItems or Products update fails, you'll have an orphaned order with no items — violating transactional atomicity.

-- Start the transaction
START TRANSACTION;

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.

Hardcoded OrderID = 1 will fail if the database has existing orders. Use LAST_INSERT_ID() to get the ID of the order just created in this session.

VALUES (LAST_INSERT_ID(), 1, 1);
UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 where Name = "AwersomeProduct";
-- And some data should be created inside the transaction

COMMIT;
Loading