Bird
0
0

You want to create a trigger that logs changes to a salary column only when the salary is updated to a higher value. Which trigger condition and record access correctly implements this in PostgreSQL?

hard📝 Application Q15 of 15
PostgreSQL - Triggers in PostgreSQL
You want to create a trigger that logs changes to a salary column only when the salary is updated to a higher value. Which trigger condition and record access correctly implements this in PostgreSQL?
AIF OLD.salary > NEW.salary THEN INSERT INTO log_table VALUES (OLD.id, OLD.salary); END IF;
BIF NEW.salary < OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
CIF NEW.salary = OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
DIF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition for logging

    The trigger should log only when the new salary is greater than the old salary, so the condition is NEW.salary > OLD.salary.
  2. Step 2: Use correct record variables for UPDATE

    The new salary and id come from NEW because the row is updated with new values.
  3. Final Answer:

    IF NEW.salary > OLD.salary THEN INSERT INTO log_table VALUES (NEW.id, NEW.salary); END IF; -> Option D
  4. Quick Check:

    Log when NEW > OLD salary = B [OK]
Quick Trick: Compare NEW and OLD to detect increases, then log NEW data [OK]
Common Mistakes:
  • Reversing NEW and OLD in condition
  • Logging when salary decreases
  • Using equality instead of greater than

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes