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

Commit and Rollback Operations

Understand how COMMIT finalizes changes and ROLLBACK undoes operations to maintain atomicity.

Commit and Rollback Operations

You'll learn how COMMIT finalizes changes and ROLLBACK undoes operations to maintain atomicity in database transactions.

What Are COMMIT and ROLLBACK?

In the previous lesson, you learned that atomicity means a transaction must execute completely or not at all. COMMIT and ROLLBACK are the two operations that make this possible.

COMMIT is the command that tells the database: "I'm done with this transaction, and everything worked correctly. Make all these changes permanent." Once committed, changes are saved to disk and visible to other users.

ROLLBACK is the opposite—it tells the database: "Something went wrong. Undo everything I've done in this transaction and return to the state before it started." This is your safety net when errors occur.

The Transaction Lifecycle

Imagine you're writing in pencil with an eraser nearby:

  1. BEGIN TRANSACTION: Start writing your draft
  2. Execute operations: Write your changes (INSERT, UPDATE, DELETE)
  3. Decision point:
    • Everything looks good? COMMIT (trace over in permanent ink)
    • Made a mistake? ROLLBACK (erase everything and start fresh)

When to Use Each

Use COMMIT when:

  • All operations completed successfully
  • Data validation passed
  • Business rules are satisfied

Use ROLLBACK when:

  • An error occurs mid-transaction
  • A constraint is violated
  • You detect inconsistent data
  • The user cancels the operation

Key Takeaway: COMMIT makes transaction changes permanent and visible to others, while ROLLBACK cancels all changes and restores the database to its pre-transaction state—together they enforce atomicity by ensuring transactions complete fully or not at all.