postgresql trigger example
Example 1: example of trigger in postgresql
CREATE or REPLACE FUNCTION increase_count()
RETURNS TRIGGER
AS
$$
BEGIN
UPDATE donor SET dcount = dcount + 1 WHERE did = NEW.did;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER update_donation_count AFTER INSERT ON donation
FOR EACH ROW
EXECUTE PROCEDURE increase_count();
Example 2: trigger in postgresql to change incoming entry
CREATE or REPLACE FUNCTION func()
RETURNS TRIGGER
AS $$
BEGIN
NEW.columnname = 0;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER change_incoming BEFORE INSERT on Tablename
FOR EACH ROW EXECUTE PROCEDURE func();
Example 3: how to drop a trigger in postgresql
DROP TRIGGER [IF EXISTS] trigger-name
ON table-name [ CASCADE | RESTRICT ];
Example 4: postgres trigger insert into another table
CREATE TRIGGER trig_copy
AFTER INSERT ON table1
FOR EACH ROW
EXECUTE PROCEDURE function_copy();