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小时的行数据,或者是将一个 ...
随机推荐
- SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-007-表单验证@Valid、Error
一. Starting with Spring 3.0, Spring supports the Java Validation API in Spring MVC . No extra config ...
- 133. Clone Graph
题目: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. ...
- RxJava学习(一)
注意:文字和图片转载自抛物线博客 参考:http://gank.io/post/560e15be2dca930e00da1083 RxJava 到底是什么 一个词:异步. RxJava 在 GitHu ...
- Covariance and Contravariance in C#, Part One
http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.a ...
- How to: Host and Run a Basic Windows Communication Foundation Service
This is the third of six tasks required to create a Windows Communication Foundation (WCF) applicati ...
- Java实现Dijkstra算法求最短路径
任务描述:在一个无向图中,获取起始节点到所有其他节点的最短路径描述 Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层 ...
- Html——footer的使用
html部分 <div class="container"> <div class="body"></div> <di ...
- java 页面url传值中文乱码的解决方法
parent.window.location.href 和 iframe中src的乱码问题.要在这两个url地址中传中文,必须加编码,然后再解码.编码:encodeURI(encodeURI(&quo ...
- uiview scale
http://stackoverflow.com/questions/3946797/cgaffinetransformmakescale-makes-uiview-jump-to-original- ...
- iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”
iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...