mysql 5.6 中 explicit_defaults_for_timestamp参数

一:

官方文档中关于explicit_defaults_for_timestamp参数说明如下:

 

 

  •  explicit_defaults_for_timestamp

    Introduced 5.6.6
    Deprecated 5.6.6
    Command-Line Format --explicit_defaults_for_timestamp=#
    System Variable Name explicit_defaults_for_timestamp
    Variable Scope Global, Session
    Dynamic Variable No
    Permitted Values Type boolean
    Default FALSE

    In MySQL, the TIMESTAMP data type differs in nonstandard ways from other data types:(在没有设置explicit_defaults_for_timestamp=1的情况下)

    • TIMESTAMP columns not explicitly declared with the NULL attribute are assigned the NOT NULL attribute. (Columns of other data types, if not explicitly declared as NOT NULL, permit NULL values.) Setting such a column to NULL sets it to the current timestamp.在默认情况下,如果TIMESTAMP列没有显示的指明null属性,那么该列会被自动加上not null属性(而其他类型的列如果没有被显示的指定not null,那么是允许null值的),如果往这个列中插入null值,会自动的设置该列的值为current timestamp值

    • The first TIMESTAMP column in a table, if not declared with the NULL attribute or an explicit DEFAULT or ON UPDATE clause, is automatically assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes.表中的第一个TIMESTAMP列,如果没有指定null属性或者没有指定默认值,也没有指定ON UPDATE语句。那么该列会自动被加上DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP属性。

    • TIMESTAMP columns following the first one, if not declared with the NULL attribute or an explicit DEFAULT clause, are automatically assigned DEFAULT '0000-00-00 00:00:00' (the zero” timestamp). For inserted rows that specify no explicit value for such a column, the column is assigned '0000-00-00 00:00:00' and no warning occurs.第一个TIMESTAMP列之后的其他的TIMESTAMP类型的列,如果没有指定null属性,也没有指定默认值,那么该列会被自动加上DEFAULT '0000-00-00 00:00:00'属性。如果insert语句中没有为该列指定值,那么该列中插入'0000-00-00 00:00:00',并且没有warning

    Those nonstandard behaviors remain the default for TIMESTAMP but as of MySQL 5.6.6 are deprecated and this warning appears at startup:在5.6.6及以后的版本中,如果在配置文件中没有指定explicit_defaults_for_timestamp参数,启动时error日志中会报如下错误

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

    As indicated by the warning, to turn off the nonstandard behaviors, enable the explicit_defaults_for_timestamp system variable at server startup. With this variable enabled, the server handles TIMESTAMP as follows instead:如果我们在启动的时候在配置文件中指定了explicit_defaults_for_timestamp=1,mysql会按照如下的方式处理TIMESTAMP 列

    • TIMESTAMP columns not explicitly declared as NOT NULL permit NULL values. Setting such a column to NULL sets it to NULL, not the current timestamp. 此时如果TIMESTAMP列没有显示的指定not null属性,那么默认的该列可以为null,此时向该列中插入null值时,会直接记录null,而不是current timestamp。

    • No TIMESTAMP column is assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. Those attributes must be explicitly specified. 不会自动的为表中的第一个TIMESTAMP列加上DEFAULT CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP属性,除非你在建表的时候显示的指明

    • TIMESTAMP columns declared as NOT NULL and without an explicit DEFAULT clause are treated as having no default value. For inserted rows that specify no explicit value for such a column, the result depends on the SQL mode. If strict SQL mode is enabled, an error occurs. If strict SQL mode is not enabled, the column is assigned the implicit default of '0000-00-00 00:00:00' and a warning occurs. This is similar to how MySQL treats other temporal types such as DATETIME.如果TIMESTAMP列被加上了not null属性,并且没有指定默认值。这时如果向表中插入记录,但是没有给该TIMESTAMP列指定值的时候,如果strict sql_mode被指定了,那么会直接报错。如果strict sql_mode没有被指定,那么会向该列中插入'0000-00-00 00:00:00'并且产生一个warning

    Note

    explicit_defaults_for_timestamp is itself deprecated because its only purpose is to permit control over now-deprecatedTIMESTAMP behaviors that will be removed in a future MySQL release. When that removal occurs,explicit_defaults_for_timestamp will have no purpose and will be removed as well.

    This variable was added in MySQL 5.6.6

