1.概述

在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践。尤其是可能需要做数据同步或者对数据新鲜度有要求的表。举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个月前的订单数据归档等等。我们想把这个的需求丢给数据库服务器管理,而不是在应用程序中对每一条语句设置创建时间和最后更新时间字段。在mysql中,这实现起来很容易。我们需要借助于DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP

2.简单示例

1 --创建测试表

2 CREATE TABLE`timestampTest` (3 `id` int(11) NOT NULLAUTO_INCREMENT,4 `name` varchar(20) DEFAULT NULL,5 `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,6 `last_modify_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,7 PRIMARY KEY(`id`)8 ) ENGINE=InnoDB DEFAULT CHARSET=utf89
10 --检测默认值,插入测试数据
11 INSERT INTO timestampTest (name) VALUES ('aa'),('bb'),('cc');12
13 --检测自动更新,更新某条数据
14 UPDATE timestampTest SET name = 'ab' WHERE id = 1;

mysql自动更新时间失败 mysql 0点自动更新_mysql自动更新时间失败

例子非常简单,结果也很明显,我就不加赘述了。

3.思考

执行update语句,并未改变列值(或者说设置值为当前值),on update current_timestamp列是否会更新?

不会,大家可以看一下执行完update后出现的提示——Rows matched: 1 Changed: 0 Warnings: 0。官方文档的解释是

An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have

mysql自动更新时间失败 mysql 0点自动更新_显式_02

CURRENT_TIMESTAMP,CURRENT_TIMESTAMP(),LOCALTIME,LOCALTIME(),LOCALTIMESTAMP,LOCALTIMESTAMP(),NOW()的关系?

这七个是同义词关系

Timestamp类型的默认值

我们讨论默认情况(严格模式)下mysql对timestamp类型的处理:

mysql不会给timestamp设置默认值,除非显式设置default约束或者可空null。特例:mysql会给表第一个timestamp类型的字段同时添加default current_timestamp和on update timestamp

禁止mysql的特例处理有两个办法

设置explicit_defaults_for_timestamp为enable

显式设置该字段default或者null

timestamp列默认not null。没有显式指定nullable,那么default null不合法

其他情况均会引起不合法报错

举一些例子,帮助理解

1 #语句不合法,出现了两个未显示设置default或null的timestamp2 CREATE TABLE`tt1` (3 `id` int(11) NOT NULLAUTO_INCREMENT,4 `name` varchar(20),5 `t1` timestamp,6 `t2` timestamp,7 PRIMARY KEY(`id`)8 ) ENGINE=InnoDB DEFAULT CHARSET=utf89
10 #语句合法,t1字段 not null default current_timestamp on update current_timestamp,t2可空11 CREATE TABLE`tt2` (12 `id` int(11) NOT NULLAUTO_INCREMENT,13 `name` varchar(20),14 `t1` timestamp,15 `t2` timestamp null,16 PRIMARY KEY(`id`)17 ) ENGINE=InnoDB DEFAULT CHARSET=utf818
19 #语句不合法 t2字段没有设置default或null,也非表的第一个timestamp字段20 CREATE TABLE`tt3` (21 `id` int(11) NOT NULLAUTO_INCREMENT,22 `name` varchar(20),23 `t1` timestamp null,24 `t2` timestamp,25 PRIMARY KEY(`id`)26 ) ENGINE=InnoDB DEFAULT CHARSET=utf827
28 #语句不合法,这个看起来貌似合法,套用我们的规则,可以发现t2字段没有显示指定null/default,尽管指定了not null也不行29 CREATE TABLE`tt4` (30 `id` int(11) NOT NULLAUTO_INCREMENT,31 `name` varchar(20),32 `t1` timestamp null,33 `t2` timestamp not null,34 PRIMARY KEY(`id`)35 ) ENGINE=InnoDB DEFAULT CHARSET=utf836
37 #语句不合法 t1,t2均合法,问题出在t3上,timestamp 默认not null,在没有显式指定null的时候,default null是不合法的38 CREATE TABLE`tt5` (39 `id` int(11) NOT NULLAUTO_INCREMENT,40 `name` varchar(20),41 `t1` timestamp not null,42 `t2` timestamp null,
43 `t3` timestamp DEFAULT null,
44 PRIMARY KEY(`id`)45 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

小技巧:可以使用show create table `tablename`来查看mysql处理后的表定义,下面是tt2这张表的定义,验证了我们的结论

mysql自动更新时间失败 mysql 0点自动更新_mysql timestamp 自动更新_03

mysql自动更新时间失败 mysql 0点自动更新_显式_04

Timestamp和datetime的异同
timestamp
datetime
同
可自动更新和初始化,默认显示格式相同YYYY-MM-dd HH:mm:ss
异
'1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC
自动时区转化,实际存储毫秒数,4字节存储
'1000-01-01 00:00:00' to '9999-12-31 23:59:59'

不支持时区,8字节存储