I m a new comer to postgresql and database part. I wish to check function exit or otherwise in postgresql using trigger before place statement. any body understand how to create trigger this?
Produce a special trigger function:
CREATE OR REPLACE FUNCTION some_function() RETURNS trigger AS $$ BEGIN IF some_condition THEN RAISE EXCEPTION 'Some message for id %', NEW.id; END IF; RETURN NEW; END; $$ LANGUAGE plpgsql;
Then define a trigger to fireplace it
CREATE TRIGGER some_trigger BEFORE INSERT OR UPDATE ON some_table FOR EACH ROW EXECUTE PROCEDURE some_function();
Once the exception is elevated, the update will fail.
The SQL performed might be or arbitrary complexity, including any query or function you want. This may also alter the NEW values via
SET NEW.some_column = some_value, as well as update other tables.
NEW.id is definitely an example column only. Go to whichever column you want in the NEW row (or none) within the message.
When the check is localized towards the row, get a column constraint, eg:
CREATE TABLE my_table ( some_column int CHECK (some_column between 0 and 10), other_column int CHECK (other_column < some_column) )