Course contentsShow
← Software Engineering
Lesson 898 of 1,93328. Transactions and ConsistencyFree lesson

The Consistency Property

Ensuring transactions move the database from one valid state to another, maintaining all defined rules and constraints.

The Consistency Property

You'll learn: How the Consistency property ensures transactions maintain database integrity by respecting all defined rules and constraints.


What Consistency Means

Consistency is the "C" in ACID and guarantees that every transaction moves your database from one valid state to another valid state. It doesn't just mean the transaction completes successfully—it means all the database's rules, constraints, and invariants are preserved throughout and after the transaction.

Think of consistency like a bank vault that requires two keys turned simultaneously to open. If your database rule says "an account balance must never be negative," consistency ensures no transaction can violate that rule, even temporarily. The transaction either fully respects all constraints or doesn't execute at all.

What Gets Enforced

Consistency enforcement covers:

  • Constraints: Primary keys, foreign keys, unique constraints, check constraints
  • Data types: Column type validation
  • Triggers: Business logic that fires on data changes
  • Application-level rules: Custom validations your code implements

How It Works With Atomicity

Consistency works hand-in-hand with atomicity (which you just learned). Atomicity ensures all-or-nothing execution. Consistency ensures that the "all" part results in a valid database state. If a transaction would violate any rule, the database rolls back the entire transaction, keeping the database consistent.

Example Without Code

Imagine transferring $100 between accounts. Consistency means:

  • Both accounts exist (foreign key constraint)
  • Neither balance goes negative (check constraint)
  • The transaction log matches account balances (custom rule)

If any rule fails, the transaction rolls back completely.

Key Takeaway:
Consistency ensures transactions never leave your database in an invalid state—all constraints, rules, and invariants must be satisfied before a transaction commits.