I want to create a trigger in PostgreSQL.
I have a products table and a categories table. If a new tuple is inserted into the products table with a category_id, which does not exist yet, I want to create a category with this exact category_id. I wrote some SQL code but I get a syntax error at "INSERT" at line 6.
My code is the following:
CREATE TRIGGER ProductTrig
AFTER INSERT ON products
REFERENCING NEW ROW AS newrow
FOR EACH ROW
WHEN (newrow.category_id NOT IN (SELECT category_id FROM categories))
INSERT INTO categories (category_id) VALUES (newrow.category_id);
Does anybody see the problem? Thanks for your help!