Bird
0
0

Given this trigger function to prevent negative prices:

medium📝 query result Q13 of 15
PostgreSQL - Triggers in PostgreSQL
Given this trigger function to prevent negative prices:
CREATE FUNCTION check_price() RETURNS trigger AS $$ BEGIN IF NEW.price < 0 THEN RAISE EXCEPTION 'Price cannot be negative'; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
What happens if you try to insert a row with price = -5?
AAn error is raised and the insert is stopped
BThe row is inserted with price -5
CThe price is automatically set to 0
DThe trigger is ignored and insert proceeds
Step-by-Step Solution
Solution:
  1. Step 1: Analyze trigger function logic

    The function checks if NEW.price is less than 0 and raises an exception if true.
  2. Step 2: Understand RAISE EXCEPTION effect

    RAISE EXCEPTION stops the operation and returns an error to the user.
  3. Final Answer:

    An error is raised and the insert is stopped -> Option A
  4. Quick Check:

    Negative price triggers error [OK]
Quick Trick: RAISE EXCEPTION stops insert on invalid data [OK]
Common Mistakes:
  • Assuming data is inserted anyway
  • Thinking price auto-corrects
  • Ignoring trigger effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes