0
0
DbmsConceptBeginner · 3 min read

What is Atomicity in DBMS: Definition and Examples

In a database, atomicity means that a transaction is treated as a single, indivisible unit that either fully happens or does not happen at all. This ensures that partial changes do not occur, keeping the database consistent.
⚙️

How It Works

Atomicity in databases works like a light switch that is either fully on or fully off, with no in-between state. Imagine you are transferring money from one bank account to another. Atomicity ensures that either both the withdrawal and deposit happen together, or neither happens if something goes wrong.

This means the database keeps track of all steps in a transaction and only commits the changes if every step succeeds. If any step fails, the database rolls back all changes, leaving everything as it was before the transaction started. This prevents errors like losing money or duplicating data.

💻

Example

This example shows a simple transaction in SQL that transfers money between two accounts. Atomicity ensures both updates happen together or not at all.

sql
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
Output
If both updates succeed, the transaction commits and balances update; if either update fails, no changes are made.
🎯

When to Use

Use atomicity whenever you need reliable and consistent changes in a database, especially for operations involving multiple steps. Common cases include banking systems, online shopping carts, and booking systems where partial updates could cause serious errors.

Atomicity helps avoid problems like money disappearing, orders being duplicated, or seats being double-booked by ensuring all parts of a transaction complete successfully or none at all.

Key Points

  • Atomicity means a transaction is all-or-nothing.
  • It prevents partial updates that could corrupt data.
  • Rollback restores the database if a transaction fails.
  • Essential for financial, booking, and multi-step operations.

Key Takeaways

Atomicity ensures database transactions complete fully or not at all to keep data consistent.
It prevents partial changes that could cause errors or data corruption.
Rollback is used to undo changes if any part of the transaction fails.
Atomicity is critical for multi-step operations like money transfers and bookings.