Sub topic 4
Consistency: Constraints and Valid States
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, plus tables, primary keys, and constraints. You should also understand that multiple users or processes can run database statements at the same time.
Consistency means that a transaction changes the database from one valid state to another valid state. A valid state satisfies the rules defined by the database, such as primary keys, foreign keys, NOT NULL, CHECK, and UNIQUE constraints.
Consistency does not mean that every business rule is automatically enforced. The database can enforce only rules that you define through constraints, triggers, or other database logic. Application code may need to enforce rules that cross several systems or depend on external information.
Constraints define valid states
Consider an accounts table:
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, balance)
VALUES (1, 100.00), (2, 50.00);This table defines several rules:
- Every account must have an
id. - No two accounts can have the same
id. - Every account must have a balance.
- A balance cannot be negative.
A transaction that violates one of these rules cannot create a committed invalid state.
BEGIN;
UPDATE accounts
SET balance = balance - 500
WHERE id = 1;
-- The CHECK constraint rejects the negative balance.
ROLLBACK;
SELECT balance
FROM accounts
WHERE id = 1;
-- 100.00In PostgreSQL, the failed statement places the transaction in an aborted state. You must roll it back before issuing other normal statements in that transaction. Other database systems may report the error differently, but the important result is the same: the invalid change must not be committed.
Constraints across tables
A foreign key is a rule connecting rows in different tables. For example, an order may refer only to an existing customer:
CREATE TABLE customers (
id integer PRIMARY KEY
);
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer NOT NULL REFERENCES customers(id)
);The database rejects an order whose customer_id does not exist. It can also reject deleting a customer that existing orders still reference, depending on the configured foreign-key action.
Business invariants
An invariant is a condition that must remain true. “An account balance is never negative” is an invariant enforced by a CHECK constraint. “At least one doctor must remain on call” is also an invariant, but checking it may require reading several rows. A simple row constraint cannot express that rule.
Transactions help preserve multi-row invariants by grouping the related reads and writes. The appropriate isolation level or locking strategy may also be required when concurrent transactions can change those rows.
Consistency during a transaction
A transaction can temporarily perform steps that would look incomplete if viewed alone. For example, a transfer may subtract money from one account before adding it to another. Atomicity ensures that other transactions do not observe a committed half-transfer, while consistency ensures that the final committed state satisfies its constraints.
Consistency is therefore a result of constraints, transaction boundaries, and concurrency control working together. It is not a promise that every statement is independently meaningful.
Exercise
An UPDATE would make one account's balance negative, but a later statement in the same transaction would restore it. Should the transaction be allowed to continue automatically?
Show answer
No. The constraint violation must be handled explicitly. In PostgreSQL, roll back the transaction or roll back to a savepoint before continuing.