Course contentsShow
← Data Science
Lesson 1110 of 2,14528. Working with Databases from PythonFree lesson

What Are Database Transactions?

Learn the definition of transactions, ACID properties, and why transactions matter for data integrity.

What Are Database Transactions?

You'll learn what database transactions are, why they're essential for maintaining data integrity, and the fundamental ACID properties that govern them.

What is a Transaction?

A transaction is a logical unit of work that groups one or more database operations together. Think of it like a package deal: either all the operations complete successfully, or none of them do. There's no in-between state.

Imagine transferring money between bank accounts. You need to:

  1. Subtract $100 from Account A
  2. Add $100 to Account B

If the database crashes after step 1 but before step 2, you'd lose $100! A transaction ensures both steps happen together or neither happens at all.

The ACID Properties

Transactions follow four critical properties, known by the acronym ACID:

  • Atomicity: All operations in a transaction succeed or all fail—no partial completion
  • Consistency: Transactions move the database from one valid state to another, respecting all constraints
  • Isolation: Concurrent transactions don't interfere with each other
  • Durability: Once committed, changes persist even if the system crashes

Why Transactions Matter for Data Integrity

Without transactions, your data can become inconsistent when:

  • Multiple users modify the same data simultaneously
  • System failures occur mid-operation
  • Complex multi-step operations need to be coordinated

Transactions provide a safety net, ensuring your database remains accurate and trustworthy even when things go wrong. They're especially crucial in data pipelines where you might insert thousands of records—if something fails halfway through, you want the ability to roll back everything cleanly.

Key Takeaway: Transactions bundle database operations into atomic units governed by ACID properties, ensuring data integrity even in the face of errors, crashes, or concurrent access.