COMMIT
is an SQL statement that permanently confirms all changes made within a transaction. When executed, the operations (INSERT, UPDATE, DELETE) are moved from the temporary workspace to the persistent database and become visible to other users and sessions.
Until COMMIT
is executed, the changes remain provisional and are only visible within the current session.
Relation to transactions
A transaction is a set of SQL operations that are treated as a single, indivisible unit of work and must comply with the ACID properties:
- Atomicity: either all operations are executed, or none.
- Consistency: the database moves from one valid state to another.
- Isolation: transactions do not interfere with one another.
- Durability: changes confirmed with
COMMIT
are preserved even in case of a system failure.
Example
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
- If
COMMIT
is executed, both changes are saved and become visible. - If a problem occurs and
ROLLBACK
is executed, the changes are undone and the database returns to its initial state.
See also
- [ROLLBACK]
- [Transaction (SQL)]
- [ACID properties]