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 |
在mysql中:
- timestamp列如果沒有顯式定義為null,默認會被設置為not null屬性。(其它的數據類型如果沒有顯式定義為not null,默認是可以為null的)。設置timestamp的列值為null,會自動存儲為當前timestamp
- 表中的第一個timestamp列,如果沒有定義為null、定義default值或者on update,會自動分配default current_timestamp和on update current_timestamp屬性
- 表中第一個timestamp列之后的所有timestamp列,如果沒有被定義為null、定義default值,會自動被指定默認值'0000-00-00 00:00:00'。在插入時,如果沒有指定這些列的值,會自動指定為'0000-00-00 00:00:00',且不會產生警告
mysql> create table timestamp_eg( -> id int not null auto_increment, -> time1 timestamp, -> time2 timestamp, -> time3 timestamp NOT NULL DEFAULT '2010-01-01 00:00:00', -> time4 timestamp, -> primary key(id)); Query OK, 0 rows affected (0.01 sec) mysql> insert into timestamp_eg(id) values(1); Query OK, 1 row affected (0.00 sec) mysql> select * from timestamp_eg; +----+---------------------+---------------------+---------------------+---------------------+ | id | time1 | time2 | time3 | time4 | +----+---------------------+---------------------+---------------------+---------------------+ | 1 | 2015-12-16 09:23:33 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 | +----+---------------------+---------------------+---------------------+---------------------+ 1 row in set (0.01 sec) mysql> update timestamp_eg set id=2 where id=1; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from timestamp_eg; +----+---------------------+---------------------+---------------------+---------------------+ | id | time1 | time2 | time3 | time4 | +----+---------------------+---------------------+---------------------+---------------------+ | 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 | +----+---------------------+---------------------+---------------------+---------------------+ 1 row in set (0.00 sec) mysql> insert into timestamp_eg(id,time4) values(3,'2011-01-01 00:00:00'); Query OK, 1 row affected (0.00 sec) mysql> select * from timestamp_eg; +----+---------------------+---------------------+---------------------+---------------------+ | id | time1 | time2 | time3 | time4 | +----+---------------------+---------------------+---------------------+---------------------+ | 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 | | 3 | 2015-12-16 09:28:04 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 2011-01-01 00:00:00 | +----+---------------------+---------------------+---------------------+---------------------+ 2 rows in set (0.00 sec) mysql> update timestamp_eg set id=4 where id=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from timestamp_eg; +----+---------------------+---------------------+---------------------+---------------------+ | id | time1 | time2 | time3 | time4 | +----+---------------------+---------------------+---------------------+---------------------+ | 2 | 2015-12-16 09:25:01 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 0000-00-00 00:00:00 | | 4 | 2015-12-16 09:28:24 | 0000-00-00 00:00:00 | 2010-01-01 00:00:00 | 2011-01-01 00:00:00 | +----+---------------------+---------------------+---------------------+---------------------+ 2 rows in set (0.00 sec) mysql>
從MySQL5.6.6這種默認設置的方法被廢棄了。在MySQL啟動時會出現以下警告:
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
要想取消該警告,在啟動mysql時,my.cnf中加入
[mysqld] explicit_defaults_for_timestamp=true
修改該參數后,timestamp類型的列的處理方式也會發生變化:
- timestamp列如果沒有顯式定義為not null,則支持null屬性。設置timestamp的列值為null,就不會被設置為current timestamp
- 不會自動分配default current_timestamp和on update current_timestamp屬性,這些屬性必須顯式指定
- 聲明為not null且沒有顯式指定默認值是沒有默認值的。表中插入列,又沒有給timestamp列賦值時,如果是嚴格sql模式,會拋出一個錯誤;如果嚴格sql模式沒有啟用,該列會賦值為’0000-00-00 00:00:00′,同時出現一個警告。(這和mysql處理其它時間類型數據一樣,如datetime)
mysql> create table timestamp_02( -> id int not null auto_increment, -> time1 timestamp not null, -> primary key(id) -> ); Query OK, 0 rows affected (0.03 sec) mysql> insert into timestamp_02(id) values(1); ERROR 1364 (HY000): Field 'time1' doesn't have a default value mysql>