Skip to main content

Why are transactions important for data integrity?

Transactions matter for data integrity because they prevent the database from ending up in an incomplete or contradictory state, if something goes wrong while operations are running.

Example

Say a money transfer between accounts is underway:

sql
BEGIN; UPDATE accounts SET balance = balance - 500 WHERE id = 1; UPDATE accounts SET balance = balance + 500 WHERE id = 2; COMMIT;

If the second operation fails (e.g. due to a network error), without a transaction, the first account decreases while the second never increases, so the money "disappears". With a transaction, everything rolls back, and the data stays consistent.

Why this matters:

  • it prevents data loss or duplication;
  • it protects against partially completed operations;
  • it guarantees the database always reflects the system's real, consistent state.

Summary: Transactions are a mechanism that preserves the logical integrity of data, guaranteeing that any change to the database is either fully applied or fully undone.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.