1、直接在創建表時添加該列並聲明默認值,如下:
CREATE TABLE `table1` ( `id` int(11) NOT NULL, `createtime` timestamp NULL default 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
OK