Bird
0
0

What will be the result of a BEFORE UPDATE trigger that sets NEW.updated_at = NOW() on a row update?

medium📝 query result Q5 of 15
PostgreSQL - Triggers in PostgreSQL
What will be the result of a BEFORE UPDATE trigger that sets NEW.updated_at = NOW() on a row update?
AThe updated_at column remains unchanged
BThe updated_at column is set to the current timestamp
CThe update fails with an error
DThe updated_at column is set to NULL
Step-by-Step Solution
Solution:
  1. Step 1: Analyze BEFORE UPDATE trigger behavior

    The trigger modifies the NEW row before the update is applied.
  2. Step 2: Setting NEW.updated_at = NOW()

    This sets the updated_at column to the current timestamp at update time.
  3. Final Answer:

    The updated_at column is set to the current timestamp -> Option B
  4. Quick Check:

    BEFORE UPDATE triggers can set timestamp columns [OK]
Quick Trick: Use BEFORE UPDATE to set timestamps automatically [OK]
Common Mistakes:
  • Assuming updated_at does not change
  • Expecting an error on update
  • Thinking updated_at becomes NULL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes