ITPub博客

首页 > 数据库 > MySQL > [Warning] TIMESTAMP with implicit DEFAULT value is deprecated

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated

原创 MySQL 作者:goolen 时间:2014-12-20 08:34:52 0 删除 编辑
MySQL 5.6启动的时候提示警告信息如下:
2014-12-20 08:02:25 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. 
Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

警告信息说timestamp的默认值设置被废弃了,需要配置explicit_defaults_for_timestamp参数

查看文档说明:
explicit_defaults_for_timestamp system variable. If this variable is enabled, the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP 
 clauses that specify automatic initialization and updating are available, but are not assigned to any TIMESTAMP column unless explicitly included 
 in the column definition.

如果配置了参数explicit_defaults_for_timestamp,那么,timestamp列不再默认拥有DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP属性,除非在建表语句中显示
的指定timestamp列的DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP属性

下面来看看默认的timestamp的属性:
mysql> use goolen;
Database changed

创建表,不指定任何属性值
mysql> create table g2 ( id int ,time1 timestamp,time2 timestamp );
Query OK, 0 rows affected (0.21 sec)

查看表结构,mysql自动给表的第一歌timestamp列加上了DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP属性
第二列timestamp的默认值为“0000-00-00 00:00:00”
并且表中所有timestamp列全部是not null
mysql> desc g2;
+-------+-----------+------+-----+---------------------+-----------------------------+
| Field | Type      | Null | Key | Default             | Extra                       |
+-------+-----------+------+-----+---------------------+-----------------------------+
| id    | int(11)   | YES  |     | NULL                |                             |
| time1 | timestamp | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| time2 | timestamp | NO   |     | 0000-00-00 00:00:00 |                             |
+-------+-----------+------+-----+---------------------+-----------------------------+
3 rows in set (0.00 sec)

mysql> insert into g2(id) values(1);
Query OK, 1 row affected (0.05 sec)

mysql> insert into g2(id) values(2);
Query OK, 1 row affected (0.05 sec)

mysql> select * from g2;
+------+---------------------+---------------------+
| id   | time1               | time2               |
+------+---------------------+---------------------+
|    1 | 2014-12-20 08:04:54 | 0000-00-00 00:00:00 |
|    2 | 2014-12-20 08:04:58 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> update g2 set id = 3 where id = 1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from g2;
+------+---------------------+---------------------+
| id   | time1               | time2               |
+------+---------------------+---------------------+
|    3 | 2014-12-20 08:05:40 | 0000-00-00 00:00:00 |
|    2 | 2014-12-20 08:04:58 | 0000-00-00 00:00:00 |
+------+---------------------+---------------------+
2 rows in set (0.00 sec)

配置explicit_defaults_for_timestamp参数后,再测试
mysql> use goolen;
Database changed
mysql> 
mysql> 
mysql> 
mysql> create table g3 ( id int ,time1 timestamp,time2 timestamp ); 
Query OK, 0 rows affected (0.21 sec)

mysql> insert into g3(id) values(1); 
Query OK, 1 row affected (0.04 sec)

mysql> insert into g3(id) values(2);
Query OK, 1 row affected (0.06 sec)

mysql> select * from g3;
+------+-------+-------+
| id   | time1 | time2 |
+------+-------+-------+
|    1 | NULL  | NULL  |
|    2 | NULL  | NULL  |
+------+-------+-------+
2 rows in set (0.00 sec)

mysql> desc g3;
+-------+-----------+------+-----+---------+-------+
| Field | Type      | Null | Key | Default | Extra |
+-------+-----------+------+-----+---------+-------+
| id    | int(11)   | YES  |     | NULL    |       |
| time1 | timestamp | YES  |     | NULL    |       |
| time2 | timestamp | YES  |     | NULL    |       |
+-------+-----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> update g2 set id = 3 where id = 1;
Query OK, 0 rows affected (0.02 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> select * from g3;
+------+-------+-------+
| id   | time1 | time2 |
+------+-------+-------+
|    1 | NULL  | NULL  |
|    2 | NULL  | NULL  |
+------+-------+-------+
2 rows in set (0.00 sec)

发现所有timestamp列默认都是可以null,并且没有DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP属性
insert和update数据,timestamp列的值都为null

如果指定timestamp列为not null
mysql> create table g4 ( id int ,time timestamp not null); 
Query OK, 0 rows affected (0.20 sec)

严格模式下不指定timestamp列的insert语句会报错
mysql> insert into g4(id) values(1);
ERROR 1364 (HY000): Field 'time' doesn't have a default value
mysql> 
mysql> select @@sql_mode;
+--------------------------------------------+
| @@sql_mode                                 |
+--------------------------------------------+
| STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> set session sql_mode='';
Query OK, 0 rows affected (0.00 sec)

mysql> select @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            |
+------------+
1 row in set (0.00 sec)

在非严格模式下的insert,会自动插入一个值“0000-00-00 00:00:00”
mysql> insert into g4(id) values(1);
Query OK, 1 row affected, 1 warning (0.05 sec)

mysql> select * from g4;
+------+---------------------+
| id   | time                |
+------+---------------------+
|    1 | 0000-00-00 00:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> update g4 set id = 2;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from g4;
+------+---------------------+
| id   | time                |
+------+---------------------+
|    2 | 0000-00-00 00:00:00 |
+------+---------------------+
1 row in set (0.00 sec)

显示指定了default current_timestamp on update current_timestamp
mysql> create table g5 ( id int ,time timestamp not null default current_timestamp on update current_timestamp);
Query OK, 0 rows affected (0.16 sec)

mysql> insert into g5(id) values(1); 
Query OK, 1 row affected (0.04 sec)

mysql> select * from g5;
+------+---------------------+
| id   | time                |
+------+---------------------+
|    1 | 2014-12-20 08:15:10 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> update g5 set id = 2; 
Query OK, 1 row affected (0.05 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from g5;
+------+---------------------+
| id   | time                |
+------+---------------------+
|    2 | 2014-12-20 08:15:24 |
+------+---------------------+
1 row in set (0.00 sec)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23249684/viewspace-1373936/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-10-20

  • 博文量
    121
  • 访问量
    732236