Reassigning a Beacon Robotics project from one employee to another takes two statements: delete the old project_assignments row, insert the new one. If the connection drops between them, the project now has no one assigned at all, half a change applied is worse than no change. A transaction is how to make both statements succeed or fail together.
Learning Objectives
- Define a transaction, and explain what "all-or-nothing" actually means for a group of statements.
- Use COMMIT and ROLLBACK, and the real BEGIN/START TRANSACTION syntax split across all six databases.
- Use SAVEPOINT to undo part of a transaction without discarding all of it.
BEGIN
Open a unit of work
Run Statements
Multiple INSERT / UPDATE / DELETE
COMMIT
Make every change permanent
or ROLLBACK
Undo everything since BEGIN
Starting a Transaction, by Database
| Database | Starting a Transaction |
|---|---|
| MySQL / MariaDB | START TRANSACTION; (BEGIN; also works as an alias) |
| PostgreSQL | BEGIN; (START TRANSACTION; also works, both are accepted) |
| SQL Server | BEGIN TRANSACTION; (or the short form BEGIN TRAN;) |
| SQLite | BEGIN TRANSACTION; (or just BEGIN;) |
| Oracle | No statement needed at all, a transaction starts implicitly with the first DML statement. |
Oracle is the one genuine exception here, there's no BEGIN or START TRANSACTION statement for ordinary use, every INSERT, UPDATE, or DELETE simply opens a transaction on its own if one isn't already open.
COMMIT and ROLLBACK
COMMIT makes every statement since the transaction began permanent, visible to every other connection. ROLLBACK, used instead of COMMIT, undoes every statement since the transaction began, as if none of them had ever run. Once COMMIT has actually succeeded, ROLLBACK can no longer undo it, commit is the point of no return.
START TRANSACTION; DELETE FROM project_assignments WHERE employee_id = 12 AND project_id = 4; INSERT INTO project_assignments (employee_id, project_id) VALUES (18, 4); COMMIT;
BEGIN TRANSACTION; DELETE FROM project_assignments WHERE employee_id = 12 AND project_id = 4; INSERT INTO project_assignments (employee_id, project_id) VALUES (18, 4); COMMIT TRANSACTION;
SAVEPOINT: Undoing Only Part of a Transaction
A SAVEPOINT marks a point inside a still-open transaction that a later ROLLBACK can return to, without discarding everything back to the very start. Useful when one step of a larger transaction might fail in a way that's fine to retry or skip, without losing the steps already done before it.
| Database | Rolling Back to a Savepoint |
|---|---|
| MySQL / MariaDB / PostgreSQL / SQLite / Oracle | SAVEPOINT name; ... ROLLBACK TO SAVEPOINT name; |
| SQL Server | SAVE TRANSACTION name; ... ROLLBACK TRANSACTION name; (different keywords, same idea) |
A MySQL-Specific Gotcha: Storage Engine
Transactions on MySQL and MariaDB only work on the InnoDB storage engine, the default since MySQL 5.5. The older MyISAM engine has no transaction support at all, START TRANSACTION and ROLLBACK are silently accepted but do nothing on a MyISAM table, every statement commits immediately regardless. This is specific to MySQL/MariaDB's pluggable storage engines, none of the other five databases have an engine choice that can silently disable transactions this way.
Try It Yourself
Write a transaction on PostgreSQL that deletes employee id 25's row from project_assignments, then inserts a replacement assignment for employee id 31 on the same project, committing only if both succeed.
BEGIN; DELETE FROM project_assignments WHERE employee_id = 25 AND project_id = 7; INSERT INTO project_assignments (employee_id, project_id) VALUES (31, 7); COMMIT;
Frequently Asked Questions
What happens to an open transaction if the connection just drops, without COMMIT or ROLLBACK?
Every database here rolls it back automatically. A transaction that was never explicitly committed has made no permanent change, regardless of why the connection ended.
Can a SELECT be part of a transaction too, not just INSERT/UPDATE/DELETE?
Yes, a transaction can contain any mix of statements, including SELECT. Running a SELECT inside an open transaction matters most for what it's allowed to see from other, concurrently-running transactions, covered in the next lesson on isolation levels.
Common Beginner Mistakes
Expecting Oracle to need a BEGIN or START TRANSACTION statement
It doesn't have one for ordinary use, a transaction simply starts with the first DML statement. Writing BEGIN; before it on Oracle is a syntax error, not just unnecessary.
Assuming ROLLBACK can undo a transaction after COMMIT already succeeded
It can't, COMMIT is the point of no return, once it succeeds the change is permanent and visible to every other connection. ROLLBACK only works on a transaction that's still open.
Best Practices
- Keep a transaction to the statements that genuinely need to succeed or fail together, not everything a request happens to do.
- Always pair BEGIN/START TRANSACTION with exactly one COMMIT or ROLLBACK, in every code path, including error handling.
- Use SAVEPOINT when one step of a larger transaction might reasonably need to be undone without discarding the whole thing.
Interview Questions
What does it mean for a transaction to be "all-or-nothing"?
Every statement inside the transaction either takes effect together, on COMMIT, or none of them take effect at all, on ROLLBACK or a dropped connection. There's no state where only some of the transaction's statements are visible to other connections, that's the atomicity guarantee a transaction provides.
Why does Oracle not need a BEGIN TRANSACTION statement, when every other database here does?
Oracle's transaction model opens a transaction implicitly with the first DML statement in a session that doesn't already have one open, rather than requiring an explicit statement to start one. COMMIT and ROLLBACK still work identically to the other databases, only the start is implicit.
Summary
A transaction groups multiple statements so they succeed or fail together, COMMIT makes them permanent, ROLLBACK undoes them, SAVEPOINT marks a point partway through for a partial rollback. Starting one is genuine, real syntax that differs by database, down to Oracle not needing a start statement at all.
What's Next?
The next lesson covers ACID, the four properties a transaction is actually supposed to guarantee, atomicity, consistency, isolation, and durability, and what each one really means underneath.
Ready to Practice SQL?
Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.
Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.
Or try 50 SQL practice questions →