Tutorials  /  Databases

SQL Commit and Rollback Guide

Ccentron Redaktion · January 2024 ·4 min read ·Databases, Tutorial

The most important aspect of a database is the ability to store data and the ability to manipulate data. COMMIT and ROLLBACK are two such keywords which are used in order to store and revert the process of data storage. These keywords are usually used in context with a transaction. Let’s try to understand the details about COMMIT and ROLLBACK.

SQL Commit and Rollback

COMMIT and ROLLBACK are performed on transactions. A transaction is the smallest unit of work that is performed against a database. It's a sequence of instructions in a logical order. A transaction can be performed manually by a programmer or it can be triggered using an automated program.

DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

SQL Commit

COMMIT is the SQL command that is used for storing changes performed by a transaction. When a COMMIT command is issued it saves all the changes since last COMMIT or ROLLBACK.

Syntax for SQL Commit

Code
COMMIT;

The syntax for commit includes just one keyword COMMIT.

SQL Commit Example

Let us consider the following table for understanding Commit in a better way. Customer:

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA

Now let us delete one row from the above table where State is “Texas”.

SQL
DELETE from Customer where State = 'Texas';

SQL Commit

Post the DELETE command if we will not publish COMMIT, and if the session is closed then the change that is made due to the DELETE command will be lost. Updated Command with COMMIT

SQL
DELETE from Customer where State = 'Texas';

COMMIT;

SQL transaction commit

Using the above-mentioned command sequence will ensure that the change post DELETE command will be saved successfully.

Output After Commit

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA

SQL RollBack

ROLLBACK is the SQL command that is used for reverting changes performed by a transaction. When a ROLLBACK command is issued it reverts all the changes since last COMMIT or ROLLBACK.

Syntax for SQL Rollback

Code
ROLLBACK;

The syntax for rollback includes just one keyword ROLLBACK.

SQL Rollback Example

Let us consider the following table for understanding Rollback in a better way. Customer:

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA

Now let us delete one row from the above table where State is “Texas”.

SQL
DELETE from Customer where State = 'Texas';

Post the DELETE command if we publish ROLLBACK it will revert the change that is performed due to the delete command. Updated Command with ROLLBACK

SQL
DELETE from Customer where State = 'Texas';

ROLLBACK;

Using the above-mentioned command sequence will ensure that the change post DELETE command will be reverted successfully.

Output After Rollback

CUSTOMER ID CUSTOMER NAME STATE COUNTRY
1 Akash Delhi India
2 Amit Hyderabad India
3 Jason California USA
4 John Texas USA

SQL Commit and Rollback Guide

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren