在创建时间字段的时候
default current_timestamp
表示当插入数据的时候,该字段默认值为当前时间
on update current_timestamp
表示每次更新这条数据的时候,该字段都会更新成当前时间
这两个操作是mysql数据库本身在维护,所以可以根据这个特性来生成【创建时间】和【更新时间】两个字段,且不需要代码来维护
如下:
create table `mytest` ( `text` varchar(255) default '' comment '内容', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间' ) engine=innodb default charset=utf8;
可以通过navicat的可视化界面直接操作
那么如何设置一个具体的默认时间呢?
如下,注意有两个单引号
timestamp default 'yyyy-mm-dd hh:mm:ss'
文章转自:https://www.cnblogs.com/acm-bingzi/p/mysql-current-timestamp.html