mysql 中有這樣的一個默認行為,如果一行數(shù)據中某些列被更新了,如果這一行中有timestamp類型的列,那么么這個timestamp列的數(shù)據,也會被自動更新到 更新操作所發(fā)生的那個時間點;這個操作是由explicit_defaults_for_timestamp這個變更控制的
mysql> create table t(x int ,y timestamp); -- 創(chuàng)建一個帶有timestamp列的表
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t(x) values(1); -- 只插x列
Query OK, 1 row affected (0.00 sec)
mysql> select * from t; -- timestamp列會自動更新
+------+---------------------+
| x | y |
+------+---------------------+
| 1 | 2017-06-07 13:48:56 |
+------+---------------------+
row in set (0.00 sec)
mysql> update t set x=2 where x=1; -- update 時timestamp列還是會自動更新
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from t;
+------+---------------------+
| x | y |
+------+---------------------+
| 2 | 2017-06-07 13:49:21 |
+------+---------------------+
row in set (0.00 sec)
explicit_defaults_for_timestamp這個變更設置為on;對于timestamp列的值都要顯示指定,那么這一默認行為就算是關閉了。這種關閉只對關閉后創(chuàng)建的表有效,因為此參數(shù)已經改變了表結構,參數(shù)值的修改不會影響到已創(chuàng)建的表
1、explicit_defaults_for_timestamp=off 時表結構
CREATE TABLE `t` (
`x` int(11) DEFAULT NULL,
`y` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- `y` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
1、explicit_defaults_for_timestamp=on 時表結構
CREATE TABLE `t6` (
`x` int(11) DEFAULT NULL,
`y` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- `y` timestamp NULL DEFAULT NULL