
Consistency
Today I explored how databases maintain correct data using consistency rules, especially in a wallet system like PhonePe or GPay. I used the same accounts table where Alice has 1000 and Bob has 500. One important thing in this table is the condition that balance should never be negative. This rule is already defined in the table using a check condition. First, I tried a normal update where I deduct a valid amount. UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice'; This worked fine because Alice still had a positive balance. Then I tried something wrong. I attempted to deduct more money than Alice has. UPDATE accounts SET balance = balance - 2000 WHERE name = 'Alice'; This time, PostgreSQL did not allow the update. It gave an error because the balance would become negative. This showed that the database itself is enforcing the rule. Next, I tried directly setting a negative balance. UPDATE accounts SET balance = -100 WHERE name = 'Bob'; Again, the database rejected the qu
Continue reading on Dev.to
Opens in a new tab




