【MySQL】explicit_defaults_for_timestamp 参数详解
简介:
explicit_defaults_for_timestamp 系统变量决定MySQL服务端对timestamp列中的默认值和NULL值的不同处理方法。
此变量自MySQL 5.6.6 版本引入,分为全局级别和会话级别,可动态更新,默认值为OFF。本文主要介绍该参数打开和关闭情况下对timestamp的影响 。
1.explicit_defaults_for_timestamp = OFF
当该参数默认设置为OFF时,其行为如下:
在默认情况下,如果timestamp列没有显式的指明null属性,那么该列会被自动加上not null属性(而其他类型的列如果没有被显式的指定not null,那么是允许null值的),如果往这个列中插入null值,会自动的设置该列的值为current timestamp值。
表中的第一个timestamp列,如果没有指定null属性或者没有指定默认值,也没有指定ON UPDATE语句。那么该列会自动被加上DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP属性。
对于其它TIMESTAMP列,如果没有显示指定NULL和DEFAULT属性的话,会自动设置为NOT NULL DEFAULT ‘0000-00-00 00:00:00’。
(当然,这个与SQL_MODE有关,如果SQL_MODE中包含’NO_ZERO_DATE’,实际上是不允许将其默认值设置为’0000-00-00 00:00:00’的。)
下面我们来测试下:(本文操作基于MySQL5.7.23 版本 SQL_MODE不包含’NO_ZERO_DATE’)
show variables like 'explicit_defaults_for_timestamp';

create table test (
col1 timestamp,
col2 timestamp,
col3 timestamp default '2010-01-01 00:00:00'
);
我们查看创建的表:
show create table test;

insert into test values (null,null,null);

MySQL自动为第一个timestamp字段自动设置NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP属性;
而后面的timestamp字段,虽然设置了 **NOT NULL DEFAULT ‘0000-00-00 00:00:00’**属性,如果向timestamp这个列中插入null值,系统会自动的设置该列的值为current timestamp值。
综上所述:
当explicit_defaults_for_timestamp=OFF时,即使timestamp列设为NOT NULL也能插入NULL值,系统会自动将NULL值设为current timestamp。
2.explicit_defaults_for_timestamp = ON
my.ini配置文件中,添加了该项配置,否则启动时,会提示以下报警:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
但是我在华为的实例中root权限:

当该参数设置为ON时,其行为如下:
- 如果timestamp列没有显式的指定not null属性,那么默认的该列可以为null,此时向该列中插入null值时,会直接记录null,而不是current timestamp。
- 不会自动的为表中的第一个timestamp列加上DEFAULT CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP属性。
- 如果timestamp列被加上了not null属性,并且没有指定默认值。这时如果向表中插入记录,但是没有给该TIMESTAMP列指定值的时候,如果strict sql_mode被指定了,那么会直接报错。
- 如果strict sql_mode没有被指定,那么会向该列中插入’0000-00-00 00:00:00’并且产生一个warning。
同样的,我们来测试一下,为了测试我把我的本地计算的mysql5.5卸载重新安装5.7,一段操作猛如虎:
mysql> show variables like 'explicit_defaults_for_timestamp';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF |
+---------------------------------+-------+
1 row in set, 1 warning (0.00 sec) mysql> set explicit_defaults_for_timestamp=1;
Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'explicit_defaults_for_timestamp';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | ON |
+---------------------------------+-------+
1 row in set, 1 warning (0.00 sec) mysql> use test;
Database changed mysql> create table test (
-> col1 timestamp,
-> col2 timestamp,
-> col3 timestamp default '2010-01-01 00:00:00'
-> );
Query OK, 0 rows affected (0.02 sec) mysql> insert into test values (null,null,null);
Query OK, 1 row affected (0.01 sec) mysql> select * from test;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| NULL | NULL | NULL |
+------+------+------+
1 row in set (0.00 sec)
【MySQL】explicit_defaults_for_timestamp 参数详解的更多相关文章
- shell编程系列23--shell操作数据库实战之mysql命令参数详解
shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...
- 1201MySQL配置文件mysql.ini参数详解
转自http://www.cnblogs.com/feichexia/archive/2012/11/27/mysqlconf.html my.ini(Linux系统下是my.cnf),当mysql服 ...
- (转)MySQL配置文件mysql.ini参数详解、MySQL性能优化
本文转自:http://www.cr173.com/html/18331_1.html my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数 ...
- MySQL配置文件mysql.ini参数详解、MySQL性能优化
my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section. ...
- MySQL配置文件mysql.ini参数详解
my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section. ...
- mysql配置文件参数详解
(一) [client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir ...
- mysql运行参数详解
1, 查看MySQL服务器配置信息 mysql> show variables; 2, 查看MySQL服务器运行的各种状态值 mysql> show global status; 3, 慢 ...
- MySQL性能参数详解 - max_connect_errors
max_connect_errors是一个MySQL中与安全有关的计数器值,它负责阻止过多尝试失败的客户端以防止暴力破解密码的情况.max_connect_errors的值与性能并无太大关系. 默认情 ...
- MySQL备份参数详解
mysqldump 是采用SQL级别的备份机制,它将数据表导成 SQL 脚本文件,在不同的 MySQL 版本之间升级时相对比较合适,这也是最常用的备份方法. 1. --compatible = nam ...
随机推荐
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- LeetCode 792. 匹配子序列的单词数(Number of Matching Subsequences)
792. 匹配子序列的单词数 792. Number of Matching Subsequences 相似题目 392. 判断子序列
- [转帖]微软 SQ1 参数一览:8 核 Kryo 495,Adreno 685 GPU
微软 SQ1 参数一览:8 核 Kryo 495,Adreno 685 GPU http://www.myzaker.com/article/5d989ef68e9f0977765e5506/ 微软发 ...
- go select 的default
当 select 中的其他条件分支都没有准备好的时候,`default` 分支会被执行. 为了非阻塞的发送或者接收,可使用 default 分支: select { case i := <-c: ...
- PowerShell自动部署ASP.NET Core程序到 IIS
Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 .NET Framework的强大功能.有关于更多PowerShell的信息,可参阅百度词条 接 ...
- 使用querySelector添加移除style和class
document.querySelector(selector).style.styleName = 样式 对dom节点添加一个样式 document.querySelector(".nam ...
- hdu1171 灵活的运用背包问题咯。。。 还有!!!! 合理的计算数组的范围!! wa了好多次!
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- Gogs搭建私有git代码仓库
前置环境: 数据库 -> mysql git -> 服务端和客户端版本必须>=1.8.3 ssh服务 -> 如果只使用http/https方式的话,服务端无需配置ssh. st ...
- maven - 多模块构建
使用idea创建maven项目 点击next输入GroupId和ArtifactId 点击next创建项目,新建项目结构如下 修改demo打包方式为pom 按层级拆分创建模块model,server, ...
- springboot接收date类型参数
springboot接收日期类型参数,实现自动转换. 1. 当请求类型为json,date类型字段为 json 字段时,可以使用如下两种方法 1.1. 当前参数加两个注解(有些文章说接收只需要加 @D ...