美文网首页Mysql参数详解
mysql explicit_defaults_for_time

mysql explicit_defaults_for_time

作者: 飘风PF | 来源:发表于2018-01-01 15:18 被阅读0次

mysql 中有这样的一个默认行为,如果一行数据中某些列被更新了,如果这一行中有timestamp类型的列,那么么这个timestamp列的数据,也会被自动更新到 更新操作所发生的那个时间点;这个操作是由explicit_defaults_for_timestamp这个变更控制的

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 |
+------+---------------------+
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列的值都要显示指定,那么这一默认行为就算是关闭了。这种关闭只对关闭后创建的表有效,因为此参数已经改变了表结构,参数值的修改不会影响到已创建的表
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

相关文章

网友评论

    本文标题:mysql explicit_defaults_for_time

    本文链接:https://www.haomeiwen.com/subject/ugetnxtx.html