sajad torkamani

In a nutshell

A database transaction is a unit of work performed on a database (e.g., MySQL) that is treated atomically. That means if you wrap the following operations in a transaction:

INSERT INTO users (name) VALUES ('John Doe');
UPDATE users SET email = 'johndoe@gmail.com' WHERE name = 'John Doe';
UPDATE users SET username = 'johndoe' WHERE name = 'John Doe';

Then the database would guarantee that either all three operations succeed or none of them does. So if the first two operations succeed, but the third doesn’t, the database will roll back all the operations.

This is very useful for scenarios where you need to ensure that either a group of operations all succeed or that none of them do to keep your data in a consistent state.