Mysql自動設定時間(自動獲取時間,填充時間)

2023-01-28 21:01:17 字數 1497 閱讀 5748

應用場景:

1、在資料表中,要記錄每條資料是什麼時候建立的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄建立時間;

2、在資料庫中,要記錄每條資料是什麼時候修改的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄修改時間;

實現方式:

1、將欄位型別設為  timestamp 

2、將預設值設為  current_timestamp

舉例應用:

1、mysql 指令碼實現用例

--新增createtime 設定預設時間 current_timestamp 

alter

table

`table_name`

addcolumn `createtime` datetime

null

default

current_timestamp comment '

建立時間

' ;

--修改createtime 設定預設時間 current_timestamp 

alter

table

`table_name`

modify

column `createtime` datetime

null

default

current_timestamp comment '

建立時間

' ;

--新增updatetime 設定 預設時間 current_timestamp   設定更新時間為 on update current_timestamp 

alter

table

`table_name`

addcolumn `updatetime` timestamp

null

default

current_timestamp

onupdate

current_timestamp comment '

建立時間

' ;

--修改 updatetime 設定 預設時間 current_timestamp   設定更新時間為 on update current_timestamp 

alter

table

`table_name`

modify

column `updatetime` timestamp

null

default

current_timestamp

onupdate

current_timestamp comment '

建立時間

' ;

2、mysql工具設定

總結:1、mysql自動管理,保持和資料庫時間一致性;

2、簡單高效,不需要應用程式開發支援,mysql自動完成;

Mysql自動設定時間(自動獲取時間,填充時間)

應用場景 1 在資料表中,要記錄每條資料是什麼時候建立的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄建立時間 2 在資料庫中,要記錄每條資料是什麼時候修改的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄修改時間 實現方式 1 將欄位型別設為 timestamp 2 ...

MySQL 自動設定時間

工具總結 在資料表中,插入資料,自動記錄建立時間 create time 在資料庫中,修改資料,自動記錄更新時間 update time 將欄位型別設為 timestamp 將預設值設為 current timestamp 新增 create time 設定預設時間 current timestam...

mySQL自動設定當前時間

應用場景 1 在資料表中,要記錄每條資料是什麼時候建立的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄建立時間 2 在資料庫中,要記錄每條資料是什麼時候修改的,不需要應用程式去特意記錄,而由資料資料庫獲取當前時間自動記錄修改時間 實現方式 1 將欄位型別設為 timestamp 2 ...