Course contentsShow
← Computer Science
Lesson 1655 of 2,87238. Database Transactions and IndexingFree lesson

Atomicity: All-or-Nothing Execution

Learn how atomicity ensures transactions either complete fully or have no effect, preventing partial updates.

Atomicity: All-or-Nothing Execution

You'll learn what atomicity means for database transactions and why it's essential for data integrity.

What Is Atomicity?

Atomicity is one of the core guarantees of database transactions. It ensures that a transaction either completes entirely or has no effect at all—there's no middle ground. Think of it as an "all-or-nothing" promise.

Why Atomicity Matters

Imagine you're transferring $100 from your checking account to your savings account. This involves two steps:

  1. Subtract $100 from checking
  2. Add $100 to savings

What happens if the database crashes after step 1 but before step 2? Without atomicity, you'd lose $100! Atomicity prevents this disaster by treating both operations as a single, indivisible unit.

The Real-World Analogy

Think of atomicity like a light switch. You can't have a light be "half on." It's either fully on or fully off. Similarly, a transaction is either fully committed (all changes saved) or fully rolled back (all changes undone).

How It Works

When you begin a transaction, the database tracks all changes in a temporary space. If everything succeeds, the database commits the changes, making them permanent. If anything fails (error, crash, power outage), the database rolls back all changes, restoring the original state as if nothing happened.

This prevents partial updates—the dangerous situation where only some of your intended changes take effect, leaving your data in an inconsistent, corrupted state.

Key Takeaway: Atomicity guarantees that transactions are indivisible units of work—they either execute completely and successfully, or they leave the database unchanged, protecting your data from partial updates and maintaining consistency even during failures.