偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

PostgreSQL之時(shí)間戳自動(dòng)更新

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

[[207061]]

問題描述

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)更新字段,比如 :

  1. create table ab ( 
  2.   id int
  3.   changetimestamp timestamp 
  4.     NOT NULL 
  5.     default CURRENT_TIMESTAMP 
  6.     on update CURRENT_TIMESTAMP 
  7. );  

那PostgreSQL中怎么操作呢?

解決方案

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

  1. create or replace function upd_timestamp() returns trigger as 
  2. $$ 
  3. begin 
  4.     new.modified = current_timestamp
  5.     return new; 
  6. end 
  7. $$ 
  8. language plpgsql; 
  9.  
  10. drop table if exists ts; 
  11. create table ts ( 
  12.     id      bigserial  primary key
  13.     tradeid integer , 
  14.     email varchar(50), 
  15.     num integer
  16.     modified timestamp default current_timestamp 
  17. ); 
  18. create trigger t_name before update on ts for each row execute procedure upd_timestamp();  

測(cè)試代碼: 

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

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

責(zé)任編輯:龐桂玉 來源: 36大數(shù)據(jù)
相關(guān)推薦

2015-03-30 14:15:55

自動(dòng)更新Android

2021-11-08 22:24:04

Windows 10Windows微軟

2010-01-08 13:20:52

ibmdwWeb

2025-02-10 00:14:00

2015-09-15 15:25:36

更新配置Windows 10

2012-04-16 14:55:29

MacChrome

2010-12-13 13:33:47

Windows 7驅(qū)動(dòng)

2017-01-12 21:02:29

Windows 10系統(tǒng)更新

2021-07-26 16:23:13

Windows 10Windows微軟

2011-08-02 18:30:32

iOS 應(yīng)用程序 屬性

2010-06-02 19:54:48

SVN自動(dòng)更新

2015-07-27 14:48:16

Windows 10更新

2016-10-21 10:40:06

Windows 10Updata禁用

2018-06-06 10:14:32

Kafka時(shí)間輪任務(wù)

2015-07-21 10:20:00

Windows 10更新微軟

2018-03-19 11:50:00

LinuxCentOS yum命令行

2010-05-24 11:27:20

SVN配置自動(dòng)更新WE

2025-05-29 05:59:56

2019-07-15 16:11:50

微軟WindowsWindows 10

2023-11-22 17:00:10

ElectronWeb
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)