-
Notifications
You must be signed in to change notification settings - Fork 186
'Solution' #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
'Solution' #187
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,58 @@ | |
| USE ShopDB; | ||
|
|
||
| -- Some data should be created outside the transaction (here) | ||
| DROP DATABASE ShopDB; | ||
| CREATE DATABASE ShopDB; | ||
| USE ShopDB; | ||
|
|
||
| CREATE TABLE Products ( | ||
| ID INT AUTO_INCREMENT, | ||
| Name VARCHAR(50), | ||
| Description VARCHAR(100), | ||
| Price INT, | ||
| WarehouseAmount INT, | ||
| PRIMARY KEY (ID) | ||
| ); | ||
|
|
||
| CREATE TABLE Customers ( | ||
| ID INT AUTO_INCREMENT, | ||
| FirstName VARCHAR(50), | ||
| LastName VARCHAR(50), | ||
| Email VARCHAR(50), | ||
| Address VARCHAR(50), | ||
| PRIMARY KEY (ID) | ||
| ); | ||
|
|
||
| CREATE TABLE Orders ( | ||
| ID INT AUTO_INCREMENT, | ||
| CustomerID INT, | ||
| FOREIGN KEY (CustomerID) REFERENCES Customers(ID) ON DELETE SET NULL, | ||
| Date DATE, | ||
| PRIMARY KEY (ID) | ||
| ); | ||
|
|
||
| CREATE TABLE OrderItems ( | ||
| ID INT AUTO_INCREMENT, | ||
| OrderID INT, | ||
| FOREIGN KEY (OrderID) REFERENCES Orders(ID) ON DELETE SET NULL, | ||
| ProductID INT, | ||
| Count INT, | ||
| FOREIGN KEY (ProductID) REFERENCES Products(ID) ON DELETE SET NULL, | ||
| PRIMARY KEY (ID) | ||
|
Comment on lines
+9
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace all manual CREATE TABLE statements (lines 9-42) with the
Comment on lines
+9
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 9-42 contain manual |
||
| ); | ||
|
|
||
| INSERT INTO Products (Name, Description, Price, WarehouseAmount) | ||
| VALUES ('AwersomeProduct', 'Product Desctiption', 5, 42); | ||
|
|
||
| INSERT INTO Customers (FirstName, LastName, Email, Address) | ||
| VALUES ('John', 'Dou', 'j@dou.ua', 'far, far away'); | ||
|
|
||
| INSERT INTO Orders (CustomerID, Date) VALUES (1, '2023-01-01'); | ||
| -- Start the transaction | ||
| START TRANSACTION; | ||
|
|
||
| -- And some data should be created inside the transaction | ||
| INSERT INTO OrderItems (OrderID, ProductID, Count) | ||
| VALUES (LAST_INSERT_ID(), 1, 1); | ||
| UPDATE Products SET WarehouseAmount = WarehouseAmount - 1 WHERE ID = 1; | ||
|
|
||
| COMMIT; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move
USE ShopDBAFTER the DROP/CREATE DATABASE statements, or remove this initial USE since there's no ShopDB to use yet at this point.