diff --git a/task.sql b/task.sql index 8adf22b..aabf105 100644 --- a/task.sql +++ b/task.sql @@ -1,11 +1,28 @@ -- Use our database -USE ShopDB; +USE ShopDB; -- Some data should be created outside the transaction (here) --- Start the transaction -START TRANSACTION; +-- Creating an order is a standalone action: a new order starts out empty, +-- so it does not need to be atomic with anything else. +INSERT INTO Orders (CustomerID, Date) + VALUES (1, '2023-01-01'); --- And some data should be created inside the transaction +SET @OrderID = LAST_INSERT_ID(); -COMMIT; \ No newline at end of file +-- Start the transaction +START TRANSACTION; + +-- And some data should be created inside the transaction + +-- Adding an order item and decreasing the corresponding product's +-- WarehouseAmount must happen together, or not at all, to keep stock +-- counts consistent with what has actually been ordered. +INSERT INTO OrderItems (OrderID, ProductID, Count) + VALUES (@OrderID, 1, 1); + +UPDATE Products + SET WarehouseAmount = WarehouseAmount - 1 + WHERE ID = 1; + +COMMIT;