Bird
0
0

You want to create an UPDATE trigger that fires only when the status column changes. Which condition correctly detects this change?

hard📝 Application Q8 of 15
PostgreSQL - Triggers in PostgreSQL
You want to create an UPDATE trigger that fires only when the status column changes. Which condition correctly detects this change?
AIF NEW.status IS DISTINCT FROM OLD.status THEN
BIF NEW.status = OLD.status THEN
CIF NEW.status IS NULL THEN
DIF OLD.status IS NULL THEN
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to detect column changes

    To detect if a column value changed, compare NEW and OLD values.
  2. Step 2: Use IS DISTINCT FROM for NULL-safe comparison

    Using IS DISTINCT FROM handles NULL values correctly, unlike simple equality.
  3. Step 3: Analyze options

    IF NEW.status IS DISTINCT FROM OLD.status THEN correctly checks if NEW.status differs from OLD.status, including NULL cases.
  4. Final Answer:

    IF NEW.status IS DISTINCT FROM OLD.status THEN -> Option A
  5. Quick Check:

    Use IS DISTINCT FROM to detect changes safely [OK]
Quick Trick: Use IS DISTINCT FROM to detect changes including NULLs [OK]
Common Mistakes:
  • Using = instead of IS DISTINCT FROM
  • Checking only for NULL values
  • Confusing OLD and NEW references

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes