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: 14 additions & 6 deletions task.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
-- Use our database
USE ShopDB;
USE ShopDB;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The task requires dropping and recreating the ShopDB database using DROP DATABASE ShopDB; and the create-database.sql script. Currently, only USE ShopDB; is present, which assumes the database already exists. Add DROP DATABASE IF EXISTS ShopDB; and CREATE DATABASE ShopDB; statements before the USE statement.


-- Some data should be created outside the transaction (here)
-- ShopDB should be created by running create-database.sql before this script.
-- AwersomeProduct with ID 1 and customer with ID 1 are inserted there as initial data.

-- Start the transaction
START TRANSACTION;
INSERT INTO Orders (CustomerID, Date)
VALUES (1, '2023-01-01');
Comment on lines +7 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The INSERT INTO Orders is placed before START TRANSACTION, breaking atomicity. If the OrderItems insert or Products update fails, the order will remain orphaned in the database. Move START TRANSACTION before this INSERT and the COMMIT after the UPDATE to ensure all operations succeed or fail together.


-- And some data should be created inside the transaction
-- Start the transaction
START TRANSACTION;

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

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

COMMIT;
Loading