A transaction in SQL Server is a sequential group of operations (such as queries or statements) that are performed as a single unit of work. These operations may involve reading, writing, updating, or deleting data in a database.
Transactions ensure data integrity by following the ACID properties.

- Atomicity: Ensures that a transaction is treated as a single unit. Either all operations in the transaction are completed successfully, or none are applied, maintaining the "all-or-nothing" principle.
- Consistency: Guarantees that a transaction transforms the database from one valid state to another, adhering to all defined rules, constraints, and relationships.
- Isolation: Ensures that concurrent transactions do not interfere with each other, maintaining data integrity as if transactions were executed sequentially.
- Durability: Ensures that once a transaction is committed, its changes are permanently recorded in the database, even in the event of a system failure.
Transaction Control
The following are the commands used to control transactions.
- BEGIN TRANSACTION: Marks the start of a transaction. All subsequent operations will be part of this transaction until it is committed or rolled back.
- COMMIT: Saves all the changes made during the transaction permanently to the database. Once committed, the changes cannot be undone.
- ROLLBACK: Reverts all changes made during the transaction to their state at the start of the transaction, effectively canceling the transaction.
- SAVEPOINT: Creates a checkpoint within a transaction. This allows rolling back a transaction to a specific point without undoing the entire transaction.
- RELEASE SAVEPOINT: Deletes a previously defined SAVEPOINT. Once released, SAVEPOINT can no longer be used for rollback.
- SET TRANSACTION: Configures a transaction with specific properties, such as setting it to read-only or read/write, or associating it with a specific rollback segment.
Types of Transactions
- Implicit Transactions
- Automatically initiated by the database system when specific commands (e.g., INSERT, DELETE, UPDATE) are executed.
- The transaction remains active until explicitly committed or rolled back by the user.
- Explicit Transactions
- Manually initiated and controlled by the user.
- Typically defined using BEGIN TRANSACTION, followed by COMMIT or ROLLBACK to either save or undo changes.
- Autocommit Transactions
- The default transaction mode in most SQL systems.
- Each individual SQL statement is automatically committed if it executes successfully. No explicit commands are needed to commit or rollback.
- Savepoints
- Checkpoints within a transaction that allow partial rollbacks.
- Useful for rolling back a specific part of a transaction without undoing the entire







Join the conversation! Your thoughts help the community grow.