二:测试
1.启动mysql时未设置explicit_defaults_for_timestamp=1
1)查看errorlog
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).  
2)创建测试表test_time
create table test_time(
time1 timestamp,
time2 timestamp,
time3 timestamp,
id int
);
##创建表的时候,没有显示的指定三个timestamp列的not null属性,也没有为三个列执行默认值
3)查看表结构
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. show create table test_time;  
  2. +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Table     | Create Table                                                                                                                                                                                                                                                                                            |  
  4. +-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  5. | test_time | CREATE TABLE `test_time` (  
  6.   `time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  
  7.   `time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
  8.   `time3` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
  9.   `id` int(11) DEFAULT NULL  
  10. ENGINE=InnoDB DEFAULT CHARSET=utf8 |  
##从表结构中可以看到 表中三个timestamp列都被自动设置为not null,并且表中第一个timestamp列被设置了DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP默认值,其他的timestamp列被加上了DEFAULT '0000-00-00 00:00:00'默认值
3.插入测试
insert into test_time select null,null,null,1;
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mysql> insert into test_time select null,null,null,1;  
  2. Query OK, 1 row affected (0.02 sec)  
  3. Records: 1  Duplicates: 0  Warnings: 0  
  4.   
  5. mysql> select * from test_time;  
  6. +---------------------+---------------------+---------------------+------+  
  7. | time1               | time2               | time3               | id   |  
  8. +---------------------+---------------------+---------------------+------+  
  9. | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 |    1 |  
  10. +---------------------+---------------------+---------------------+------+##往timestamp列插入null值时,会自动为该列设置为current time  
  11. 1 row in set (0.00 sec)  
  12.   
  13. mysql> insert into test_time(time1,id) select null,2;  
  14. Query OK, 1 row affected (0.04 sec)  
  15. Records: 1  Duplicates: 0  Warnings: 0  
  16.   
  17. mysql> select * from test_time;  
  18. +---------------------+---------------------+---------------------+------+  
  19. | time1               | time2               | time3               | id   |  
  20. +---------------------+---------------------+---------------------+------+  
  21. | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 |    1 |  
  22. | 2016-01-25 10:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |    2 |  
  23. +---------------------+---------------------+---------------------+------+##插入时未指定值的timestamp列中被插入了0000-00-00 00:00:00(非表中第一个timestamp列)  
  24. 2 rows in set (0.00 sec)  
  25.   
  26. mysql> insert into test_time(id) select 3;  
  27. Query OK, 1 row affected (0.03 sec)  
  28. Records: 1  Duplicates: 0  Warnings: 0  
  29.   
  30. mysql> select * from test_time;  
  31. +---------------------+---------------------+---------------------+------+  
  32. | time1               | time2               | time3               | id   |  
  33. +---------------------+---------------------+---------------------+------+  
  34. | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 | 2016-01-25 09:55:36 |    1 |  
  35. | 2016-01-25 10:00:00 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |    2 |  
  36. | 2016-01-25 10:01:41 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |    3 |  
  37. +---------------------+---------------------+---------------------+------+##插入时未指定值的第一个timestamp列中被插入了current time值  
  38. 3 rows in set (0.00 sec)  

2.启动mysql时设置explicit_defaults_for_timestamp=1
1)建表
create table test_time1(
time1 timestamp,
time2 timestamp,
time3 timestamp,
id int
);
##建表时没有为timestamp列指定null属性,也没有为其指定默认值
2) 查看表结构
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. show create table test_time1;  
  2. +------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  3. | Table      | Create Table                                                                                                                                                                                                      |  
  4. +------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
  5. | test_time1 | CREATE TABLE `test_time1` (  
  6.   `time1` timestamp NULL DEFAULT NULL,  
  7.   `time2` timestamp NULL DEFAULT NULL,  
  8.   `time3` timestamp NULL DEFAULT NULL,  
  9.   `id` int(11) DEFAULT NULL  
  10. ENGINE=InnoDB DEFAULT CHARSET=utf8 |  
##通过表结构我们看到,三个timestamp列都被设置为null,并且设置了默认值为null
3)插入测试
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. mysql> insert into test_time1 select null,null,null,1;  
  2. Query OK, 1 row affected (0.00 sec)  
  3. Records: 1  Duplicates: 0  Warnings: 0  
  4.   
  5. mysql> select * from test_time1;  
  6. +-------+-------+-------+------+  
  7. | time1 | time2 | time3 | id   |  
  8. +-------+-------+-------+------+  
  9. | NULL  | NULL  | NULL  |    1 |  
  10. +-------+-------+-------+------+##未显示指定timestamp列为not null时,能够向timestamp列中插入null值  
  11. 1 row in set (0.00 sec)  
  12.   
  13. mysql> insert into test_time1(time1,id) select null,2;  
  14. Query OK, 1 row affected (0.01 sec)  
  15. Records: 1  Duplicates: 0  Warnings: 0  
  16.   
  17. mysql> commit;  
  18. Query OK, 0 rows affected (0.00 sec)  
  19.   
  20. mysql> select * from test_time1;  
  21. +-------+-------+-------+------+  
  22. | time1 | time2 | time3 | id   |  
  23. +-------+-------+-------+------+  
  24. | NULL  | NULL  | NULL  |    1 |  
  25. | NULL  | NULL  | NULL  |    2 |  
  26. +-------+-------+-------+------+##插入时没有为timestamp列指定值时,自动插入null值  
  27. 2 rows in set (0.00 sec)  
  28.   
  29. mysql> insert into test_time1(id) select 3;  
  30. Query OK, 1 row affected (0.02 sec)  
  31. Records: 1  Duplicates: 0  Warnings: 0  
  32.   
  33. mysql> select * from test_time1;  
  34. +-------+-------+-------+------+  
  35. | time1 | time2 | time3 | id   |  
  36. +-------+-------+-------+------+  
  37. | NULL  | NULL  | NULL  |    1 |  
  38. | NULL  | NULL  | NULL  |    2 |  
  39. | NULL  | NULL  | NULL  |    3 |  
  40. +-------+-------+-------+------+##插入时没有为timestamp指定值时,自动插入null值(表中第一个timestamp列也是插入null值)  
  41. 3 rows in set (0.00 sec)  

