在MySQL 5.6.6之前,TIMESTAMP的默认行为:

  • TIMESTAMP列如果没有明确声明NULL属性,默认为NOT NULL。(而其他数据类型,如果没有显示声明为NOT NULL,则允许NULL值。)设置TIMESTAMP的列值为NULL,会自动存储为当前timestamp。
  • 表中的第一个TIMESTAMP列,如果没有声明NULL属性、DEFAULT或者 ON UPDATE,会自动分配 DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 属性。
  • 表中第二个TIMESTAMP列,如果没有声明为NULL或者DEFAULT子句,默认自动分配’0000-00-00 00:00:00′。插入行时没有指明改列的值,该列默认分配’0000-00-00 00:00:00′,且没有警告。
因为默认情况下充许为空,当插入值时,分两种情况:第一列与第二列情况
explicit_defaults_for_timestamp=false [默认值]
mysql> create table timestamp(id int not null auto_increment,time1 timestamp,time2 timestamp,primary key(id))engine=innodb;
Query OK, 0 rows affected (0.27 sec) mysql> insert into timestamp(id) values(1);
Query OK, 1 row affected (0.21 sec) mysql> select * from timestamp;
+----+---------------------+---------------------+
| id | time1 | time2 |
+----+---------------------+---------------------+
| 1 | 2016-07-02 17:12:28 | 0000-00-00 00:00:00 |
+----+---------------------+---------------------+
1 row in set (0.19 sec) mysql>
mysql> show create table timestamp;
CREATE TABLE `timestamp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 2列TIMESTAMP未声明为NULL的默认行为
timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下:

1. CURRENT_TIMESTAMP
  当要向数据库执行insert操作时,如果有个timestamp字段属性设为 CURRENT_TIMESTAMP,则无论这个字段有没有set值都插入当前系统时间 2. ON UPDATE CURRENT_TIMESTAMP
  当执行update操作时,并且字段有ON UPDATE CURRENT_TIMESTAMP属性。则字段无论值有没有变化,它的值也会跟着更新为当前UPDATE操作时的时间。

从MySQL5.6.6开始这种默认设置的方法被废弃了。在MySQL启动时会出现以下警告:

1
2
3
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated.
Please use --explicit_defaults_for_timestamp server option
(seedocumentation for more details).

关闭警告,在my.cnf中加入

1
2
[mysqld]
explicit_defaults_for_timestamp=true

重启MySQL后错误消失,这时TIMESTAMP的行为如下:

  • TIMESTAMP如果没有显示声明NOT NULL,是允许NULL值的,可以直接设置改列为NULL,而没有默认填充行为。
  • TIMESTAMP不会默认分配DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP属性。
mysql>  create table timestamp1(id int not null auto_increment,time1 timestamp,time2 timestamp,primary key(id))engine=innodb;
Query OK, 0 rows affected (0.11 sec) mysql> insert into timestamp1(id) values(1);
Query OK, 1 row affected (0.18 sec) mysql> show create table timestamp1;
CREATE TABLE `timestamp1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`time1` timestamp NULL DEFAULT NULL,
`time2` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 mysql> select * from timestamp1;
+----+-------+-------+
| id | time1 | time2 |
+----+-------+-------+
| 1 | NULL | NULL |
+----+-------+-------+
1 row in set (0.18 sec)
mysql> set sql_mode="STRICT_TRANS_TABLES";
Query OK, 0 rows affected (0.00 sec) mysql> create table timestamp4(id int not null auto_increment,time1 timestamp not null,time2 timestamp not null,primary key(id))engine=innodb;
Query OK, 0 rows affected (0.03 sec) mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","0000-00-00 00:00:00";
Query OK, 1 row affected (0.17 sec)
Records: 1 Duplicates: 0 Warnings: 0 mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","5";
ERROR 1292 (22007): Incorrect datetime value: '' for column 'time2' at row 1 mysql> set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE";
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> insert into timestamp4 select 1,"0000-00-00 00:00:00","0000-00-00 00:00:00";
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'time1' at row 1
mysql> set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE";
Query OK, 0 rows affected, 2 warnings (0.00 sec) mysql> insert into timestamp4 select 5,"2000-10-10 10:20:10","2000-10-10 10:20:00";
Query OK, 1 row affected (0.18 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> insert into timestamp4 select 7,"2000-10-00 10:20:10","2000-10-10 10:20:11"; 日期中不能有00
ERROR 1292 (22007): Incorrect datetime value: '2000-10-00 10:20:10' for column 'time1' at row 1
mysql> create table y( a int not null auto_increment primary key,b timestamp Default CURRENT_TIMESTAMP);   
当有记录插入时,把当前日期与时间写入到 b字段中,但更新不会改变值 mysql> create table y1( a int not null auto_increment primary key,b timestamp on update CURRENT_TIMESTAMP);
当有记录插入时,NULL写入到B 字段中,当该行update时,b列值为当前更新时间值 mysql> create table y2( a int not null auto_increment primary key,b timestamp default current_timestamp on update CURRENT_TIMESTAMP);
当插入时,有一个默认值,随着当前行的更新,b列值也随着更新为当前更新时间 mysql> insert into y(a) values(null);
Query OK, 1 row affected (0.17 sec) mysql> insert into y1(a) values(null);
Query OK, 1 row affected (0.17 sec) mysql> insert into y2(a) values(null);
Query OK, 1 row affected (0.17 sec) mysql> select * from y;
+---+---------------------+
| a | b |
+---+---------------------+
| 1 | 2016-07-02 18:56:59 |
+---+---------------------+
1 row in set (0.00 sec) mysql> select * from y1;
+---+------+
| a | b |
+---+------+
| 1 | NULL |
+---+------+
1 row in set (0.00 sec) mysql> select * from y2;
+---+---------------------+
| a | b |
+---+---------------------+
| 1 | 2016-07-02 18:57:08 |
+---+---------------------+
1 row in set (0.00 sec) mysql> update y set a=2 where a=1;
Query OK, 1 row affected (0.20 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from y;
+---+---------------------+
| a | b |
+---+---------------------+
| 2 | 2016-07-02 18:56:59 |
+---+---------------------+
1 row in set (0.01 sec) mysql> update y1 set a=2 where a=1;
Query OK, 1 row affected (0.17 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from y1;
+---+---------------------+
| a | b |
+---+---------------------+
| 2 | 2016-07-02 19:00:12 |
+---+---------------------+
1 row in set (0.00 sec) mysql> update y2 set a=2 where a=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from y2;
+---+---------------------+
| a | b |
+---+---------------------+
| 2 | 2016-07-02 19:00:35 |
+---+---------------------+
1 row in set (0.00 sec)
设置正常日期格式:

严格日期格式:
set sql_mode="STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE";
手动指定默认值:
create table y2( a int not null auto_increment primary key,b timestamp default current_timestamp on update CURRENT_TIMESTAMP);

TIMESTAMP 与 explicit_defaults_for_timestamp的更多相关文章

  1. MySQL 5.6 中的 TIMESTAMP 和 explicit_defaults_for_timestamp 参数

    安装MySQL时,有warning: [root@localhost mysql]# scripts/mysql_install_db --user=mysql Installing MySQL sy ...

  2. MySQL `explicit_defaults_for_timestamp` 与 TIMESTAMP

    考察下面的 SQL 脚本: CREATE TABLE test1( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(20), ts1 ...

  3. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

    Linux下安装MySQL执行scripts/mysql_install_db --user=mysql脚本时,报错如下: Filling help tables...2019-12-24 16:46 ...

  4. TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option

    先解决他 详细不详解 在初始化 加上 --explicit_defaults_for_timestamp=true 即可

  5. 关于mysql字段时间类型timestamp默认值为当前时间问题

    今天把应用部署到AWS上发现后台修改内容提交后程序报错,经过排查发现是更新数据的时候,有张数据表中的一个timestamp类型的字段默认值变成了"0000-00-00 00:00:00.00 ...

  6. MySQL中有关TIMESTAMP和DATETIME的总结

    一.MySQL中如何表示当前时间? 其实,表达方式还是蛮多的,汇总如下: CURRENT_TIMESTAMP CURRENT_TIMESTAMP() NOW() LOCALTIME LOCALTIME ...

  7. mysql5.7下的timestampn Error : Invalid default value for 'timestamp'

    表格创建是爆了个错 Error : Invalid default value for 'timestamp' 参考:http://www.jb51.net/article/71107.htm 这版本 ...

  8. 【MySQL】探究之TIMESTAMP

    背景 之前有业务反馈表中start_time,end_time时间字段随着时间的推移被自动更新,这可不是业务意愿,说的严重点是要出故障的. MySQL中有DATE,DATETIME,TIMESTAMP ...

  9. [mysql] timestamp自动更新和初始化

    1.概述 在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践.尤其是可能需要做数据同步或者对数据新鲜度有要求的表.举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个 ...

随机推荐

  1. 基于Qt5.x的QCA加解密开源项目的编译过程

    记录一下Qt5.x下的QCA的编译过程 需要注意的几点 针对windows环境 1.编译QCA源码前,必须先安装openssl二进制文件 2.需要安装好vs2008等 ( 64-bit Visual ...

  2. 浏览器助手,请求拦截,后台模拟键鼠操作,页内嵌入JS

    http://www.cnblogs.com/ /// <summary>        /// 网页浏览器助手        /// 请求拦截,后台模拟键鼠操作,页内嵌入JS       ...

  3. objective-c 强大的布尔类型

    objective-c codes: #import <Foundation/Foundation.h> BOOL areIntsDifferent(int thing1,int thin ...

  4. Android 常用UI控件之TabHost(3)在4.0不显示图标的解决方案

    1,自定义 TabWidget 上每个tab的view 2,用多个图片

  5. Application.EnableVisualStyles();

    Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);  Application ...

  6. jquery formatCurrency货币格式化处理

    // This file is part of the jQuery formatCurrency Plugin. // // The jQuery formatCurrency Plugin is ...

  7. 利用OpenXml生成Word2007文档

    一.OpenXml简介 利用C#生成Word文档并非一定要利用OpenXml技术,至少可以使用微软提供的Office相关组件来编程,不过对于Office2007(确切的说是Word.Excel和Pow ...

  8. Get ListView items from other windows z

    This is more difficult than one might think. In order to get the information you're looking for, you ...

  9. powershell学习

    PowerShell 调试器 在开始运行处,输入powershell ISE回车即可 PowerShell 与操作系统版本 powershell在windows server 2008上自带,但最好在 ...

  10. 【转】Install MATLAB 2013a on CentOS 6.4 x64 with mode silent

    首先要下载安装光盘. Matlab801_MacUnix.iso [root@db-172-16-3-150 mnt]# md5sum /ssd1/Matlab801_MacUnix.iso  0d3 ...