国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > PostgreSQL > 正文

PostgreSQL更新表時(shí)時(shí)間戳不會(huì)自動(dòng)更新的解決方法

2020-03-12 23:45:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PostgreSQL更新表時(shí)時(shí)間戳不會(huì)自動(dòng)更新的解決方法,具體如下

操作系統(tǒng):CentOS7.3.1611_x64

PostgreSQL版本:9.6

問(wèn)題描述

PostgreSQL執(zhí)行Insert語(yǔ)句時(shí),自動(dòng)填入時(shí)間的功能可以在創(chuàng)建表時(shí)實(shí)現(xiàn),但更新表時(shí)時(shí)間戳不會(huì)自動(dòng)自動(dòng)更新。

在mysql中可以在創(chuàng)建表時(shí)定義自動(dòng)更新字段,比如 :

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

那PostgreSQL中怎么操作呢?

解決方案

通過(guò)觸發(fā)器實(shí)現(xiàn),具體如下:

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();

測(cè)試代碼:

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 updateset email = excluded.email,num=excluded.num;select * from ts;-- delete from ts;

好,就這些了,希望對(duì)你有幫助。

本文github地址

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PostgreSQL頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 嘉黎县| 辽宁省| 云龙县| 蓬溪县| 嘉善县| 英山县| 榆树市| 洱源县| 阿荣旗| 江永县| 同心县| 栾川县| 赣榆县| 会泽县| 桂林市| 开远市| 淮安市| 方山县| 利津县| 金坛市| 达尔| 大化| 钦州市| 湖北省| 江永县| 公主岭市| 民乐县| 寿光市| 巴青县| 波密县| 肥乡县| 武安市| 烟台市| 文水县| 晋州市| 沙雅县| 彝良县| 苍山县| 得荣县| 乐安县| 连平县|