实现方式:
1、将字段类型设为 timestamp
2、将默认值设为 current_timestamp
举例应用:
1、mysql 脚本实现用例
--添加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 '创建时间' ;
2、mysql工具设置
总结:
1、mysql自动管理,保持和数据库时间一致性;
2、简单高效,不需要应用程序开发支持,mysql自动完成;
以上就是mysql自动获取时间日期的方法的详细内容。