START TRANSACTION is used in MySQL to begin a database transaction. A transaction allows you to execute multiple SQL statements as a single logical unit, ensuring data consistency and control over commit or rollback.
A transaction starts with START TRANSACTION and ends with either COMMIT or ROLLBACK.
-- Start a new transaction
START TRANSACTION;
-- Insert a new record
INSERT INTO accounts (name, balance) VALUES ('Rahul', 5000);
-- Update another record
UPDATE accounts SET balance = balance - 1000 WHERE name = 'Amit';
-- Save all changes permanently
COMMIT;
All SQL statements execute successfully and the changes are permanently stored in the database after COMMIT. If any statement fails before commit, you can use ROLLBACK to undo all changes.
ROLLBACK during testing to avoid permanent changes