From deeb9ea1050e6983fcd4e4b46b5cd699c0f23677 Mon Sep 17 00:00:00 2001 From: Artem Date: Sun, 14 Jun 2026 16:53:47 +0000 Subject: [PATCH] solution --- task.sql | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/task.sql b/task.sql index 8adf22b..ea2d844 100644 --- a/task.sql +++ b/task.sql @@ -1,11 +1,26 @@ --- Use our database -USE ShopDB; +-- 1. Switch to the correct database +USE ShopDB; --- Some data should be created outside the transaction (here) +-- 2. Create a new empty order for Customer ID 1 +INSERT INTO Orders (CustomerID, Date) +VALUES (1, '2023-01-01'); --- Start the transaction -START TRANSACTION; +-- 3. Get the ID of the order we just created and save it into a variable +SET @NewOrderID = LAST_INSERT_ID(); --- And some data should be created inside the transaction +-- 4. START THE TRANSACTION +-- We group the stock update and the order item creation together +START TRANSACTION; -COMMIT; \ No newline at end of file + -- Step A: Decrease the warehouse stock for AwersomeProduct (ID: 1) by 1 + UPDATE Products + SET WarehouseAmount = WarehouseAmount - 1 + WHERE ID = 1; + + -- Step B: Add AwersomeProduct to the OrderItems table + INSERT INTO OrderItems (OrderID, ProductID, Count) + VALUES (@NewOrderID, 1, 1); + +-- 5. COMMIT THE TRANSACTION +-- This permanently saves both changes to the database at the exact same time +COMMIT;