Overview
Scenarios
Product Architecture
Instance Types
Compatibility Notes
BEGIN statementBEGIN;
START TRANSACTION statementSTART TRANSACTION;
SET autocommit=0;
SET autocommit=0 is used, all subsequent SQL statements will be executed within a single transaction until explicitly executing COMMIT or ROLLBACK.START TRANSACTION;-- Update the user tableUPDATE users SET balance = balance - 100 WHERE user_id = 1;-- Insert order recordsINSERT INTO orders (user_id, amount, order_date)VALUES (1, 100, NOW());
COMMIT statement to commit the transaction:COMMIT;
ROLLBACK statement to roll back the transaction:ROLLBACK;
SAVEPOINT savepoint_name;
ROLLBACK TO SAVEPOINT savepoint_name;
RELEASE SAVEPOINT savepoint_name;
-- Create a test table.CREATE TABLE bank_account (account_id VARCHAR(20) PRIMARY KEY,account_name VARCHAR(50),balance DECIMAL(15, 2));-- Insert initial data.INSERT INTO bank_account VALUES('1001', 'Zhang San', 5000.00),('1002', 'Li Si', 3000.00);
-- 1. BEGIN TRANSACTIONSTART TRANSACTION;-- Check initial balanceSELECT * FROM bank_account WHERE account_id IN ('1001', '1002');+------------+--------------+---------+| account_id | account_name | balance |+------------+--------------+---------+| 1001 | Zhang San | 5000.00 || 1002 | Li Si | 3000.00 |+------------+--------------+---------+2 rows in set (0.00 sec)-- 2. Create the first savepoint (before deduction)SAVEPOINT before_debit;-- 3. Deduct 1000 CNY from Zhang San's account.UPDATE bank_account SET balance = balance - 1000 WHERE account_id = '1001';-- 4. Create the second savepoint (after deduction, before deposit)SAVEPOINT after_debit;-- 5. Deposit 1000 CNY to Li Si's account.UPDATE bank_account SET balance = balance + 1000 WHERE account_id = '1002';-- Check account balance within the current transactionSELECT * FROM bank_account WHERE account_id IN ('1001', '1002');+------------+--------------+---------+| account_id | account_name | balance |+------------+--------------+---------+| 1001 | Zhang San | 4000.00 || 1002 | Li Si | 4000.00 |+------------+--------------+---------+2 rows in set (0.00 sec)-- Assuming that Li Si's account status is found to be abnormal at this time, it is necessary to roll back to before the deposit operation.-- 6. Roll back to the second savepoint (This will undo the deposit operation while retaining the deduction operation.)ROLLBACK TO SAVEPOINT after_debit;-- Check the balance after rollback (Li Si's account balance remains unchanged)SELECT * FROM bank_account WHERE account_id IN ('1001', '1002');+------------+--------------+---------+| account_id | account_name | balance |+------------+--------------+---------+| 1001 | Zhang San | 4000.00 || 1002 | Li Si | 3000.00 |+------------+--------------+---------+2 rows in set (0.00 sec)-- 7. Since business rules require that transfers must be complete, we decide to roll back the entire transaction.ROLLBACK; -- Roll back to the transaction start state with all operations undone-- Finally confirm whether the data has been restored to its original state.SELECT * FROM bank_account WHERE account_id IN ('1001', '1002');+------------+--------------+---------+| account_id | account_name | balance |+------------+--------------+---------+| 1001 | Zhang San | 5000.00 || 1002 | Li Si | 3000.00 |+------------+--------------+---------+2 rows in set (0.00 sec)-- 8. Restart the transaction and perform a complete transferSTART TRANSACTION;UPDATE bank_account SET balance = balance - 1000 WHERE account_id = '1001';UPDATE bank_account SET balance = balance + 1000 WHERE account_id = '1002';-- 9. Commit the transaction to make the changes permanent after confirming everything is correctCOMMIT;-- Finally confirm whether the data modification has taken effect.SELECT * FROM bank_account WHERE account_id IN ('1001', '1002');+------------+--------------+---------+| account_id | account_name | balance |+------------+--------------+---------+| 1001 | Zhang San | 4000.00 || 1002 | Li Si | 4000.00 |+------------+--------------+---------+2 rows in set (0.00 sec)
피드백