考察下面的 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. 读取xml生成lua測试代码

    #include <iostream> #include <string> #include <fstream> #include "tinyxml2.h ...

  2. java中简单字符替换

    在网络编程中,假设URL含有特殊字符,如空格.'#'等,server将无法识别导致无法获得正确的參数值.我们须要将这些特殊字符转换成server能够识别的字符,比如将空格转换成'%20'.给定一个字符 ...

  3. cocoaPods 安装和应用

    一.安装 下载安装CocoaPods需要Ruby环境 1. 检测gem版本 $ gem -v 如果gem版本小于2.6.x,则需要更新gem 2. 更新gem(gem版本高于2.6.x可跳过此步) 检 ...

  4. iOS文件的管理(添加,删除,拷贝,移动)

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...

  5. dom get selector

    function realsQuery(element) { if(!element){ return ""; } var currentQuery=""; i ...

  6. flask-本地线程-请求上下文补充

    context(上下文)是flask里面非常好的设计,使用flask需要非常理解应用上下文和请求上下文这两个概念 本地线程 本地线程(thread local)希望不同的线程对于内容的修改只在线程内部 ...

  7. Redis HyperLogLog及应用

    参考:http://www.runoob.com/redis/redis-hyperloglog.html Redis 在 2.8.9 之后的版本中,添加了 HyperLogLog 结构,用来做基数统 ...

  8. 利用BADI WORKORDER_INFOSYSTEM在COOIS中加入自己定义列办事处

    需求描写叙述:依据LC业务部门提出的需求.须要在COOIS中加入办事处一列. 1.在IOHEADER_TAB的CI_IOHEADER中加入字段办事处.如以下图所看到的:   watermark/2/t ...

  9. python day-3 基本数据类型

    1. 编码 1. 最早的计算机编码是ASCII. 美国人创建的. 包含了英文字母(大写字母, 小写字母). 数字, 标点等特殊字符!@#$% 128个码位 2**7 在此基础上加了一位 2**8 8位 ...

  10. HDU 6086 Rikka with String AC自动机 + DP

    Rikka with String Problem Description As we know, Rikka is poor at math. Yuta is worrying about this ...