4)指定了not null属性的timestamp列插入测试
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="color:#555555;">mysql> create table test_time2(  
  2.     -> time1 timestamp,  
  3.     -> time2 timestamp not null,  
  4.     -> time3 timestamp,  
  5.     -> id int  
  6.     -> );  
  7. Query OK, 0 rows affected (0.10 sec)  
  8.   
  9. mysql> insert into test_time2(time1,id) select null,1;  
  10. ERROR 1364 (HY000): Field 'time2' doesn't have a default value </span><span style="color:#ff0000;">##为timestamp列指定了not null属性,在stric sql_mode时,如果插入时该列没有指定值,会直接报错</span><span style="color:#555555;">  
  11. mysql> show variables like 'sql_mode';  
  12. +---------------+--------------------------------------------------------------------------------------+  
  13. | Variable_name | Value                                                                                |  
  14. +---------------+--------------------------------------------------------------------------------------+  
  15. | sql_mode      | NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |  
  16. +---------------+--------------------------------------------------------------------------------------+  
  17. 1 row in set (0.01 sec)  
  18.   
  19.   
  20. mysql> set session sql_mode='';  
  21. Query OK, 0 rows affected (0.00 sec)  
  22.   
  23. mysql> show variables like 'sql_mode';  
  24. +---------------+-------+  
  25. | Variable_name | Value |  
  26. +---------------+-------+  
  27. | sql_mode      |       |  
  28. +---------------+-------+  
  29. 1 row in set (0.00 sec)  
  30.   
  31. mysql> insert into test_time2(time1,id) select null,1;  
  32. Query OK, 1 row affected, 1 warning (0.01 sec)  
  33. Records: 1  Duplicates: 0  Warnings: 1 </span><span style="color:#ff0000;">##如果为timestamp列指定not null属性,在非stric sql_mode模式下,如果插入的时候该列没有指定值,那么会向该列中插入0000-00-00 00:00:00,并且产生告警</span><span style="color:#555555;">  
  34.   
  35. mysql> show warnings;  
  36. +---------+------+--------------------------------------------+  
  37. | Level   | Code | Message                                    |  
  38. +---------+------+--------------------------------------------+  
  39. | Warning | 1364 | Field 'time2' doesn't have a default value |  
  40. +---------+------+--------------------------------------------+  
  41. 1 row in set (0.00 sec)  
  42.   
  43. mysql> select * from test_time2;  
  44. +-------+---------------------+-------+------+  
  45. | time1 | time2               | time3 | id   |  
  46. +-------+---------------------+-------+------+  
  47. | NULL  | 0000-00-00 00:00:00 | NULL  |    1 |  
  48. +-------+---------------------+-------+------+  
  49. 1 row in set (0.00 sec)</span>