在MySQL中可以在創建表時定義自動更新字段,比如 :
注意時間戳字段tmv的長度為0
create table ab ( id int, tmv timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP );
在PostgreSQL中可以在創建表時定義自動更新字段,比如 :
通過觸發器實現,具體如下:
1、創建函數(注意tmv就是時間戳字段):
create or replace function upd_timestamp() returns trigger as $$ begin new.tmv= current_timestamp; return new; end $$ language plpgsql;
2、創建測試表(注意tmv就是時間戳字段)
drop table if exists test; create table test( name varchar(50), tmv timestamp default current_timestamp );
3、創建觸發器
create trigger t_name before update on test for each row execute procedure upd_timestamp();