What is a transaction in SQL?

Answer

A transaction is a sequence of one or more SQL operations treated as a single logical unit of work. Either ALL operations succeed (COMMIT) or ALL are undone (ROLLBACK), ensuring the database remains in a consistent state. Classic example: bank transfer — debit one account and credit another. If the credit fails after the debit, the debit must be rolled back. Transaction control: START TRANSACTION; (or BEGIN;) — begins the transaction; COMMIT; — saves all changes permanently; ROLLBACK; — reverts all changes made since the transaction started; SAVEPOINT name; — creates a checkpoint within a transaction; ROLLBACK TO SAVEPOINT name; — rolls back to the savepoint without undoing the entire transaction. Auto-commit: MySQL auto-commits each statement by default (each statement is its own transaction). Disable with SET autocommit = 0; or use explicit START TRANSACTION. Transactions are only supported by transactional storage engines — InnoDB supports transactions; MyISAM does not. In application code, always wrap multi-step operations that must be atomic in a transaction.