> 数据库 > MySQL >

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: '5' 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);

 

 




(责任编辑:IT)