考察下面的 SQL 脚本:

CREATE TABLE test1(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
data VARCHAR(20),
ts1 TIMESTAMP NOT NULL,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updatedAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

语义上来看,这里 ts1 列没有指定默认值,同时也没指定 ON UPDATE 的操作。

实际情况则不然,在 explicit_defaults_for_timestamp 关闭的情况下,t1 会被默认加上 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP。这可通过 DESCRIBE test1 查看到。

mysql> describe test1;
+-----------+-------------+------+-----+-------------------+-----------------------------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------------+-----------------------------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| data | varchar(20) | YES | | NULL | |
| ts1 | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
| createdAt | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
| updatedAt | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
+-----------+-------------+------+-----+-------------------+-----------------------------------------------+
5 rows in set (0.01 sec)

@@explicit_defaults_for_timestamp

explicit_defaults_for_timestamp 控制是否开启非标准模式下非法的日期值继续努力 NULL,零值等日期默认值被写入 TIMESTAMP 类型。

mysql> select @@explicit_defaults_for_timestamp;
+-----------------------------------+
| @@explicit_defaults_for_timestamp |
+-----------------------------------+
| 1 |
+-----------------------------------+
1 row in set (0.00 sec)

可以看到默认为开,现在来关闭它:

set @@explicit_defaults_for_timestamp=0;
关闭 `explicit_defaults_for_timestamp` 的执行结果
mysql> set @@explicit_defaults_for_timestamp=0;
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select @@explicit_defaults_for_timestamp;

+-----------------------------------+

| @@explicit_defaults_for_timestamp |

+-----------------------------------+

| 0 |

+-----------------------------------+

1 row in set (0.00 sec)

此时便可以正常执行文章开头的 SQL 语句创建一个未指定默认值的 TIMESTAMP 列。

根据官方文档中相关描述:

TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.

--11.3.4 Automatic Initialization and Updating for TIMESTAMP and DATETIME

当且仅当表中第一个 TIMESTAMP 类型的列在没有指定默认值时,MySQL 会会其自动加上 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP。这里 ON UPDATE CURRENT_TIMESTAMP 就需要小心了,如果你想保存的是一个确定性,它会在记录更新时自动更新这个日期,这就是我们表要的表现了。

所以对于 TIMESTAMP 类型,最好指定默认值,避免不可预知的表现。

相关资源

MySQL `explicit_defaults_for_timestamp` 与 TIMESTAMP的更多相关文章

  1. mysql explicit_defaults_for_timestamp参数

    在mysql中:- timestamp列如果没有显式定义为null,默认会被设置为not null属性.(其它的数据类型如果没有显式定义为not null,默认是可以为null的).设置timesta ...

  2. MySQL datetime 和 timestamp 的区别

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

  3. MySQL 时间戳(Timestamp)函数

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

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

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

  5. MySQL 时间戳(Timestamp)函数

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

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

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

  7. mysql多个TimeStamp设置

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

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

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

  9. mysql explicit_defaults_for_timestamp 变量的作用

    mysql 中有这样的一个默认行为,如果一行数据中某些列被更新了,如果这一行中有timestamp类型的列,那么么这个timestamp列的数据 也会被自动更新到 更新操作所发生的那个时间点:这个操作 ...

随机推荐

  1. CentOS 5.5下搭建部署独立SVN服务器全程详解

    SVN服务器有2种运行方式:1.独立服务器 (例如:svn://xxx.com/xxx):2.借助apache   (例如:http://svn.xxx.com/xxx):为了不依赖apache,我选 ...

  2. Java字符串String 集合的迭代器

    Java字符串String 我们知道Java的字符窜是Immutable(不可变)的,一旦创建就不能更改其内容了:平常我们对字符串的操作是最多的,其实对字符串的操作,返回的字符串都是新建的字符串对象, ...

  3. android binder 机制三(匿名Service)

    什么是匿名Service?凡是没有到ServiceManager上注冊的Service,都是匿名Service. 还是拿上一篇的样例来举例,看代码: status_t MediaPlayer::set ...

  4. 【转载】FAT12格式的引导程序(2)

     1.用WinImage来写入到引导区的详细步骤: 启动WinImage后,打开“文件”菜单,单击菜单中的“打开”命令. 选择之前保存的磁盘镜像文件“boot.img”或者“boot.ima”. 打开 ...

  5. java的多态以及重载,重写,前期绑定,后期绑定

    多态的定义: 一个类实例的相同方法在不同情形有不同表现形式.多态机制使具有不同内部结构的对象可以共享相同的外部接口.这意味着,虽然针对不同对象的具体操作不同,但通过一个公共的类,它们(那些操作)可以通 ...

  6. 网页Html代码优化及分析

  7. TiDB 整体架构 结合yarn zookeeper分析架构

    TiDB 简介与整体架构| PingCAP https://www.pingcap.com/docs-cn/overview/ 真正金融级高可用 相比于传统主从 (M-S) 复制方案,基于 Raft ...

  8. What is the difference between message queue pattern and publish-subscribe?

    What is the difference between message queue pattern and publish-subscribe? - Quora https://www.quor ...

  9. nlp_tool

    http://www.afenxi.com/post/9700 11款开放中文分词引擎大比拼 附录评测数据地址http://bosonnlp.com/dev/resource 各家分词系统链接地址Bo ...

  10. acd - 1427 - Nice Sequence(线段树)

    题意:一个由n个数组成的序列(序列元素的范围是[0, n]).求最长前缀 j .使得在这个前缀 j 中对于随意的数 i1 < i2.都满足随意的 m <= j.i1 在前 m 个数里出现的 ...