What is a database transaction?
Posted: (EET/GMT+2)
Databases are the core of data storage for many applications. If your application updates data in multiple steps in a relational database, it is useful to group those changes into one a database operation. These are called transactions.
A transaction is a unit of work that either succeeds completely or fails completely. In SQL databases like InterBase, Oracle or SQL Server, you can execute SQL commands to control transactions. A typical transaction consists of three operations:
BEGIN TRANSACTION ... (your work here, such as INSERTs) ... COMMIT ROLLBACK
Changes made during the transaction are not permanently stored until a COMMIT is issued.
If an error occurs, a ROLLBACK can be used to undo all changes made during the transaction.
For example, when transferring money between two accounts:
Debit Account A Credit Account B COMMIT
If the second operation fails, the transaction can be rolled back to prevent the database from entering an inconsistent state.
Transactions are supported by most relational database systems, including Microsoft SQL Server and Oracle.