mysql时间戳字段设置默认时间为当前时间

wylc123 1年前 ⋅ 752 阅读

1、直接在创建表时添加该列并声明默认值,如下:

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

如果是在navicat下操作的话,设置字段的类型为timestamp,默认值写上CURRENT_TIMESTAMP,如下图:

设置字段类型为时间戳类型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

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: