15.7 KB · commit 40bb182
← Previous: T09.1: ACID Properties of Transactions | 🏠 Index | Next: T09.3: Transaction States and Life-Cycle Diagram →
Topic: T09: Transactions, ACID & Anomalies | Priority: 🟡 Medium Priority | Exam Frequency: 2/7 finals (6 marks: 2019 Q.3(b), 2024 Q.2(c))
Sources: Silberschatz 7e Ch. 17 (§17.1–§17.2, pp. 800–802) · Lecture Deck 07 (Slide 6) · Raidah Class Notes (p. 26)
While exams often test all four ACID properties together, the consistency property was singled out as a standalone 3-mark question in both 2019 and 2024. Examiners want to see if you understand the difference between explicit database schema rules and implicit application rules. To earn full marks, you need to explain why a transaction is allowed to pass through a temporarily inconsistent state while it runs, and why keeping the database consistent is mainly the job of the application programmer rather than the DBMS engine alone.
Consistency: Consistency guarantees that running a transaction in isolation on a consistent database leaves the database consistent when it finishes. Even if the database passes through a temporarily inconsistent state during execution, all rules must hold again at commit.
Consistency Requirement: A transaction must preserve both explicit SQL schema constraints and implicit application business rules. The DBMS automatically checks explicit rules like primary keys, foreign keys, and
CHECKconstraints. The application programmer is responsible for writing transaction code that preserves business invariants, such as keeping total money constant during a transfer.
Think of database consistency like accounting rules in a real bank. Some rules are simple form checks, like making sure an account number exists or a balance never drops below zero. The database software can check those automatically. However, other rules depend on the meaning of the business action itself. For example, when you transfer money between two accounts, every dollar deducted from one account must show up in the other. Because the database engine does not know whether your SQL code is meant to be a transfer, a fee, or a deposit, the programmer writing the transaction has to make sure the math balances out.
In a relational database, a state is consistent when every stored value obeys all declared schema constraints and all real-world business rules (invariants).
Database consistency works across two distinct levels:
Explicit Integrity Constraints (Enforced by the DBMS): These are formal rules written directly into the SQL schema using DDL statements. Whenever a query breaks one of these rules, the DBMS engine immediately stops the operation and throws an error. Common examples include:
PRIMARY KEY).FOREIGN KEY ... REFERENCES).NOT NULL, VARCHAR(20)).CHECK (balance >= 0) or CHECK (status IN ('active', 'closed')).Implicit Application Invariants (Enforced by the Programmer): These are real-world business rules that are too complex to express with basic table constraints. Because the DBMS engine does not understand the business logic behind arbitrary formulas, the application programmer must write transaction code that preserves them. Common examples include:
| Constraint Type | Description | Who Enforces It? | What Happens on Violation? |
|---|---|---|---|
| Explicit Constraints | Schema-level rules (PK, FK, CHECK, NOT NULL) | DBMS Engine | DBMS rejects the statement or aborts the transaction automatically |
| Implicit Invariants | Business rules and multi-table math invariants | Application Programmer | Wrong data gets committed because the DBMS cannot spot the logic bug |
A common misconception is that a database must stay consistent at every single microsecond. In reality, a transaction almost always passes through a temporarily inconsistent state while it executes.
Why does this happen? A computer executes instructions one step at a time. When a transaction updates two related pieces of data, there is always a brief moment after the first update finishes where the second update has not happened yet.
Suppose a warehouse tracks 100 laptops across two counters: Available_Stock (S=100) and Reserved_Stock (R=0). The business invariant requires the total physical count to remain constant:
Now suppose transaction T1 reserves 5 laptops for a customer order:
1.2.read(S),S:=S−5,write(S)[S=95]read(R),R:=R+5,write(R)[R=5]Look at the database state right after Step 1 finishes, before Step 2 runs:
| Point in Time | Available_Stock (S) | Reserved_Stock (R) | Total (S+R) | Database Status |
|---|---|---|---|---|
| Before Step 1 | 100 | 0 | 100 | Consistent (Initial state) |
| Between Step 1 & Step 2 | 95 | 0 | 95 | Temporarily Inconsistent (5 laptops appear missing!) |
| After Step 2 | 95 | 5 | 100 | Consistent (Final state restored) |
Right between Step 1 and Step 2, the total count is 95 instead of 100, breaking the invariant S+R=100. Even so, this temporary inconsistency is completely safe because two other ACID properties protect the transaction during that gap:
Because of this partnership, the consistency rule simply states: if the database is consistent when a transaction starts, and the transaction runs to completion by itself, the database will be consistent again when the transaction finishes.
The table below lists past appearances of the standalone consistency question:
| Year & Question | Marks | Format / Focus | Key Elements Expected in Solution |
|---|---|---|---|
| 2019 Q.3(b) | 3 | Standalone explanation with example | Formal definition, explicit vs. implicit rules, $50 bank transfer walkthrough. |
| 2024 Q.2(c) | 3 | Standalone explanation with example | Exact repeat of 2019 Q.3(b). Requires the same definition and step-by-step trace. |
Appeared: 2019 Q.3(b), 2024 Q.2(c) (3 marks)
Question:
Explain the consistency property of a database system with a proper example.
Full Answer:
The consistency property requires that running a transaction in isolation (without interference from other concurrent transactions) preserves the consistency of the database. If the database starts in a valid state that obeys all integrity constraints and business rules, it must also end in a valid state once the transaction finishes.
Consistency covers two types of rules:
PRIMARY KEY uniqueness, FOREIGN KEY referential integrity, and CHECK conditions (like balance >= 0).Consider a banking database with two accounts, A and B, holding initial balances of $1000 and $2000.
The consistency invariant requires the sum of both balances to remain constant before and after the transfer:
A+B=1000+2000=3000Transaction T1 transfers $50 from account A to account B through six steps:
1.2.3.4.5.6.7.read(A)A:=A−50write(A)[A=950]read(B)B:=B+50write(B)[B=2050]commitTemporary inconsistency inside an active transaction is completely normal because instructions execute one at a time. The consistency property guarantees that once the transaction commits, all invariants hold again. If a crash happens halfway through, atomicity rolls back the partial changes and returns the database to its initial consistent state.
Source: Silberschatz 7e · Chapter 17 (Exercise 17.13)
Exam Relevance: High-Probability Foundational Theory Drill
Key Tested Concept: Invariant preservation, intermediate state inconsistency, and developer vs DBMS responsibility.
Problem Statement:
Consider a fund transfer transaction T1 that moves $50 from bank account A to account B:
T1:read(A);A:=A−50;write(A);read(B);B:=B+50;write(B)
- Formulate the explicit database consistency requirement (invariant) for this system.
- Identify the exact intermediate step during execution where the database enters an inconsistent state.
- Explain why the database system cannot automatically determine whether an arbitrary transaction preserves consistency, and distinguish the application programmer's role from the DBMS's role.
Model Exam-Style Solution:
💡 RUET Exam Takeaway: Always emphasize that Consistency is primarily the programmer's responsibility, while the DBMS provides the Atomicity and Isolation mechanisms that protect this consistency.
PK, FK, CHECK), while the application programmer is responsible for implicit business invariants.← Previous: T09.1: ACID Properties of Transactions | 🏠 Index | Next: T09.3: Transaction States and Life-Cycle Diagram →