-
-
Save dwlince/af962df29493c1cbd039114a8554435d to your computer and use it in GitHub Desktop.
Disparadores (Triggers) En PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE emp ( | |
empname text NOT NULL, | |
salary integer | |
); | |
CREATE TABLE emp_audit( | |
operation char(1) NOT NULL, | |
stamp timestamp NOT NULL, | |
userid text NOT NULL, | |
empname text NOT NULL, | |
salary integer | |
); | |
CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS TRIGGER AS $emp_audit$ | |
BEGIN | |
-- | |
-- Create a row in emp_audit to reflect the operation performed on emp, | |
-- make use of the special variable TG_OP to work out the operation. | |
-- | |
IF (TG_OP = 'DELETE') THEN | |
INSERT INTO emp_audit SELECT 'D', now(), user, OLD.*; | |
RETURN OLD; | |
ELSIF (TG_OP = 'UPDATE') THEN | |
INSERT INTO emp_audit SELECT 'U', now(), user, NEW.*; | |
RETURN NEW; | |
ELSIF (TG_OP = 'INSERT') THEN | |
INSERT INTO emp_audit SELECT 'I', now(), user, NEW.*; | |
RETURN NEW; | |
END IF; | |
RETURN NULL; -- result is ignored since this is an AFTER trigger | |
END; | |
$emp_audit$ LANGUAGE plpgsql; | |
CREATE TRIGGER emp_audit | |
AFTER INSERT OR UPDATE OR DELETE ON emp | |
FOR EACH ROW EXECUTE PROCEDURE process_emp_audit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment