Back to articles
Consistency

Consistency

via Dev.to BeginnersSharmila devi

When I build a wallet system like PhonePe or GPay, I make sure the data is always correct. This is called Consistency in ACID. What Consistency Means Consistency means: Data should always follow rules No invalid data should be stored My Table CREATE TABLE accounts ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, balance INT NOT NULL CHECK (balance >= 0), last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); Here, I added: CHECK (balance >= 0) → balance should never be negative Dummy Data: INSERT INTO accounts (name, balance) VALUES ('Alice', 1000), ('Bob', 500); Testing the Rules Try to make balance negative UPDATE accounts SET balance = -100 WHERE name = 'Alice'; This will fail Reason: database rule (CHECK constraint) Try to deduct more money UPDATE accounts SET balance = balance - 2000 WHERE name = 'Alice'; This will also fail Because balance becomes negative What I Understand Database itself blocks wrong data It keeps balance valid This is Consistency Simple Idea Database ensures rules Da

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
7 views

Related Articles