MySql建立時間及更新時間的智慧型更新

2021-09-23 06:08:50 字數 877 閱讀 4109

一條記錄一般會有建立時間跟更新時間

建立時間為第一次寫資料時填入,更新時間則記錄資料的最後修改時間

我們在資料庫設定了唯一索引,想基於唯一索引來批量插入更新資料,一般可以用replace 或者 on duplicate key

replace的原理是先根據唯一索引檢查有無資料,沒有的插入,有的先刪除舊的記錄,然後再插入新資料。原記錄的資料無法保留

on duplicate key在已有資料的情況下,可以選擇性的更新字段 ,可保留某些字段值 如create_time。

current_timestamp ,設定字段獲取當前的系統時間,但是乙個表只允許設定乙個。

如下這種設定是不會同時生效的

create table aaa(

create_timedatetime not null default current_timestamp comment 『建立時間』,

update_timetimestamp not null default current_timestamp on update current_timestamp comment 『更新時間』);

有兩種解決方式:

方式一:on duplicate key update 時寫入更新時間

方式二:網上找到一種解決方式

create tablebbb(

create_timetimestamp not null default 0,

update_timetimestamp not null default current_timestamp on update current_timestamp

) ;

Mysql 建立時間和更新時間

分三種情況 create table mytest text varchar 255 default comment 內容 直接指定建立時間和更新時間 create time timestamp not null default current timestamp comment 建立時間 upda...

MySQL 設定 建立時間 和 更新時間

在實際應用中,我們時常會需要用到建立時間和更新時間這兩個字段,下面記錄一下 create table user info uid varchar 255 character set utf8 collate utf8 general ci not null,username varchar 255 ...

mysql更新時間 Mysql 更新時間

mysql時間加減函式為date add date sub 定義和用法 date add 函式向日期新增指定的時間間隔。date sub 函式向日期減少指定的時間間隔。語法date add date,interval expr type date sub date,interval expr typ...