MySQL 自動插入 更新時間戳

2022-05-07 04:33:08 字數 565 閱讀 3419

在 mysql 中,要做到自動出入當前時間戳,只要在定義**時將字段的預設值設定為current_timestamp 即可。

如:

create table if

not exists my_table(

creation_date datetime default current_timestamp

--other fields

) engine=innodb default charset=utf8;

要在修改資料時自動更新,則可以通過 on update 關鍵字實現,結合上面的default,就可以實現建立時自動插入時間戳,修改時自動更新了。

create table if

not exists my_table(

last_update_date datetime default current_timestamp on update current_timestamp

--other fields

) engine=innodb default charset=utf8;

mysql更新時間戳 Mysql中時間戳自動更新

mysql時間戳字段更新 timestamp是mysql中的時間戳字段,這個字段可以支援自動新增和更新。1.概述 在我們設計表的時候,考慮將行資料的建立時間和最後更新時間記錄下來是很好的實踐。尤其是可能需要做資料同步或者對資料新鮮度有要求的表。舉些應用場景,更新距上次更新超過2小時的行資料,或者是將...

PostgreSQL之時間戳自動更新

postgresql之時間戳自動更新 問題描述 postgresql執行insert語句時,自動填入時間的功能可以在建立表時實現,但更新表時時間戳不會自動自動更新。在mysql中可以在建立表時定義自動更新字段,比如 create table ab id int,age int,changetimes...

oracle插入,更新自動設定當前時間

1.建表語句create table userinfo id varchar2 32 not null primary key,name varchar2 200 age int create time date default sysdate,modify time date default sy...