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
29 changes: 22 additions & 7 deletions task.sql
Original file line number Diff line number Diff line change
@@ -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;
-- 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Double-check the OrderItems table definition in create-database.sql. The task text says OrderItems has ID, OrderID, ProductID (and later mentions Count), so if the actual schema doesn’t include Count or uses a different column name, this insert will fail; align the column list with the real schema.

VALUES (@NewOrderID, 1, 1);

-- 5. COMMIT THE TRANSACTION
-- This permanently saves both changes to the database at the exact same time
COMMIT;
Loading