Master SQL Transaction Commit and Rollback with Clear Examples

Transactions are a critical aspect of SQL, allowing multiple related operations to be performed together as a single, atomic unit. This ensures data integrity and consistency in databases. Let's explore SQL transaction COMMIT and ROLLBACK operations, params which are crucial for understanding and managing transactions.

SQL Transactions Explained πŸ’Ύ
SQL Transactions Explained πŸ’Ύ

Imagine a bank transfer scenario. We want to ensure that if our transfer fails, the money isn't deducted from our account. Here, the atomic nature of SQL transactions comes into play, protecting data from partial or uncompleted operations.

What is transaction in SQL?
What is transaction in SQL?

What is a COMMIT in SQL?

COMMIT is a SQL command that makes changes permanent and prepares a transaction for commit.

Transaction Management in DBMS Explained | ACID, States & Commands
Transaction Management in DBMS Explained | ACID, States & Commands

A COMMIT operation is typically used to end a transaction and save all changes. For instance, when successful bank transfers are finalized, your credited and debited amounts are made permanent.

COMMIT Syntax

πŸ“š Transaction States in DBMS Explained | Active to Terminated (Simple Notes)
πŸ“š Transaction States in DBMS Explained | Active to Terminated (Simple Notes)

The basic syntax for the COMMIT command is:

COMMIT;

Optionally, you can provide a qualitative explanation:

COMMIT AND CHAIN; -- This command also chains to the next transaction.

COMMIT Example

the four different types of commands are shown in this graphic above it is an image of how to use them
the four different types of commands are shown in this graphic above it is an image of how to use them

Here's an example using a simple banking table:

AccountIDBalance
11000
2500

Let's transfer $200 from Account 2 to Account 1:

All essential aspects of SQL server transaction
All essential aspects of SQL server transaction

START TRANSACTION; ... DEBIT Account 2 by 200; ... CREDIT Account 1 by 200; ... COMMIT;

A Look into ROLLBACK in SQL

ROLLBACK is a SQL command that discards all changes made in the current transaction and restores the database to its state from before the transaction began.

SQL Commads
SQL Commads
a screenshot of a web page with the text,'save all or nothing '
a screenshot of a web page with the text,'save all or nothing '
SQL UPDATE Statement (Transact SQL) - Essential SQL
SQL UPDATE Statement (Transact SQL) - Essential SQL
SQL in 60 days
SQL in 60 days
7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
2.4K views Β· 102 reactions | 🎯 SQL Cheatsheet – Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet πŸš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
2.4K views Β· 102 reactions | 🎯 SQL Cheatsheet – Everything You Need at a Glance Master SQL fundamentals with this clean and powerful SQL Cheatsheet πŸš€ Perfect for beginners, developers, data analysts, and inte | Codewithprashantt
Save This SQL Commands Cheat Sheet Before Your DBMS Exam! πŸ’»πŸ“š
Save This SQL Commands Cheat Sheet Before Your DBMS Exam! πŸ’»πŸ“š
SQL Joins Explained with Examples | Beginner SQL Guide
SQL Joins Explained with Examples | Beginner SQL Guide
an image of a diagram with the words,'the sq circle'in different languages
an image of a diagram with the words,'the sq circle'in different languages
Learn SQL Fast πŸš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast πŸš€ | Beginner SQL Commands Cheat Sheet for Data Analysts
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
SQL Basics Tutorial for Beginners – Structured Query Language Commands, Syntax & Examples Guide
an info poster showing the different types of web pages and how they are used to create them
an info poster showing the different types of web pages and how they are used to create them
7 SQL Commands Every Beginner Must Know
7 SQL Commands Every Beginner Must Know
SQL JOINS Made Easy πŸ”— | INNER, LEFT, RIGHT & FULL JOIN with Examples
SQL JOINS Made Easy πŸ”— | INNER, LEFT, RIGHT & FULL JOIN with Examples
Transactional Replication : How to configuration in SQL Server 2008
Transactional Replication : How to configuration in SQL Server 2008
CQF - SQL
CQF - SQL
four different types of soljoins with the names and numbers in each section on them
four different types of soljoins with the names and numbers in each section on them
Learn SQL just by investing 30 minutes a day within 30 Days
Learn SQL just by investing 30 minutes a day within 30 Days
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide
Complete SQL Cheat Sheet for Beginners | SQL Commands, Joins, Queries & Database Guide

ROLLBACK is like declining the bank transfer when the receiving amount is less than the sending amount. Similarly, in SQL, if any error occurs during a transaction, ROLLBACK Reverts all those changes to maintain data integrity.

ROLLBACK Syntax

The syntax is quite simple:

ROLLBACK;

ROLLBACK Example

Let's try to transfer $600 from Account 2 to Account 1, knowing that Account 2 only has $500. The transaction will fail due to insufficient funds:

START TRANSACTION; ... DEBIT Account 2 by 600; ... CREDIT Account 1 by 600; ... ERROR: Insufficient funds; ... ROLLBACK;

After a ROLLBACK, our banking table will remain unchanged:

AccountIDBalance
11000
2500

Understanding SQL commits and rollbacks is critical for managing database transactions. It ensures data accuracy, consistency, and securityβ€”protecting your data from incomplete or erroneous operations.

Related Articles

Sql Pivot Syntax Examples Sql Server Create Table Primary Key Example Create Table Sql With Identity Column Sql Max Date Group By W3 Sql Class How Many Days It Take To Learn Sql Create Table Sql With Select Statement Pivot Example In Sql Server W3schools Sql Server Create Table Script Example How Long To Learn Sql For A Job Reddit