設定mysql中時間的預設值

2021-10-22 08:50:43 字數 994 閱讀 7097

實現方式:

1、將字段型別設為 timestamp

2、將預設值設為 current_timestamp

舉例應用:

–新增createtime 設定預設時間 current_timestamp

alter table `table_name`add column  `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`add column `updatetime` timestamp null default current_timestamp on update current_timestamp comment '建立時間' ;
–修改 updatetime 設定 預設時間 current_timestamp 設定更新時間為 on update current_timestamp

alter table `table_name`modify column `updatetime` timestamp null default current_timestamp on update current_timestamp comment '建立時間' ;
引用文章:

MySql時間的型別以及預設值設定

mysql時間的型別以及預設值設定 在資料庫mysql 的日期型別有5個 date time 型別 位元組 格式 作用 支援預設 date 3 yyyy mm dd 日期值 否 time 3 hhh mm ss 時間值或持續時間否 year 1 yyyy 年份值 否 datetime 8 yyyy ...

MySQL設定當前時間為預設值的方法

mysql設定當前時間為預設值的問題我們經常會遇到,下面就為您介紹mysql設定當前時間為預設值的實現全步驟,希望對您能有所啟迪。資料庫 test db1 建立表 test ta1 兩個字段 id 自增 且為主鍵 createtime 建立日期 預設值為當前時間 方法一 是用alert table語...

MYSQL datetime設定預設值

使用decimal代替float和double儲存精確浮點數 如果用float簡單的浮點數插入操作,居然會產生偏差,坑爹啊由於timestamp是從1970開始的,如果要記錄出生日期,還是需要用datetime 當然,在一般情況下,推薦使用timestamp alter table project ...