From 5cb9acb949e493f283da4e8df9b3dbd8ca6eb47a Mon Sep 17 00:00:00 2001 From: Illia Fedoruk Date: Wed, 15 Jul 2026 20:38:38 +0300 Subject: [PATCH 1/2] Add transaction for order creation --- task.sql | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/task.sql b/task.sql index 8adf22b..54bf738 100644 --- a/task.sql +++ b/task.sql @@ -1,11 +1,17 @@ --- 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'); --- And some data should be created inside the transaction + +START TRANSACTION; + +INSERT INTO OrderItems (OrderID, ProductID, Count) + VALUES (1, 1, 1); + +UPDATE Products +SET WarehouseAmount = WarehouseAmount - 1 +WHERE ID = 1; COMMIT; \ No newline at end of file From 59ce23031722f0e5992c937fdd5c87632c54f7ba Mon Sep 17 00:00:00 2001 From: Illia Fedoruk Date: Wed, 15 Jul 2026 20:49:45 +0300 Subject: [PATCH 2/2] fix: use LAST_INSERT_ID to link order items and synchronize inventory update --- task.sql | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/task.sql b/task.sql index 54bf738..540117a 100644 --- a/task.sql +++ b/task.sql @@ -7,11 +7,14 @@ INSERT INTO Orders (CustomerID, Date) START TRANSACTION; +SET @order_id = LAST_INSERT_ID(); +SET @item_count = 1; + INSERT INTO OrderItems (OrderID, ProductID, Count) - VALUES (1, 1, 1); + VALUES (@order_id, 1, @item_count); -UPDATE Products -SET WarehouseAmount = WarehouseAmount - 1 +UPDATE Products +SET WarehouseAmount = WarehouseAmount - @item_count WHERE ID = 1; COMMIT; \ No newline at end of file