我是一名立志把细节都说清楚的博主,欢迎关注🎉 ~
原创不易,如果有帮助,还请鼓励个【赞】哦~ ❥(^_-)~
背景
根据《阿里巴巴Java开发手册》第五章MySQL数据库:第一节 建表规约,第9条:
【强制】表必备三字段:id,create_time,update_time。
说明:其中 id 必为主键,类型为 bigint unsigned、单表时自增、步长为 1。create_time,update_time 的类型均为datetime 类型,前者现在时表示主动式创建,后者过去分词表示被动式更新。
实现方式
- 按照《阿里巴巴Java开发手册》规范要求,create_time 和 update_time 数据类型先设定为 datetime 。
- 默认值设置为 CURRENT_TIMESTAMP。
- 更新时间( update_time )还要再设置一个ON UPDATE CURRENT_TIMESTAMP。
MySQL 实现
- 直接查看第四行(create_time)和第五行(update_time)的SQL语句即可。
- update_time 在create_time的基础上,还需要设置 ON UPDATE CURRENT_TIMESTAMP。
create table datetime (
id bigint unsigned primary key auto_increment comment "id",
name varchar not null comment "名称",
create_time datetime not null default CURRENT_TIMESTAMP comment "创建时间",
update_time datetime not null default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment "更新时间",
) engine = Innodb default charset = utf8mb4 comment = "演示表";
Navicat 实现
- create_time :不勾选“根据当前时间戳更新”。
- update_time:勾选“根据当前时间戳更新”。
DataGrip 实现
- create_time :ON UPDATE 不设置 CURRENT_TIMESTAMP。
- update_time:ON UPDATE 设置 CURRENT_TIMESTAMP。
我是一名立志把细节都说清楚的博主,欢迎关注🎉 ~
原创不易,如果有帮助,还请鼓励个【赞】哦~ ❥(^_-)~