From a2a6508d04b680cbf151698f64f25daf43ef646f Mon Sep 17 00:00:00 2001 From: Vitalii Date: Thu, 9 Jul 2026 14:20:07 +0400 Subject: [PATCH 1/3] Solved task transactions --- task.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/task.sql b/task.sql index 8adf22b..6e1155a 100644 --- a/task.sql +++ b/task.sql @@ -5,7 +5,11 @@ USE ShopDB; -- Start the transaction START TRANSACTION; +INSERT INTO Orders (CustomerID, Date) +VALUES ('1', '2023-01-01'); --- And some data should be created inside the transaction +INSERT INTO OrderItems (OrderID, ProductID , Count) +VALUES (LAST_INSERT_ID(), '1', '1'); -COMMIT; \ No newline at end of file +UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = '1'; +COMMIT \ No newline at end of file From 58b28dc34e1fb52bc97b61e86beefb7cfda53eb4 Mon Sep 17 00:00:00 2001 From: Vitalii Date: Thu, 9 Jul 2026 14:30:32 +0400 Subject: [PATCH 2/3] Fix problems with logic --- task.sql | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/task.sql b/task.sql index 6e1155a..3ecb666 100644 --- a/task.sql +++ b/task.sql @@ -4,12 +4,14 @@ 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'); +START TRANSACTION; + INSERT INTO OrderItems (OrderID, ProductID , Count) VALUES (LAST_INSERT_ID(), '1', '1'); UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = '1'; -COMMIT \ No newline at end of file +COMMIT; \ No newline at end of file From 3b4e560cd387e7c0791876be0d566e3abd7f36ff Mon Sep 17 00:00:00 2001 From: Vitalii Date: Thu, 9 Jul 2026 14:40:01 +0400 Subject: [PATCH 3/3] Fix all --- task.sql | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/task.sql b/task.sql index 3ecb666..7dd3e3d 100644 --- a/task.sql +++ b/task.sql @@ -1,17 +1,21 @@ -- Use our database -USE ShopDB; +USE ShopDB; --- Some data should be created outside the transaction (here) +-- Some data should be created outside the transaction +INSERT INTO Orders (CustomerID, Date) +VALUES (1, '2023-01-01'); --- Start the transaction +-- Save created order ID before transaction +SET @order_id = LAST_INSERT_ID(); -INSERT INTO Orders (CustomerID, Date) -VALUES ('1', '2023-01-01'); +-- Start the transaction +START TRANSACTION; -START TRANSACTION; +INSERT INTO OrderItems (OrderID, ProductID, Count) +VALUES (@order_id, 1, 1); -INSERT INTO OrderItems (OrderID, ProductID , Count) -VALUES (LAST_INSERT_ID(), '1', '1'); +UPDATE Products +SET WarehouseAmount = WarehouseAmount - 1 +WHERE ID = 1; -UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = '1'; COMMIT; \ No newline at end of file