From 772d7f26720b6c64ef16af67c74a187d1aab1e34 Mon Sep 17 00:00:00 2001 From: maksy Date: Tue, 12 May 2026 16:18:08 +0300 Subject: [PATCH 1/2] Add transaction for creating order and updating stock --- task.sql | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/task.sql b/task.sql index 8adf22b..2437f07 100644 --- a/task.sql +++ b/task.sql @@ -1,11 +1,17 @@ --- Use our database -USE ShopDB; +USE ShopDB; --- Some data should be created outside the transaction (here) +START TRANSACTION; --- Start the transaction -START TRANSACTION; +INSERT INTO Orders (CustomerID, Date) +VALUES (1, '2023-01-01'); --- And some data should be created inside the transaction +SET @order_id = LAST_INSERT_ID(); -COMMIT; \ No newline at end of file +INSERT INTO OrderItems (OrderID, ProductID, Count) +VALUES (@order_id, 1, 1); + +UPDATE Products +SET WarehouseAmount = WarehouseAmount - 1 +WHERE ID = 1; + +COMMIT; \ No newline at end of file From 78eedf74dcc6399aae71fb86dfea60d1739ba49d Mon Sep 17 00:00:00 2001 From: maksy Date: Tue, 12 May 2026 16:23:07 +0300 Subject: [PATCH 2/2] Add transaction for creating order and updating stock v1 --- task.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task.sql b/task.sql index 2437f07..2818097 100644 --- a/task.sql +++ b/task.sql @@ -1,12 +1,12 @@ USE ShopDB; -START TRANSACTION; - INSERT INTO Orders (CustomerID, Date) VALUES (1, '2023-01-01'); SET @order_id = LAST_INSERT_ID(); +START TRANSACTION; + INSERT INTO OrderItems (OrderID, ProductID, Count) VALUES (@order_id, 1, 1);