經過觸發器方式獲取表最後更新時間,並將時間信息寫入到另一張表html
1、建立測試表和表記錄更新時間表sql
CREATE TABLE weather( city varchar(80), temp_lo int, --最低溫度 temp_hi int, --最高溫度 prcp real, --溼度 date date );
CREATE TABLE t_record_change( table_name varchar(32) primary key, last_update_time timestamp(6) with time zone, trans_id varchar(32), commited numeric(1,0) );
2、建立觸發器函數函數
--建立觸發器函數 create or replace function f_update_change_log() returns trigger as $$ begin insert into t_record_change(table_name,last_update_time,trans_id,commited) values(TG_TABLE_NAME,current_timestamp,(select txid_current()),1) on conflict(table_name) do update set last_update_time = current_timestamp,trans_id = (select txid_current()),commited = 1; return null; end; $$ language plpgsql;
3、建立觸發器post
--建立觸發器 drop trigger if exists x_weather_u on weather; create trigger x_weather_u after insert or update or delete on weather for each statement execute procedure f_update_change_log();
4、測試測試
在sql窗口中分別執行如下sql語句,併到t_record_change表中查看時間是否更新ui
insert into weather values('nanjing',20,40,0.28,'2018-06-27'); update weather set temp_lo = 15 where city = 'nanjing'; delete from weather where city = 'nanjing';