TIMESTAMP 与 explicit_defaults_for_timestamp
在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的更多相关文章
- 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` 与 TIMESTAMP
考察下面的 SQL 脚本: CREATE TABLE test1( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(20), ts1 ...
- [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 ...
- TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option
先解决他 详细不详解 在初始化 加上 --explicit_defaults_for_timestamp=true 即可
- 关于mysql字段时间类型timestamp默认值为当前时间问题
今天把应用部署到AWS上发现后台修改内容提交后程序报错,经过排查发现是更新数据的时候,有张数据表中的一个timestamp类型的字段默认值变成了"0000-00-00 00:00:00.00 ...
- MySQL中有关TIMESTAMP和DATETIME的总结
一.MySQL中如何表示当前时间? 其实,表达方式还是蛮多的,汇总如下: CURRENT_TIMESTAMP CURRENT_TIMESTAMP() NOW() LOCALTIME LOCALTIME ...
- mysql5.7下的timestampn Error : Invalid default value for 'timestamp'
表格创建是爆了个错 Error : Invalid default value for 'timestamp' 参考:http://www.jb51.net/article/71107.htm 这版本 ...
- 【MySQL】探究之TIMESTAMP
背景 之前有业务反馈表中start_time,end_time时间字段随着时间的推移被自动更新,这可不是业务意愿,说的严重点是要出故障的. MySQL中有DATE,DATETIME,TIMESTAMP ...
- [mysql] timestamp自动更新和初始化
1.概述 在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践.尤其是可能需要做数据同步或者对数据新鲜度有要求的表.举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个 ...
随机推荐
- [jobdu]矩形覆盖
推导一下,就是斐波那契数列那样的.但是要注意的是,int存不下,算一下需要long long才行,因为是指数级上升的. #include <cstdio> #define LEN 75 # ...
- 使用haproxy做负载均衡时保持客户端真实的IP
haproxy里添加设置项 option forwardfor option httpclose apache的日志格式修改 LogFormat "MY IP=%{X-Forwarded-F ...
- diamond专题(三)—— diamond架构
大家好,这次为大家带来的是diamond的架构,架构如下图所示: 对该图进行一些说明: 1.作为一个配置中心,diamond的功能分为发布和订阅两部分.因为diamond存放的是持久数据,这些数据的变 ...
- 第K顺序统计量
1.第K顺序统计量概念 在一个由n个元素组成的集合中,第k个顺序统计量是该集合中第k小的元素.例如,最小值是第1顺序统计量,最大值是第n顺序统计量. 2.求Top K元素与求第K顺序统计量不同 Top ...
- [King.yue]Ext.JS 弹出窗体取值赋值
//从Grid取值 var name = Ext.getCmp(gridGridID).getView().getSelectionModel().getSelection()[0].data.Nam ...
- [liu yanling]测试用例作用
⒈指导测试的实施 测试用例主要适用于集成测试.系统测试和回归测试.在实施测试时测试用例作为测试的标准,测试人员一定要按照测试用例严格按用例项目和测试步骤逐一实施测试.并对测试情况记录在测试用例管理软件 ...
- 【JS】Intermediate8:jQuery:AJAX
1.$.ajax is the main method, allowing you to manually construct your AJAX request 2.eg: gets some da ...
- HDU-4414 Finding crosses 水题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4414 直接暴力判断即可. //STATUS:C++_AC_15MS_232KB #include &l ...
- JQuery Dialog 禁用X按钮关闭对话框,-摘自网络
JQuery Dialog 禁用X按钮关闭对话框,禁用ESC键关闭代码如下:$("#div1").dialog({ closeOnEscape: false, open: ...
- 【转】科普Spark,Spark是什么,如何使用Spark
本博文是转自如下链接,为了方便自己查阅学习和他人交流.感谢原博主的提供! http://www.aboutyun.com/thread-6849-1-1.html http://www.aboutyu ...