What is a database transaction?
16 May 2023 (Updated 20 January 2024)
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.
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment