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 |
||
在mysql中:
- timestamp列如果没有显式定义为null,默认会被设置为not null属性。(其它的数据类型如果没有显式定义为not null,默认是可以为null的)。设置timestamp的列值为null,会自动存储为当前timestamp
- 表中的第一个timestamp列,如果没有定义为null、定义default值或者on update,会自动分配default current_timestamp和on update current_timestamp属性
- 表中第一个timestamp列之后的所有timestamp列,如果没有被定义为null、定义default值,会自动被指定默认值'0000-00-00 00:00:00'。在插入时,如果没有指定这些列的值,会自动指定为'0000-00-00 00:00:00',且不会产生警告
mysql> create table timestamp_eg(
-> id int not null auto_increment,
-> time1 timestamp,
-> time2 timestamp,
-> time3 timestamp NOT NULL DEFAULT '2010-01-01 00:00:00',
-> time4 timestamp,
-> primary key(id));
Query OK, 0 rows affected (0.01 sec) mysql> insert into timestamp_eg(id) values(1);
Query OK, 1 row affected (0.00 sec) mysql> select * from timestamp_eg;
+----+---------------------+---------------------+---------------------+---------------------+
| id | time1 | time2 | time3 | time4 |
+----+---------------------+---------------------+---------------------+---------------------+
| 1 | 2015-12-16 09:23:33 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.01 sec) mysql> update timestamp_eg set id=2 where id=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from timestamp_eg;
+----+---------------------+---------------------+---------------------+---------------------+
| id | time1 | time2 | time3 | time4 |
+----+---------------------+---------------------+---------------------+---------------------+
| 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec) mysql> insert into timestamp_eg(id,time4) values(3,'2011-01-01 00:00:00');
Query OK, 1 row affected (0.00 sec) mysql> select * from timestamp_eg;
+----+---------------------+---------------------+---------------------+---------------------+
| id | time1 | time2 | time3 | time4 |
+----+---------------------+---------------------+---------------------+---------------------+
| 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 |
| 3 | 2015-12-16 09:28:04 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 2011-01-01 00:00:00 |
+----+---------------------+---------------------+---------------------+---------------------+
2 rows in set (0.00 sec) mysql> update timestamp_eg set id=4 where id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from timestamp_eg;
+----+---------------------+---------------------+---------------------+---------------------+
| id | time1 | time2 | time3 | time4 |
+----+---------------------+---------------------+---------------------+---------------------+
| 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 |
| 4 | 2015-12-16 09:28:24 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 2011-01-01 00:00:00 |
+----+---------------------+---------------------+---------------------+---------------------+
2 rows in set (0.00 sec) mysql>
从MySQL5.6.6这种默认设置的方法被废弃了。在MySQL启动时会出现以下警告:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
要想取消该警告,在启动mysql时,my.cnf中加入
[mysqld]
explicit_defaults_for_timestamp=true
修改该参数后,timestamp类型的列的处理方式也会发生变化:
- timestamp列如果没有显式定义为not null,则支持null属性。设置timestamp的列值为null,就不会被设置为current timestamp
- 不会自动分配default current_timestamp和on update current_timestamp属性,这些属性必须显式指定
- 声明为not null且没有显式指定默认值是没有默认值的。表中插入列,又没有给timestamp列赋值时,如果是严格sql模式,会抛出一个错误;如果严格sql模式没有启用,该列会赋值为’0000-00-00 00:00:00′,同时出现一个警告。(这和mysql处理其它时间类型数据一样,如datetime)
mysql> create table timestamp_02(
-> id int not null auto_increment,
-> time1 timestamp not null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.03 sec) mysql> insert into timestamp_02(id) values(1);
ERROR 1364 (HY000): Field 'time1' doesn't have a default value
mysql>
explicit_defaults_for_timestamp参数的更多相关文章
- mysql 5.6 中 explicit_defaults_for_timestamp参数
mysql 5.6 中 explicit_defaults_for_timestamp参数 一: 官方文档中关于explicit_defaults_for_timestamp参数说明如下: expli ...
- MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数
安装MySQL时,有warning: [root@localhost mysql]# scripts/mysql_install_db --user=mysql Installing MySQL sy ...
- mysql explicit_defaults_for_timestamp参数
在mysql中:- timestamp列如果没有显式定义为null,默认会被设置为not null属性.(其它的数据类型如果没有显式定义为not null,默认是可以为null的).设置timesta ...
- 【MySQL】explicit_defaults_for_timestamp 参数详解
简介:explicit_defaults_for_timestamp 系统变量决定MySQL服务端对timestamp列中的默认值和NULL值的不同处理方法. 此变量自MySQL 5.6.6 版本引入 ...
- MySQL——my.cnf参数设置说明
以下为个人总结的MySQL配置文件参数说明,如有错误,烦请大佬们留言指正,本人将第一时间修改.2019-12-10 12:32:08 [mysqld] server- # Mysql唯一标识,一个集群 ...
- MySQL中有关TIMESTAMP和DATETIME的总结
一.MySQL中如何表示当前时间? 其实,表达方式还是蛮多的,汇总如下: CURRENT_TIMESTAMP CURRENT_TIMESTAMP() NOW() LOCALTIME LOCALTIME ...
- 【MySQL】探究之TIMESTAMP
背景 之前有业务反馈表中start_time,end_time时间字段随着时间的推移被自动更新,这可不是业务意愿,说的严重点是要出故障的. MySQL中有DATE,DATETIME,TIMESTAMP ...
- mysql5.6 timestamp
1.timestamp 默认值 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 在创建新记录和修改现有记录的时候都对这个数据列刷新 CURRENT_TIME ...
- 设置TIMESTAMP和DATETIME的自动初始化及自动更新
最近有一个关于MySQL版本升级的事,涉及到一些关于时间类型的细节问题需要查明,因此到官网找到相关文章,翻出来比较方便自己理解,博客这里也贴一下. 参考官网网址: https://dev.mysql. ...
随机推荐
- ImportError DLL load failed: %1 不是有效的 Win32 应用程序
操作系统:win7 64位,安装mysqldb 后提示:ImportError DLL load failed: %1 不是有效的 Win32 应用程序,是由于安装的32位的 MySQL-Python ...
- Oracle使用技巧及PL/SQL Developer配置
Oracle使用技巧及PL/SQL Developer配置 摘自:http://livenzhao.spaces.live.com/blog/cns!6E368BE9F6DDD872!595.entr ...
- Office办公 SVG的图片文件如何保存为PNG
用浏览器打开,然后右击图片另存为PNG 再用PS打开可以看到就是没有背景的PNG图片了
- Invalid volume failure config value: 1
原因: hdfs-site.xml中的配置为: <property> <name>dfs.datanode.failed.volumes.tolerated</name& ...
- Multipathing for Software iSCSI
see also:http://www.vmware.com/files/pdf/techpaper/vmware-multipathing-configuration-software-iSCSI- ...
- 010-Go 操作PostgreSQL数据库2
1:sql脚本 create table post( id serial primary key, content text, author ) ) 2:post.go package post im ...
- JAVA开发人员画图表总结(ECHARTS)
随着大数据的到来,越来越多的数据需求需要开发,而这些需求不可避免需要使用JS画出图表,而大多后端JAVA开发人员对JS不太熟悉,导致身心倍受折磨,今天记录以下最近我使用echarts的步骤,供参考: ...
- 写了一个简单的Linux Shell用来下载文件
#!/bin/sh ; i<; i=i+ )); do # 利用spider来探测请求的资源是否存在,并把请求的结果写入到一个文件 wget --spider --http-user=usern ...
- yii框架的部署方法
yii框架(yii framework)的部署方法 刚開始学习的人来说,部署yii框架还是有一定难度的,Yii是一个基于组件.用于开发大型 Web 应用的高性能 PHP 框架.Yii提供了今日Web ...
- 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
本文翻译自 https://support.microsoft.com/zh-cn/help/168958/how-to-export-an-instantiation-of-a-standard-t ...