Bird
0
0

In a trigger function, you want to prevent updates that decrease the balance column. Which code snippet correctly enforces this?

hard📝 Application Q9 of 15
PostgreSQL - Triggers in PostgreSQL
In a trigger function, you want to prevent updates that decrease the balance column. Which code snippet correctly enforces this?
AIF NEW.balance > OLD.balance THEN RAISE EXCEPTION 'Balance cannot decrease'; END IF;
BIF NEW.balance = OLD.balance THEN RAISE EXCEPTION 'Balance cannot decrease'; END IF;
CIF OLD.balance < NEW.balance THEN RAISE EXCEPTION 'Balance cannot decrease'; END IF;
DIF NEW.balance < OLD.balance THEN RAISE EXCEPTION 'Balance cannot decrease'; END IF;
Step-by-Step Solution
Solution:
  1. Step 1: Identify condition to block

    We want to block if new balance is less than old balance (decrease).
  2. Step 2: Write correct IF condition

    IF NEW.balance < OLD.balance THEN raises exception to prevent decrease.
  3. Final Answer:

    IF NEW.balance < OLD.balance THEN RAISE EXCEPTION 'Balance cannot decrease'; END IF; -> Option D
  4. Quick Check:

    Block if NEW less than OLD [OK]
Quick Trick: Raise exception if NEW value is less than OLD to block decrease [OK]
Common Mistakes:
  • Reversing comparison operators
  • Raising exception on increase
  • Checking equality only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes