Bird
0
0

Consider this trigger function:

medium📝 query result Q5 of 15
PostgreSQL - Triggers in PostgreSQL
Consider this trigger function:
CREATE FUNCTION check_salary() RETURNS trigger AS $$ BEGIN IF NEW.salary <= 0 THEN RAISE EXCEPTION 'Salary must be positive'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
What will happen if you update a row setting salary = 0?
AThe update succeeds and salary is set to 0
BThe update fails with an error
CThe salary is set to 1 automatically
DThe trigger does not run on updates
Step-by-Step Solution
Solution:
  1. Step 1: Understand the trigger condition

    The trigger raises an exception if salary is less than or equal to zero.
  2. Step 2: Apply to salary = 0 update

    Since 0 <= 0, the exception triggers and update fails.
  3. Final Answer:

    The update fails with an error -> Option B
  4. Quick Check:

    Trigger blocks invalid salary update [OK]
Quick Trick: Triggers can block invalid updates by raising exceptions [OK]
Common Mistakes:
  • Assuming update succeeds
  • Thinking trigger only runs on insert
  • Believing salary auto-corrects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes