mysql 時間字段默認設置為當前時間


1、直接在創建表時添加該列並聲明默認值,如下:

CREATE TABLE `table1` (
  `id` int(11) NOT NULL,
  `createtime` timestamp NULL default CURRENT_TIMESTAMP,
`updatetime` timestamp NULL default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

如果是在navicat下操作的話,設置字段的類型為timestamp,默認值寫上CURRENT_TIMESTAMP,如下圖:

2、在現有表中添加新列

ALTER TABLE table1
ADD COLUMN  `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP

3、 修改某一列為時間格式並添加默認值

alter table table1 
 change createtime newtime timestamp null default current_timestamp

 

4、展示毫秒

如果想記錄到毫秒,設置CURRENT_TIMESTAMP(3)即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM