Answer: Using SAVEPOINT
Explore how to implement SAVEPOINT to manage transactions in MySQL effectively. Understand creating savepoints, rolling back to them, and using session variables to handle intermediate states. This lesson helps you master transaction control techniques, ensuring atomic updates while allowing partial rollback for data integrity.
Solution
The solution is given below:
Explanation
The explanation of the solution code is given below:
Line 1: Starts a new transaction so that all following operations are grouped into one atomic unit of work that can be committed or rolled back together.
Lines 4–7: Select the current
QuantityandStockvalues forOrderID = 3andProductID = 3. The values are stored in session variables@orig_qtyand@orig_stockfor later comparison.Lines 10–12: Update the
Order_Detailstable by increasing theQuantityby2units. This simulates an adjustment to the order quantity.Lines 14–16: Update the
Productstable by reducing theStockby2units to keep the inventory consistent with the order change.Line 18: Create a savepoint named
sp_first_updatethat marks the state of the database after the first set of updates. This allows rolling back to this specific point ...