Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Difference Between "git commit" and "git push"?
Next article icon

Difference Between COMMIT and ROLLBACK in SQL

Last Updated : 19 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, transaction control is essential for managing changes in a database effectively. COMMIT and ROLLBACK are two crucial Transaction Control Language (TCL) commands that help maintain data integrity and consistency. While COMMIT ensures that all changes in a transaction are permanently saved, ROLLBACK provides a mechanism to undo changes when something goes wrong.

Understanding the differences between these commands is critical for database administrators, developers, and anyone working with transactional data to ensure reliable and error-free database operations. Proper usage of these commands ensures reliable and error-free database operations while maintaining data stability.

Difference Between COMMIT and ROLLBACK

The table below highlights the key differences between COMMIT and ROLLBACK in SQL:

Feature COMMIT ROLLBACK
Function Permanently saves changes made by the current transaction. Undoes changes made by the current transaction.
Undo Capability Cannot undo changes after execution. Reverts the database to its previous state before the transaction.
When Applied Used when the transaction is successfully completed. Used when the transaction fails, is incorrect, or aborted.
Data Integrity Ensures that changes are saved permanently. Ensures that errors do not affect the database by undoing partial changes.
Syntax COMMIT; ROLLBACK;
Error Handling No changes are rolled back even if errors occur after the COMMIT statement. Automatically undoes uncommitted changes in case of errors or failures.

Understanding COMMIT and ROLLBACK with Examples

A sample table is created to demonstrate how COMMIT and ROLLBACK work. This table will be used for all examples to maintain consistency. Consider the following STAFF table with records:

STAFF Table:

1.COMMIT

COMMIT in SQL is a transaction control language that is used to permanently save all changes made during the current transaction. After executing a COMMIT statement, the changes are irreversible, and the database cannot revert to its previous state. The COMMIT command ensures that the changes made during the transaction are reflected in the database.

Syntax:

COMMIT;

Example:

In the following example, we will demonstrate how to use the COMMIT command to permanently save changes to a table. This ensures that any modifications made to the data are saved and cannot be undone. Select records where Allowance equals 400.

Query:

SELECT *
FROM Staff
WHERE Allowance = 400;

sql> COMMIT;

Output:

Explanation:

  • The UPDATE statement modifies the Allowance value for employees with an initial value of 400.
  • After executing the COMMIT statement, any changes made during the transaction are saved permanently.

2. ROLLBACK

ROLLBACK in SQL is a transactional control language that is used to undo changes made during the current transaction that have not yet been committed. This command is particularly useful when errors occur, or the transaction is aborted. The ROLLBACK command ensures that the database returns to its previous state by undoing uncommitted changes.

Syntax:

ROLLBACK;

Example:

In this example, we will use the ROLLBACK command to demonstrate how to revert changes to a table. This ensures that any errors or unintended modifications are undone, restoring the database to its previous state. Update Allowance for employees with an Allowance of 300.

Query:

SELECT *
FROM EMPLOYEES
WHERE ALLOWANCE = 400;

ROLLBACK;

Output:

Explanation:

  • The UPDATE statement modifies the Allowance value for employees with an initial value of 300
  • The ROLLBACK statement undoes all uncommitted changes, restoring the Staff table to its state before the transaction.

When to Use COMMIT and ROLLBACK

  • Use COMMIT when all operations in a transaction are successful and we want to save changes permanently.
  • Use ROLLBACK when an error occurs, or the transaction needs to be aborted, ensuring the database remains unaffected by partial or incorrect updates.

Conclusion

Understanding the roles of COMMIT and ROLLBACK is essential for effective transaction management in SQL. Use COMMIT to save changes permanently after a successful transaction and ROLLBACK to revert changes in case of errors. By applying these commands correctly, we can maintain the stability and integrity of our database. Mastering these commands allows developers and administrators to confidently handle complex transactional workflows and prevent data corruption.



Next Article
Difference Between "git commit" and "git push"?

D

dhruv5819
Improve
Article Tags :
  • Databases
  • DBMS
  • Difference Between
  • GATE CS
  • SQL
  • SQL-Clauses

Similar Reads

  • Difference Between "git commit" and "git push"?
    Git commit and git push are two essential commands you'll use a lot when working with Git. even their frequent use together, they have different functions. In order to help you understand when and how to use these two commands effectively in your version control workflow, this article will break dow
    2 min read
  • Difference between View and Cursor in SQL
    1. View : A view is a virtual table that not actually exist in the database but it can be produced upon request by a particular user. A view is an object that gives the user a logical view of data from a base table we can restrict to what user can view by allowing them to see an only necessary colum
    3 min read
  • Difference between SQL and T-SQL
    SQL (Structured Query Language) is the standard language for managing and manipulating relational databases, enabling operations like querying, updating, and deleting data. T-SQL (Transact-SQL), an extension of SQL developed by Microsoft, adds advanced features and procedural capabilities specifical
    4 min read
  • Difference between ALTER and UPDATE Command in SQL
    1. ALTER Command: ALTER SQL command is a DDL (Data Definition Language) statement. ALTER is used to update the structure of the table in the database (like add, delete, modify the attributes of the tables in the database). Syntax: // add a column to the existing table ALTER TABLE tableName ADD colum
    3 min read
  • Difference Between DELETE and DROP in SQL
    In SQL, the DELETE and DROP commands are essential for managing data in a database, but they serve different purposes. While both are used to remove data, their functionality varies significantly. The DELETE command is designed to remove specific rows (tuples) or all rows from a table while preservi
    4 min read
  • Difference between IT Chargeback and Showback
    In this article, we will know what is the difference between IT Chargeback and Showback, and their advantages and disadvantages. 1. IT Chargeback :An IT chargeback system works with an accounting strategy that deals with costs of IT services hardware or software to the business unit which is used by
    3 min read
  • Difference between Grant and Revoke
    Data Controlling Language (DCL) helps users to retrieve and modify the data stored in the database with some specified queries. Grant and Revoke belong to these types of commands of the Data controlling Language. DCL is a component of SQL commands. 1. Grant : SQL Grant command is specifically used t
    2 min read
  • Difference between DDL and TCL
    Prerequisite – SQL Commands 1 Data Definition Language (DDL) is a set of SQL (Structured Query Language) commands used to create, modify, and delete database objects such as tables, indexes, views, and constraints. DDL statements are used to define the schema or structure of a database, including it
    2 min read
  • Difference Between DML and TCL
    Data Manipulation Language (DML) and Transaction Control Language (TCL) are critical subsets of SQL (Structured Query Language). Both play essential roles in managing and controlling data in a database, but they serve different purposes. In this article, we will explore about the Difference between
    4 min read
  • Difference Between Trigger and Procedure in DBMS
    In Relational Database Management Systems (RDBMS), triggers and procedures are two important elements necessary for data manipulation. Still, there are vast differences between them, although many individuals cannot tell the difference because their uses are quite similar. On the other hand, trigger
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences