mysql中的CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP
2017-09-30 20:46
567 查看
在mysql中, 如果类型为时间的列设置了CURRENT_TIMESTAMP, 那么在insert一条新记录的收, 时间字段自动获取到当前时间, 如果设置了ON UPDATE CURRENT_TIMESTAMP, 则时间字段随着update命令的更新和实时变化。 如果两个属性都设置了, 那么时间字段默认为当前时间, 且随着记录的更新而自动变化。 注意, 如果仅仅是update操作, 但id(如下)并没有实际变更, 则时间值也不会变化。
如果时间字段没有设置如上两个属性, 则默认拥有如上两个属性, 有兴趣的可以试试:
来看下实际操作:
mysql> create table tb_test(
-> id int,
-> time1 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql>
mysql>
mysql> insert into tb_test set id = 1;
Query OK, 1 row affected (0.04 sec)
mysql> select * from tb_test;
+------+---------------------+
| id | time1 |
+------+---------------------+
| 1 | 2017-09-30 20:54:52 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> update tb_test set id = 2 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb_test;
+------+---------------------+
| id | time1 |
+------+---------------------+
| 2 | 2017-09-30 20:55:30 |
+------+---------------------+
1 row in set (0.00 sec)
mysql>
如果时间字段没有设置如上两个属性, 则默认拥有如上两个属性, 有兴趣的可以试试:
create table test( id int, expire_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP )ENGINE=InnoDB DEFAULT CHARSET=utf8;和
create table test( id int, expire_time timestamp NOT NULL )ENGINE=InnoDB DEFAULT CHARSET=utf8;是等价的。
来看下实际操作:
mysql> create table tb_test(
-> id int,
-> time1 timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.04 sec)
mysql>
mysql>
mysql>
mysql> insert into tb_test set id = 1;
Query OK, 1 row affected (0.04 sec)
mysql> select * from tb_test;
+------+---------------------+
| id | time1 |
+------+---------------------+
| 1 | 2017-09-30 20:54:52 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> update tb_test set id = 2 where id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from tb_test;
+------+---------------------+
| id | time1 |
+------+---------------------+
| 2 | 2017-09-30 20:55:30 |
+------+---------------------+
1 row in set (0.00 sec)
mysql>
相关文章推荐
- 取消MySQL timestamp列默认ON UPDATE CURRENT_TIMESTAMP
- 取消MySQL timestamp列默认ON UPDATE CURRENT_TIMESTAMP
- 取消MySQL timestamp列默认ON UPDATE CURRENT_TIMESTAMP
- mysql 5.5与5.6 timestamp 字段 DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP的区别
- 取消MySQL timestamp列默认ON UPDATE CURRENT_TIMESTAMP
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
- MySQL 5.6.5之前版本不支持多条DEFAULT CURRENT_TIMESTAMP和ON UPDATE CURRENT TIMESTAMP
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
- MySQL CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 详解
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
- mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
- mysql timestamp的两个默认值:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- mysql CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- Update MySQL timestamp on update
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- Mysql [Err] 1293 there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP
- there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
- there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause