背景

之前有业务反馈表中start_time,end_time时间字段随着时间的推移被自动更新,这可不是业务意愿,说的严重点是要出故障的。

MySQL中有DATE,DATETIME,TIMESTAMP时间类型

看看官方文档怎么说

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

这里我们重点介绍下DATATIME和TIMESTMAP

业务反馈start_time,end_time缺失就使TIMESTAMP类型,结合TIMESTAMP的特性,我们来分析原因。

Automatic Initialization and Updating for TIMESTAMP

One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both.
If the column is auto-initialized, it is set to the current timestamp for inserted rows that specify no value for the column.
If the column is auto-updated, it is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value.

什么意思呢?

create table gbtest_with_force_default_val
(
id bigint AUTO_INCREMENT,
t_null_1 timestamp,
t_null_2 timestamp,
t_with_not_null timestamp not null,
t_with_not_null_default timestamp not null default '2016-12-21',
primary key(id)
); mysql> show create table gbtest_with_force_default_val; CREATE TABLE `gbtest_with_force_default_val` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`t_null_1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`t_null_2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`t_with_not_null` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`t_with_not_null_default` timestamp NOT NULL DEFAULT '2016-12-21 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

在MySQL5.5版本中,TIMESTAMP特性

  1. TIMESTAMP字段默认为NOT NULL,如果你在定义“t_null_1 TIMESTAMP DEFAULT NULL” 会提升“ERROR 1067 (42000): Invalid default value for 't_null_1'”。 可以指定为空 null ,“t_null_1 TIMESTAMP NULL" ,这时可以再添加语句改变默认值。
  2. 如果不做特殊说明,同一个表中会对第一个TIMESTAMP字段设置DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP属性,第二个字段设置DEFAULT '0000-00-00 00:00:00',如果对两个字段都显示指定“DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP”是会被告知“ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause”,测试发现在MySQL5.6版本中并没有该限制。
mysql> select version();
+------------------+
| version() |
+------------------+
| 5.6.16.7-rc0-log |
+------------------+
1 row in set (0.00 sec) CREATE TABLE `gbtest_with_force_default_val_null` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`t_null_1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`t_null_2` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`t_with_not_null` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`t_with_not_null_default` timestamp NOT NULL DEFAULT '2016-12-21 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

那如何解决业务遇到的问题呢,其实很简单,既然“DEFAULT CURRENT_TIMESTAMP”有限制,那不用好了,换成NULL DEFAULT CURRENT_TIMESTAMP去掉自动更新属性,或者显式的允许这两个字段为NULL。

create table gbtest_with_force_default_val_null (
id bigint AUTO_INCREMENT,
t_null_1 timestamp NULL CURRENT_TIMESTAMP,
t_null_2 timestamp NULL DEFAULT 0,
t_with_not_null timestamp not null,
t_with_not_null_default timestamp not null default '2016-12-21', primary key(id) ); mysql> show create table gbtest_with_force_default_val_null; CREATE TABLE `gbtest_with_force_default_val_null` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`t_null_1` timestamp NULL CURRENT_TIMESTAMP,
`t_null_2` timestamp NULL DEFAULT '0000-00-00 00:00:00',
`t_with_not_null` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`t_with_not_null_default` timestamp NOT NULL DEFAULT '2016-12-21 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

MySQL5.6版本中TIMESTAMP特性

经过学习,发现在5.6版中中多了一个叫“explicit_defaults_for_timestamp”的系统变量,但默认是OFF的,但是也可以支持一个表中多个字段的同时更新。

mysql> show variables like '%explicit_defaults_for_timestamp%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF |
+---------------------------------+-------+
1 row in set (0.00 sec)

那将这个参数打开会有什么不同?

mysql> set @@global.explicit_defaults_for_timestamp=ON;
ERROR 1238 (HY000): Variable 'explicit_defaults_for_timestamp' is a read only variable
该参数并不支持动态修改!!! create table gbtest_with_noforce(
id bigint AUTO_INCREMENT,
t_null_1 timestamp,
t_null_2 timestamp,
t_with_not_null timestamp not null,
t_with_not_null_default timestamp not null default '2016-12-21',
primary key(id)
); CREATE TABLE `gbtest_with_noforce` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`t_null_1` TIMESTAMP NULL DEFAULT NULL,
`t_null_2` TIMESTAMP NULL DEFAULT NULL,
`t_with_not_null` TIMESTAMP NOT NULL,
`t_with_not_null_default` TIMESTAMP NOT NULL DEFAULT '2016-12-21 00:00:00',
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
;

发现如下改变:

  1. 默认参数为NULL
  2. 并不会自动添加DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP等属性

结论

那考虑的版本的兼容性,测试发现,在5,5版本中按默认属性创建的表,在5.6版本中迁移是没有任何问题的,是完全兼容的。这说明,使用Mysql5.6以后的版本,应立即将explicit_defaults_for_timestamp参数设置为True,并及时反馈开发TIMESTAMP的行为。

【MySQL】探究之TIMESTAMP的更多相关文章

  1. MySQL 时间戳(Timestamp)函数

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() mysql> select current_timestamp, curren ...

  2. mysql的时间戳timestamp精确到小数点后六位

    1.mysql的时间戳timestamp精确到小数点后六位. 公司业务使用到Greenplun数据库,根据查询的时间戳来不断的将每个时间段之间的数据,进行数据交换,但是今天发现,mysql的时间戳没有 ...

  3. MySQL 时间戳(Timestamp)函数

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp() mysql> select current_timestamp, curren ...

  4. Mysql sql_mode设置 timestamp default 0000-00-00 00:00:00 创建表失败处理

    往数据库里创建新表的时候报错: [Err] 1067 - Invalid default value for 'updateTime' DROP TABLE IF EXISTS `passwd_res ...

  5. MySQL datetime 和 timestamp 的区别

    [转载]:MySQL中有关TIMESTAMP和DATETIME的总结 1. datetime 和 timestamp 的相同点 两者都可以用来表示YYYY-MM-DD HH:MM:SS[.fracti ...

  6. mysql多个TimeStamp设置

    mysql多个TimeStamp设置 2012-11-02 12:58  轩脉刃  阅读(39590)  评论(3)  编辑  收藏 timestamp设置默认值是Default CURRENT_TI ...

  7. MySQL中有关TIMESTAMP和DATETIME的总结

    一.MySQL中如何表示当前时间? 其实,表达方式还是蛮多的,汇总如下: CURRENT_TIMESTAMP CURRENT_TIMESTAMP() NOW() LOCALTIME LOCALTIME ...

  8. mysql中的timestamp类型时间比较:unix_timestamp函数

    在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...

  9. mysql 添加[取消]timestamp的自动更新

    创建自动更新的 timestamp (插入或修改时 uptime都会自动更新) CREATE TABLE `hello` (`id` int(11) NOT NULL,`uptime` timesta ...

随机推荐

  1. CSS选择器和jQuery选择器的区别与联系之一

    到底什么是选择器?我们通过常接触的CSS选择器和jQuery选择器理解一下,我们知道CSS是用于分离网页的结构和表现的,也就是说对于一个网页,HTML定义网页的结构,CSS描述网页的样子,一个很经典的 ...

  2. android动画的Interpolator

    1.Interpolator插值,控制一个动画变化过程中是线性匀速变化,还是加速变化,还是按照某种函数关系变化. 2.android提供的几种插值. 3.对上边提供的各个效果进行测试 (1)Accel ...

  3. 转摘: CSDN linxianliang5201314 的 blog ------sql解释执行顺序

    我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动 大,那么我么还能保证下一段时间系统还能流畅的运行吗?我么 ...

  4. Oracle错误:动态执行表不可访问,本会话自动统计被禁止,关闭自动统计之后的问题

    使用PL/SQL时, 每次第一次打开表的时候会提示"动态执行表不可访问,本会话的自动统计被禁止"的错误,一消息如下: V$SESSION,V$SESSTAT,V$STATNAME没 ...

  5. 关于Elasticsearch单个索引文档最大数量问题

    因为ElasticSearch是一个基于Lucene的搜索服务器.Lucene的索引有个难以克服的限制,导致Elasticsearch的单个分片存在最大文档数量限制,一个索引分片的最大文档数量是20亿 ...

  6. mybaits入门

    1.回顾jdbc开发 orm概述 orm是一种解决持久层对象关系映射的规则,而不是一种具体技术.jdbc/dbutils/springdao,hibernate/springorm,mybaits同属 ...

  7. CodeForces 688C-NP-Hard Problem

    题意: 给你一个无向图,判断是否能够构成一个二分图,如果能的话,输出二分图左边的集合和右边的集合 分析: 先给每一个顶点的color初始化-1,表示没有被染色,用vector数组v[a],表示元素a所 ...

  8. Android 4.2以上的手机USB调试设置

    今天遇到一个问题,我手上有两部手机一部是红米.一部是中兴的青漾QY N986,两部手机的Android系统都是4.2.1的,连接到电脑测试,找了半天没有找到设置开发者选项,后来在网上找了半天,才发现g ...

  9. 浅析Hadoop文件格式

    Hadoop 作为MR 的开源实现,一直以动态运行解析文件格式并获得比MPP数据库快上几倍的装载速度为优势.不过,MPP数据库社区也一直批评Hadoop由于文件格式并非为特定目的而建,因此序列化和反序 ...

  10. loadrunner的基本操作

    一.遗留问题: 1.controller中,到设置的时间后,仍然在运行: 2.如何对多个用例的结果进行分析,找到系统可以承受的最佳的用户数量点: 3.vuser与实际的用户访问数量是一回事吗?比如vu ...