PostgreSQL之时间戳自动更新

问题描述

PostgreSQL执行Insert语句时,时自动自动填入时间的间戳功能可以在创建表时实现,但更新表时时间戳不会自动自动更新。亿华云计算更新

在mysql中可以在创建表时定义自动更新字段,时自动比如 :

create table ab (   id int,间戳   changetimestamp timestamp     NOT NULL     default CURRENT_TIMESTAMP     on update CURRENT_TIMESTAMP );  

那PostgreSQL中怎么操作呢?

解决方案

通过触发器实现,具体如下:

create or replace function upd_timestamp() returns trigger as $$ begin     new.modified = current_timestamp;     return new; end $$ language plpgsql; drop table if exists ts; create table ts (     id      bigserial  primary key,更新     tradeid integer ,     email varchar(50),     num integer,     modified timestamp default current_timestamp ); create trigger t_name before update on ts for each row execute procedure upd_timestamp();

测试代码: 

insert into ts (tradeid,email,num) values (1223,mike_zhang@live.com,1); update ts set email=Mike_Zhang@live where tradeid = 1223 ; create unique index ts_tradeid_idx on ts(tradeid); insert into ts(tradeid,email,num) values (1223,Mike_Zhang@live.com,2) on conflict(tradeid) do update set email = excluded.email,num=excluded.num; select * from ts; -- delete from ts;

好,源码库就这些了,时自动希望对你有帮助。间戳

更新云南idc服务商
IT科技
上一篇:当投资者经过第二阶段的认真学习之后又充满了信心,认为自己可以在市场上叱咤风云地大干一场了。但没想到“看花容易绣花难”,由于对理论知识不会灵活运用.从而失去灵活应变的本能,就经常会出现小赢大亏的局面,结果往往仍以失败告终。这使投资者很是困惑和痛苦,不知该如何办,甚至开始怀疑这个市场是不是不适合自己。在这种情况下,有的人选择了放弃,但有的意志坚定者则决定做最后的尝试。
下一篇:四、一定要仔细阅读细节