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

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

-- Start the transaction
START TRANSACTION;
INSERT INTO Orders (CustomerID, Date)
VALUES (1, '2023-01-01');
Comment on lines +4 to +5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here you insert the order, but you never capture the generated ID; later you assume it is 1. To meet the requirement of creating a new order reliably, consider storing LAST_INSERT_ID() in a variable so you can reference the actual order you just created.


-- And some data should be created inside the transaction

START TRANSACTION;

SET @order_id = LAST_INSERT_ID();
SET @item_count = 1;

INSERT INTO OrderItems (OrderID, ProductID, Count)
VALUES (@order_id, 1, @item_count);

UPDATE Products
SET WarehouseAmount = WarehouseAmount - @item_count
WHERE ID = 1;

COMMIT;
Loading