timestamp ---自动更新修改时间 与 记录首次插入时间

Posted zengkefu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了timestamp ---自动更新修改时间 与 记录首次插入时间相关的知识,希望对你有一定的参考价值。

自动更新修改时间:

mysql> create table z(a int ,b timestamp on update current_timestamp); 

mysql> insert into z select 1,current_timestamp;
mysql> select * from z;
+------+---------------------+
| a    | b                   |
+------+---------------------+
|    1 | 2016-06-26 07:18:45 |
+------+---------------------+
1 row in set (0.00 sec)
mysql> insert into z select 2,current_timestamp; 
Query OK, 1 row affected (0.18 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from z;
+------+---------------------+
| a    | b                   |
+------+---------------------+
|    1 | 2016-06-26 07:18:45 |
|    2 | 2016-06-26 07:19:05 |
+------+---------------------+
2 rows in set (0.00 sec)
mysql> update z set a=a+100 where a=1;
Query OK, 1 row affected (0.20 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from z;
+------+---------------------+
| a    | b                   |
+------+---------------------+
|  101 | 2016-06-26 07:19:48 |
|    2 | 2016-06-26 07:19:05 |
+------+---------------------+
2 rows in set (0.00 sec)

 

 记录首次插入时间:

mysql> create table x (a int, b timestamp default current_timestamp); 
Query OK, 0 rows affected (0.22 sec)

mysql> select * from x;
Empty set (0.00 sec)

mysql> insert into x(a) values(1); 
Query OK, 1 row affected (0.18 sec)

mysql> select * from x;
+------+---------------------+
| a    | b                   |
+------+---------------------+
|    1 | 2016-06-26 07:14:04 |
+------+---------------------+
1 row in set (0.00 sec)

mysql> insert into x(a) values(2);
Query OK, 1 row affected (0.19 sec)

mysql> select * from x;
+------+---------------------+
| a    | b                   |
+------+---------------------+
|    1 | 2016-06-26 07:14:04 |
|    2 | 2016-06-26 07:14:35 |
+------+---------------------+
2 rows in set (0.00 sec)

 

以上是关于timestamp ---自动更新修改时间 与 记录首次插入时间的主要内容,如果未能解决你的问题,请参考以下文章

数据库中存日期使用datetime与timestamp类型与Java相应收接类型的实验使用LocalDateTime与Timestamp类测试

mysql解决datetime与timestamp精确到毫秒的问题

MySQL 中 datetime 和 timestamp 的区别与选择

Hive中的Timestamp类型日期与Impala中显示不一致分析

java的类Date和Timestamp啥区别

JVM 三色标记法与读写屏障