postgres create trigger code example

Example 1: example of trigger in postgresql

-- Trigger to update donatiom count in donor table whenever
-- A new donation is made by that person

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: how to drop a trigger in postgresql

DROP TRIGGER [IF EXISTS] trigger-name 
ON table-name [ CASCADE | RESTRICT ];

Tags:

Sql Example