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)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值