Wednesday, November 18, 2015

What is a Transaction in SQL?

Books Online defines a transaction as a "sequence of operations performed as a single logical unit of work". 
In SQL a transaction has to satisfy four key properties that are abbreviated ACID
ACID is an acronym for for Atomic Consistent Isolated Durability.

There are two types of transactions in SQL Server, implicit and explicit, and they are differentiated
only by the way they are created.

Implicit transactions are used automatically by SQL Server to guarantee the ACID properties
of single commands. For example, if you wrote an update statement that modifi ed 10 rows, SQL
Server would run it as an implicit transaction so that the ACID properties would apply, and all 10
rows would be updated or none of them would.

Explicit transactions are started by using the BEGIN TRANSACTION T-SQL command and are stopped
by using the COMMIT TRANSACTION or ROLLBACK TRANSACTION commands.
Committing a transaction effectively means making the changes within the transaction permanent,
whereas rolling back a transaction means undoing all the changes that were made within the
transaction. Explicit transactions are used to group together changes to which you want to apply
the ACID properties as a whole, which also enables you to roll back the changes at any point if your
business logic determines that you should cancel the change.


For more Information on ACID properties see my next post

No comments:

Post a Comment