在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>

mysql explicit_defaults_for_timestamp参数的更多相关文章

  1. mysql 5.6 中 explicit_defaults_for_timestamp参数

    mysql 5.6 中 explicit_defaults_for_timestamp参数 一: 官方文档中关于explicit_defaults_for_timestamp参数说明如下: expli ...

  2. 【查阅】mysql配置文件/参数文件重要参数笔录(my.cnf)

    持续更新,积累自己对参数的理解 [1]my.cnf参数 [client]port = 3306socket = /mysql/data/3306/mysql.sockdefault-character ...

  3. 1201MySQL配置文件mysql.ini参数详解

    转自http://www.cnblogs.com/feichexia/archive/2012/11/27/mysqlconf.html my.ini(Linux系统下是my.cnf),当mysql服 ...

  4. (转)MySQL配置文件mysql.ini参数详解、MySQL性能优化

    本文转自:http://www.cr173.com/html/18331_1.html my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数 ...

  5. MySQL配置文件mysql.ini参数详解、MySQL性能优化

    my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section.  ...

  6. MySQL配置文件mysql.ini参数详解

    my.ini(Linux系统下是my.cnf),当mysql服务器启动时它会读取这个文件,设置相关的运行环境参数. my.ini分为两块:Client Section和Server Section. ...

  7. 在Linux最大打开文件数限制下 MySQL 对参数的调整

    http://www.actionsky.com/docs/archives/78  2016年4月7日  周文雅 目录 1 起因 2 说明 3 MySQL调整参数的方式 3.1 计算 request ...

  8. skip-grant-tables:非常有用的mysql启动参数

    skip-grant-tables:非常有用的mysql启动参数   介绍一个非常有用的mysql启动参数—— --skip-grant-tables.顾名思义,就是在启动mysql时不启动grant ...

  9. MySql配置参数很全的Mysql配置参数说明

    MySql配置参数 很全的Mysql配置参数说明 1. back_log 指定MySQL可能的连接数量.当MySQL主线程在很短的时间内得到非常多的连接请求,该参数就起作用,之后主线程花些时间(尽管很 ...

随机推荐

  1. 最小生成树 - 克鲁斯卡尔 - 并查集 - 边稀疏 - O(E * logE)

    #define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<cstring> #include<algorithm ...

  2. CentOS7系统更换YUM Repo源

    CentOS7系统更换YUM Repo源 备份原镜像 sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.re ...

  3. UA 列表

    设备 系统 浏览器 User-Agent iPhone IOS QQ浏览器 Mozilla/5.0 (iPhone 84; CPU iPhone OS 10_3_3 like Mac OS X) Ap ...

  4. jsp 简单标签开发

    实现simpleTag接口的标签通常称为是简单标签,简单标签一共定义了5个方法 setJspContext方法 setParent方法 getParent方法 setJspBody方法 doTag方法 ...

  5. httpclient cookie保持会话

    COOKIE保持回话 httpclient4.x自带维护回话的功能,只要使用同一个httpclient且未关闭连接,就可以使用相同的回话来访问其他要求登陆验证的服务. 如果需要使用HttpClient ...

  6. 下拉选择框 Spinner的用法。

    代码如下: package com.lixu.xialakuang; import android.app.Activity; import android.content.Context; impo ...

  7. C++虚析构函数的作用

    注:本文内容来源于zhice163博文,感谢作者的整理. 1.为什么基类的析构函数是虚函数? 在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生. 下面转自网络:源地址  ...

  8. 使用MyEclipse开发Java EE应用:EJB项目开发初探(下)

    你开学,我放价!MyEclipse线上狂欢继续!火热开启中>> [MyEclipse最新版下载] 三.EJB 3.x项目中的持久性支持 当创建EJB 3.x项目时,作为选项您可以添加JPA ...

  9. PowerShell基础

    1. 一些命令像Get-SPSite有不同的参数变量.这些变量通过-?展示出来. Get-SPSite -? 2. F3 在当前行显示最近的一条命令结果 3. F7 显示之前执行过的命令列表 4. 显 ...

  10. 安装 Java 6

    从服务器下载 Linux 64位 jdk 到本地.下载地址:\\192.167.100.225\share\Tool\JAVA\jdk-6u34-linux-x64.bin 给安装文件添加执行权限 $ ...