Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
-- Use our database
USE ShopDB;
USE ShopDB;

-- Some data should be created outside the transaction (here)
INSERT INTO Orders (CustomerID, Date)
VALUES (1, '2023-01-01');

-- Start the transaction
START TRANSACTION;
SET @order_id = LAST_INSERT_ID();

-- And some data should be created inside the transaction
START TRANSACTION;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This transaction should begin before the INSERT INTO Orders statement on line 3. Creating an order, adding an item, and updating stock are all parts of a single business operation. To ensure data integrity (i.e., to avoid creating an empty order if adding an item fails), all these steps should be inside the same transaction.


COMMIT;
INSERT INTO OrderItems (OrderID, ProductID, Count)
VALUES (@order_id, 1, 1);

UPDATE Products
SET WarehouseAmount = WarehouseAmount - 1
WHERE ID = 1;

COMMIT;
Loading