statement

CREATE TRIGGER

Registers automatic behavior after an event. PostgreSQL trigger functions can introduce side effects that should be tested. Example target: PostgreSQL 17. Use only the isolated starter dataset; setup and expected results are in the batch evidence.

Formal Syntax & Structure

sql syntaxsql
CREATE TRIGGER name AFTER INSERT ON table FOR EACH ROW EXECUTE FUNCTION function_name()

Working Example

Example Snippetsql
CREATE TABLE audit(id INTEGER); CREATE FUNCTION lab_audit() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN INSERT INTO audit VALUES (NEW.id); RETURN NEW; END; $$;CREATE TRIGGER lab_trigger AFTER INSERT ON customers FOR EACH ROW EXECUTE FUNCTION lab_audit();
SQL Reference