SQL Transactions

In SQL, a Transaction is a unit of work that is performed against a database

Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program

A Transaction is the propagation of one or more changes to the database. For example, if you are creating/updating/deleting a record from the table, then you are performing transaction on the table. It is important to control the transactions to ensure data integrity and to handle database errors

Practically, you will club many SQL queries into a group and you will execute all of them together as a part of a transaction

Properties of Transactions

Transactions have the following four standard properties, usually referred to by the acronym ACID listed below:

  • Consistency - Ensures that the database properly changes states upon a successfully committed transaction
  • Atomicity - Ensures that all the operations within the work unit are completed successfully, otherwise, the transaction is aborted at the point of failure, and previous operations are rolled back to their former state
  • Isolation - Enables transactions to operate independently of and transparent to each other
  • Durability - Ensures that the result/effect of a committed transaction persists in case of a system failure

SQL Transaction Control

There are the following SQL commands which can be used to control the transactions:

  • COMMIT - To save the changes
  • ROLLBACK - To rollback the changes
  • SAVEPOINT - Creates points within the groups of transactions in which to ROLLBACK
  • SET TRANSACTION - Places a name on a transaction

SQL Transactional control commands are only used with the DML commands UPDATE, INSERT, and DELETE only

SQL Online Test

« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