Bird
0
0

Given the following trigger and table, what will be the output after inserting a row?

medium📝 query result Q13 of 15
PostgreSQL - Triggers in PostgreSQL
Given the following trigger and table, what will be the output after inserting a row?
CREATE TABLE users(id SERIAL PRIMARY KEY, name TEXT);
CREATE FUNCTION log_insert() RETURNS trigger AS $$
BEGIN
  RAISE NOTICE 'Inserted user: %', NEW.name;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER after_user_insert AFTER INSERT ON users FOR EACH ROW EXECUTE FUNCTION log_insert();

INSERT INTO users(name) VALUES ('Alice');
ANo output, the insert happens silently
BAn error occurs because RETURN NEW is invalid in AFTER trigger
CThe insert is rolled back due to the trigger
DA notice message: 'Inserted user: Alice'
Step-by-Step Solution
Solution:
  1. Step 1: Understand the trigger function behavior

    The function raises a NOTICE with the inserted user's name after insert.
  2. Step 2: Recognize AFTER trigger effects

    AFTER triggers run after the insert, so the notice will be shown, and the insert completes successfully.
  3. Final Answer:

    A notice message: 'Inserted user: Alice' -> Option D
  4. Quick Check:

    AFTER trigger raises notice = A notice message: 'Inserted user: Alice' [OK]
Quick Trick: AFTER triggers can raise notices after insert [OK]
Common Mistakes:
  • Thinking RETURN NEW is invalid in AFTER triggers
  • Expecting no output from the trigger
  • Assuming the insert is rolled back

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes