Transaction

A transaction is a sequence of one or more SQL operations executed as a single unit of work, ensuring data consistency using ACID properties.

Transactions are used to ensure that multiple database operations either all succeed or all fail. If an error occurs, the transaction can be rolled back to its previous state.

Example SQL Transaction:

sqlCopyEditSTART TRANSACTION;
UPDATE Accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;

If any step fails, a ROLLBACK can undo changes.