mysql 5.6 中 explicit_defaults_for_timestamp参数
mysql 5.6 中 explicit_defaults_for_timestamp参数
一:
官方文档中关于explicit_defaults_for_timestamp参数说明如下:
explicit_defaults_for_timestamp
Introduced 5.6.6 Deprecated 5.6.6 Command-Line Format --explicit_defaults_for_timestamp=#
System Variable Name explicit_defaults_for_timestamp
Variable Scope Global, Session Dynamic Variable No Permitted Values Type boolean
Default FALSE
In MySQL, the
TIMESTAMP
data type differs in nonstandard ways from other data types:(在没有设置explicit_defaults_for_timestamp=1的情况下)TIMESTAMP
columns not explicitly declared with theNULL
attribute are assigned theNOT NULL
attribute. (Columns of other data types, if not explicitly declared asNOT NULL
, permitNULL
values.) Setting such a column toNULL
sets it to the current timestamp.在默认情况下,如果TIMESTAMP列没有显示的指明null属性,那么该列会被自动加上not null属性(而其他类型的列如果没有被显示的指定not null,那么是允许null值的),如果往这个列中插入null值,会自动的设置该列的值为current timestamp值The first
TIMESTAMP
column in a table, if not declared with theNULL
attribute or an explicitDEFAULT
orON UPDATE
clause, is automatically assigned theDEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
attributes.表中的第一个TIMESTAMP列,如果没有指定null属性或者没有指定默认值,也没有指定ON UPDATE语句。那么该列会自动被加上DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP属性。TIMESTAMP
columns following the first one, if not declared with theNULL
attribute or an explicitDEFAULT
clause, are automatically assignedDEFAULT '0000-00-00 00:00:00'
(the “zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned'0000-00-00 00:00:00'
and no warning occurs.第一个TIMESTAMP列之后的其他的TIMESTAMP类型的列,如果没有指定null属性,也没有指定默认值,那么该列会被自动加上DEFAULT '0000-00-00 00:00:00'属性。如果insert语句中没有为该列指定值,那么该列中插入'0000-00-00 00:00:00',并且没有warning
Those nonstandard behaviors remain the default for
TIMESTAMP
but as of MySQL 5.6.6 are deprecated and this warning appears at startup:在5.6.6及以后的版本中,如果在配置文件中没有指定explicit_defaults_for_timestamp参数,启动时error日志中会报如下错误[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).As indicated by the warning, to turn off the nonstandard behaviors, enable the
explicit_defaults_for_timestamp
system variable at server startup. With this variable enabled, the server handlesTIMESTAMP
as follows instead:如果我们在启动的时候在配置文件中指定了explicit_defaults_for_timestamp=1,mysql会按照如下的方式处理TIMESTAMP 列TIMESTAMP
columns not explicitly declared asNOT NULL
permitNULL
values. Setting such a column toNULL
sets it toNULL
, not the current timestamp. 此时如果TIMESTAMP列没有显示的指定not null属性,那么默认的该列可以为null,此时向该列中插入null值时,会直接记录null,而不是current timestamp。No
TIMESTAMP
column is assigned theDEFAULT CURRENT_TIMESTAMP
orON UPDATE CURRENT_TIMESTAMP
attributes automatically. Those attributes must be explicitly specified. 不会自动的为表中的第一个TIMESTAMP列加上DEFAULT CURRENT_TIMESTAMP
和ON UPDATE CURRENT_TIMESTAMP属性,除非你在建表的时候显示的指明TIMESTAMP
columns declared asNOT NULL
and without an explicitDEFAULT
clause are treated as having no default value. For inserted rows that specify no explicit value for such a column, the result depends on the SQL mode. If strict SQL mode is enabled, an error occurs. If strict SQL mode is not enabled, the column is assigned the implicit default of'0000-00-00 00:00:00'
and a warning occurs. This is similar to how MySQL treats other temporal types such asDATETIME
.如果TIMESTAMP列被加上了not null属性,并且没有指定默认值。这时如果向表中插入记录,但是没有给该TIMESTAMP列指定值的时候,如果strict sql_mode被指定了,那么会直接报错。如果strict sql_mode没有被指定,那么会向该列中插入'0000-00-00 00:00:00'并且产生一个warning
Noteexplicit_defaults_for_timestamp
is itself deprecated because its only purpose is to permit control over now-deprecatedTIMESTAMP
behaviors that will be removed in a future MySQL release. When that removal occurs,explicit_defaults_for_timestamp
will have no purpose and will be removed as well.This variable was added in MySQL 5.6.6
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2)创建测试表test_time
time1 timestamp,
time2 timestamp,
time3 timestamp,
id int
);
- show create table test_time;
- +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | Table | Create Table |
- +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | test_time | CREATE TABLE `test_time` (
- `time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- `time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
- `time3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
- `id` int(11) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
##从表结构中可以看到 表中三个timestamp列都被自动设置为not null,并且表中第一个timestamp列被设置了DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP默认值,其他的timestamp列被加上了DEFAULT '0000-00-00 00:00:00'默认值
- mysql> insert into test_time select null,null,null,1;
- Query OK, 1 row affected (0.02 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> select * from test_time;
- +---------------------+---------------------+---------------------+------+
- | time1 | time2 | time3 | id |
- +---------------------+---------------------+---------------------+------+
- | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 1 |
- +---------------------+---------------------+---------------------+------+##往timestamp列插入null值时,会自动为该列设置为current time
- 1 row in set (0.00 sec)
- mysql> insert into test_time(time1,id) select null,2;
- Query OK, 1 row affected (0.04 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> select * from test_time;
- +---------------------+---------------------+---------------------+------+
- | time1 | time2 | time3 | id |
- +---------------------+---------------------+---------------------+------+
- | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 1 |
- | 2016-01-25 10:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 2 |
- +---------------------+---------------------+---------------------+------+##插入时未指定值的timestamp列中被插入了0000-00-00 00:00:00(非表中第一个timestamp列)
- 2 rows in set (0.00 sec)
- mysql> insert into test_time(id) select 3;
- Query OK, 1 row affected (0.03 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> select * from test_time;
- +---------------------+---------------------+---------------------+------+
- | time1 | time2 | time3 | id |
- +---------------------+---------------------+---------------------+------+
- | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 1 |
- | 2016-01-25 10:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 2 |
- | 2016-01-25 10:01:41 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 | 3 |
- +---------------------+---------------------+---------------------+------+##插入时未指定值的第一个timestamp列中被插入了current time值
- 3 rows in set (0.00 sec)
2.启动mysql时设置explicit_defaults_for_timestamp=1
time1 timestamp,
time2 timestamp,
time3 timestamp,
id int
);
- show create table test_time1;
- +------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | Table | Create Table |
- +------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | test_time1 | CREATE TABLE `test_time1` (
- `time1` timestamp NULL DEFAULT NULL,
- `time2` timestamp NULL DEFAULT NULL,
- `time3` timestamp NULL DEFAULT NULL,
- `id` int(11) DEFAULT NULL
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
##通过表结构我们看到,三个timestamp列都被设置为null,并且设置了默认值为null
- mysql> insert into test_time1 select null,null,null,1;
- Query OK, 1 row affected (0.00 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> select * from test_time1;
- +-------+-------+-------+------+
- | time1 | time2 | time3 | id |
- +-------+-------+-------+------+
- | NULL | NULL | NULL | 1 |
- +-------+-------+-------+------+##未显示指定timestamp列为not null时,能够向timestamp列中插入null值
- 1 row in set (0.00 sec)
- mysql> insert into test_time1(time1,id) select null,2;
- Query OK, 1 row affected (0.01 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> commit;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from test_time1;
- +-------+-------+-------+------+
- | time1 | time2 | time3 | id |
- +-------+-------+-------+------+
- | NULL | NULL | NULL | 1 |
- | NULL | NULL | NULL | 2 |
- +-------+-------+-------+------+##插入时没有为timestamp列指定值时,自动插入null值
- 2 rows in set (0.00 sec)
- mysql> insert into test_time1(id) select 3;
- Query OK, 1 row affected (0.02 sec)
- Records: 1 Duplicates: 0 Warnings: 0
- mysql> select * from test_time1;
- +-------+-------+-------+------+
- | time1 | time2 | time3 | id |
- +-------+-------+-------+------+
- | NULL | NULL | NULL | 1 |
- | NULL | NULL | NULL | 2 |
- | NULL | NULL | NULL | 3 |
- +-------+-------+-------+------+##插入时没有为timestamp指定值时,自动插入null值(表中第一个timestamp列也是插入null值)
- 3 rows in set (0.00 sec)
4)指定了not null属性的timestamp列插入测试
- <span style="color:#555555;">mysql> create table test_time2(
- -> time1 timestamp,
- -> time2 timestamp not null,
- -> time3 timestamp,
- -> id int
- -> );
- Query OK, 0 rows affected (0.10 sec)
- mysql> insert into test_time2(time1,id) select null,1;
- ERROR 1364 (HY000): Field 'time2' doesn't have a default value </span><span style="color:#ff0000;">##为timestamp列指定了not null属性,在stric sql_mode时,如果插入时该列没有指定值,会直接报错</span><span style="color:#555555;">
- mysql> show variables like 'sql_mode';
- +---------------+--------------------------------------------------------------------------------------+
- | Variable_name | Value |
- +---------------+--------------------------------------------------------------------------------------+
- | sql_mode | NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
- +---------------+--------------------------------------------------------------------------------------+
- 1 row in set (0.01 sec)
- mysql> set session sql_mode='';
- Query OK, 0 rows affected (0.00 sec)
- mysql> show variables like 'sql_mode';
- +---------------+-------+
- | Variable_name | Value |
- +---------------+-------+
- | sql_mode | |
- +---------------+-------+
- 1 row in set (0.00 sec)
- mysql> insert into test_time2(time1,id) select null,1;
- Query OK, 1 row affected, 1 warning (0.01 sec)
- Records: 1 Duplicates: 0 Warnings: 1 </span><span style="color:#ff0000;">##如果为timestamp列指定not null属性,在非stric sql_mode模式下,如果插入的时候该列没有指定值,那么会向该列中插入0000-00-00 00:00:00,并且产生告警</span><span style="color:#555555;">
- mysql> show warnings;
- +---------+------+--------------------------------------------+
- | Level | Code | Message |
- +---------+------+--------------------------------------------+
- | Warning | 1364 | Field 'time2' doesn't have a default value |
- +---------+------+--------------------------------------------+
- 1 row in set (0.00 sec)
- mysql> select * from test_time2;
- +-------+---------------------+-------+------+
- | time1 | time2 | time3 | id |
- +-------+---------------------+-------+------+
- | NULL | 0000-00-00 00:00:00 | NULL | 1 |
- +-------+---------------------+-------+------+
- 1 row in set (0.00 sec)</span>
mysql 5.6 中 explicit_defaults_for_timestamp参数的更多相关文章
- MySQL 并行复制演进及 MySQL 8.0 中基于 WriteSet 的优化
MySQL 8.0 可以说是MySQL发展历史上里程碑式的一个版本,包括了多个重大更新,目前 Generally Available 版本已经已经发布,正式版本即将发布,在此将介绍8.0版本中引入的一 ...
- 【MySQL】explicit_defaults_for_timestamp 参数详解
简介:explicit_defaults_for_timestamp 系统变量决定MySQL服务端对timestamp列中的默认值和NULL值的不同处理方法. 此变量自MySQL 5.6.6 版本引入 ...
- MySQL中binlog参数:binlog_rows_query_log_events-记录具体的SQL【转】
在使用RBR也就是行格式的时候,去解析binlog,需要逆向才能分析出对应的原始SQL是什么,而且,里面对应的是每一条具体行变更的内容.当然,你可以开启general log,但如果我们需要的只是记录 ...
- Talk About AWS Aurora for MySQL max_connections parameter Calculation | 浅谈AWS Aurora for MySQL数据库中 max_connections参数的计算
1. The Problem | 现象 When connect to the product environment database of my company, the Navicat show ...
- mysql中max_allowed_packet参数的配置方法(避免大数据写入或者更新失败)
修改方法 1.修改配置文件 可以编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改. 代码如下: max_allowed_packe ...
- mysql向表中某字段后追加一段字符串:
mysql向表中某字段后追加一段字符串:update table_name set field=CONCAT(field,'',str) mysql 向表中某字段前加字符串update table_n ...
- MySQL服务 - MySQL程序的配置文件、参数、变量查看
查看配置文件及读取顺序 MySQL的配置文件以.cnf结尾,可能会有多个,而不同版本的MySQL程序的读取配置文件的路径也都不同,要想获取MySQL读取配置文件的顺序可以通过以下指令查看: shell ...
- soapUI使用-DataSource获取oracle库中的参数
soapUI使用-DataSource获取oracle库中的参数 下载mysql和oracle驱动包:http://pan.baidu.com/s/1i3sy1MH 放在Program Files\S ...
- mysql的innodb中事务日志ib_logfile
mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...
随机推荐
- 虚拟机win7 自动休眠
1.问题描述:vmware 虚拟机,windows7系统.当一段时间不操作时,任务会休眠. 2.原因:win7系统自动休眠引起的. 3.解决方法:关闭win7系统的自动休眠. 依次打开“控制面板”-- ...
- [bzoj3295][Cqoi2011]动态逆序对_主席树
动态逆序对 bzoj-3295 Cqoi-2011 题目大意:题目链接. 注释:略. 想法:直接建立主席树. 由于是一个一个删除,所以我们先拿建立好的root[n]的权值线段树先把总逆序对求出来,接着 ...
- java GC垃圾回收机制G1、CMS
CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间.对于要求服务器响应速度的应用上,这种垃圾回收器非常适合.在启动JVM参数加上-XX:+UseConcMa ...
- Spring整体架构
Spring整体架构 Spring的整体架构 Spring框架是分层架构的,它包含了一系列的功能要素. Spring整体架构图 模块分类 1. Core Container Core Containe ...
- 【翻译自mos文章】在12c数据库中,哪种audit trail 受到支持?
在12c数据库中,哪种audit trail 受到支持? 来源于:What Audit Trail Types Are Supported For A 12c Database? (文档 ID 198 ...
- SharePoint 2013 关闭 customErrors
在SharePoint 2013上做开发时,如果不把customErrors 关掉,那调试起来那真叫一个费劲.在2013里,关闭customErrors 和2010一样,只要把文件路径改成15目录下即 ...
- iOS 基础类解析 - NSString、NSMutableString
iOS 基础类解析 - NSString 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...
- B1232 [Usaco2008Nov]安慰奶牛cheer 最小生成树
%%%小詹太巨啦!!!我就想直接最小生成树之后建树跑dfs,然后写跪了...然后看小詹博客之后恍然大悟,原来直接把边权改为w * 2 + 两边点权值就行了. 但是还是不对,为什么呢?原来我们起点走了三 ...
- 第2课 Git配置文件的妙用
2-1 "git config" 指令的用法 文件夹中".git"子文件夹内的config文件的优先权>登录账号的home directory中的.gi ...
- A Round Peg in a Ground Hole(圆与凸包)
http://poj.org/problem?id=1584 题意:判断所给的点能不能形成凸包,并判断所给的圆是否在凸包内. 改了好几天的一个题,今天才发现是输入顺序弄错了,办过的最脑残的事情..sa ...