mysql explicit_defaults_for_timestamp 變量的作用


mysql 中有這樣的一個默認行為,如果一行數據中某些列被更新了,如果這一行中有timestamp類型的列,那么么這個timestamp列的數據

也會被自動更新到 更新操作所發生的那個時間點;這個操作是由explicit_defaults_for_timestamp這個變更控制的

 

一、體驗一下mysql的默認更新行為

mysql> create table t(x int ,y timestamp);  -- 創建一個帶有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 |
+------+---------------------+
1 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 |
+------+---------------------+
1 row in set (0.00 sec)

 

二、如何關閉這一默認行為

  聰明的你一想到了,只要把explicit_defaults_for_timestamp這個變更設置為on;對於timestamp列的值都要顯示指定,那么這一默認行為就

  算是關閉了。來操作一把!

mysql> set @@global.explicit_defaults_for_timestamp=on; -- 把全局的設置為on 那么新的連接就會被設置成on
Query OK, 0 rows affected (0.00 sec)

mysql> set @@session.explicit_defaults_for_timestamp=on; -- 把當前連接explicit_defaults_for_timestamp設置為on
Query OK, 0 rows affected (0.00 sec)

mysql> update t set x=3 ; -- 第一次更新 結果是變了
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t;
+------+---------------------+
| x    | y                   |
+------+---------------------+
|    3 | 2017-06-07 13:57:21 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> update t set x=4 ; -- 第二次更新 結果還是變了
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t;
+------+---------------------+
| x    | y                   |
+------+---------------------+
|    4 | 2017-06-07 13:57:52 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> show variables like 'explicit_defaults_for_timestamp'; -- 全局發動生效
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | ON    |
+---------------------------------+-------+
1 row in set (0.00 sec)

mysql> show global variables like 'explicit_defaults_for_timestamp'; -- session 級別改動也升效了
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | ON    |
+---------------------------------+-------+
1 row in set (0.00 sec)

-- 發現了吧、就算把explicit_defaults_for_timestamp 設置成了on 也不會有效的

 

三、找問題出在了哪里

  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 

  2、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

 

 

四、結論

  explicit_defaults_for_timestamp 變量會直接影響表結構,也就是說explicit_defaults_for_timestamp的作用時間

  是在表定義的時候;你的update | insert 想通過它去改變行為已經太晚了!

 

五、如何解決這失控的場面

  1、改表結構

mysql> alter table t modify column y timestamp null default null; -- 解決辦法改表結構
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> update t set x=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t;
+------+---------------------+
| x    | y                   |
+------+---------------------+
|    1 | 2017-06-07 13:59:21 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> update t set x=2; -- 再次對比,發現時間沒有變哦!
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:59:21 |
+------+---------------------+
1 row in set (0.00 sec)

 

  

----

學習交流


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM