postgresql数据库创建触发器记录表修改时间
postgresql 数据库创建触发器记录对A表增删改(之前之后或更新时)的详细数据
1、创建function
CREATE OR REPLACE FUNCTION auditlogfunc() RETURNS TRIGGER AS $example_table$
BEGIN
INSERT INTO AUDIT(EMP_ID, ENTRY_DATE) VALUES (new.ID, current_timestamp);
RETURN NEW;
END;
$example_table$ LANGUAGE plpgsql;
2、创建测试表
CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE AUDIT(
EMP_ID INT NOT NULL,
ENTRY_DATE TEXT NOT NULL
);
3、创建trigger
CREATE TRIGGER example_trigger AFTER INSERT ON COMPANY FOR EACH ROW EXECUTE PROCEDURE auditlogfunc();
4、测试
testdb=# INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1000, 'Paul', 32, 'California', 20000.00 );
INSERT 0 1
testdb=# select * from audit ;
emp_id | entry_date
--------+-------------------------------
1000 | 2020-04-14 09:35:32.173252+08
(1 row)