mysql在一个表中存储创建时间和最近更新时间
2014-09-04 09:00
555 查看
今天写一段程序,在mysql中创建了一张表,其中有两个字段,一个是createdTS用来存储本记录创建时间,一个是modifiedTS用来存储本记录最近更新时间。
create table test(a varchar(3),
createdTS timestamp,
modifiedTS timestamp);
表的创建插入修改删除没有任何问题,但是查询出来的结果却有出入,问题就是createdTS实际显示的是最近更新时间,而modifiedTS显示创建时间。
这个问题在于----
使用show create table test;查看生成的create语句
CREATE TABLE `test` (
`a` char(1) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULTCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modified` timestamp NOT NULL DEFAULT'0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
那么调换createdTS和modifiedTS顺序可否,不是很合理。
最后我使用以下解决方案。
create table test(a varchar(3),
createdTS datetime,
modifiedTS timestamp);
搞定!!!
在插入的时候这样写
insert into test values('1',current_timestamp,null);
---------------------------------------------------------------
最后---
insert语句可以这样写
mysql> insert into testvalues('3',current_timestamp,null);
Query OK, 1 row affected (0.03 sec)
mysql> insert into testvalues('4',now(),null);
Query OK, 1 row affected (0.03 sec)
mysql> update test set a='33' where a='3';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update test set a='44' where a='4';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test;
+------+---------------------+---------------------+
| a | createdTS | modifiedTS |
+------+---------------------+---------------------+
| 11 | 2014-04-10 00:02:25 | 2014-04-1000:02:43 |
| 22 | 2014-04-10 00:02:27 | 2014-04-1000:02:48 |
| 33 | 2014-04-10 00:18:34 | 2014-04-1000:19:19 |
| 44 | 2014-04-10 00:18:46 | 2014-04-1000:19:28 |
+------+---------------------+---------------------+
create table test(a varchar(3),
createdTS timestamp,
modifiedTS timestamp);
表的创建插入修改删除没有任何问题,但是查询出来的结果却有出入,问题就是createdTS实际显示的是最近更新时间,而modifiedTS显示创建时间。
这个问题在于----
使用show create table test;查看生成的create语句
CREATE TABLE `test` (
`a` char(1) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULTCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`modified` timestamp NOT NULL DEFAULT'0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
那么调换createdTS和modifiedTS顺序可否,不是很合理。
最后我使用以下解决方案。
create table test(a varchar(3),
createdTS datetime,
modifiedTS timestamp);
搞定!!!
在插入的时候这样写
insert into test values('1',current_timestamp,null);
---------------------------------------------------------------
最后---
insert语句可以这样写
mysql> insert into testvalues('3',current_timestamp,null);
Query OK, 1 row affected (0.03 sec)
mysql> insert into testvalues('4',now(),null);
Query OK, 1 row affected (0.03 sec)
mysql> update test set a='33' where a='3';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update test set a='44' where a='4';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test;
+------+---------------------+---------------------+
| a | createdTS | modifiedTS |
+------+---------------------+---------------------+
| 11 | 2014-04-10 00:02:25 | 2014-04-1000:02:43 |
| 22 | 2014-04-10 00:02:27 | 2014-04-1000:02:48 |
| 33 | 2014-04-10 00:18:34 | 2014-04-1000:19:19 |
| 44 | 2014-04-10 00:18:46 | 2014-04-1000:19:28 |
+------+---------------------+---------------------+
相关文章推荐
- mysql创建一个改变固定字段才更新时间的时间戳timestamp
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析
- MYSQL 更新时间自动同步与创建时间默认值共存问题
- mysql创建与更新时间字段
- MySql创建表时插入自动更新时间戳报错:SQL执行错误#1293:Incorrect table definition;there can be only one TIMESTAMP ...
- MySQL--创建时间和更新时间字段
- oracel与mysql 查询从创建时间到更新时间的3天内的数据
- 最近写的一个Mysql存储过程
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析;MYSQL日期 字符串 时间戳互转
- Asp.net Membership的BUG:存储过程错误导致无法更新用户最近活动时间
- Mysql的创建和更新时间
- MYSQL 更新时间自动同步与创建时间默认值共存问题
- MySQL中同时存在创建和上次更新时间戳字段解决方法浅析
- MYSQL 储存过程 用户登录更新token创建时间,过期时间,账户是否冻结
- MYSQL 更新时间自动同步与创建时间默认值共存问题
- MYSQL 存储过程带参数 按时间创建相应的表
- MYSQL 更新时间自动同步与创建时间默认值共存问题
- 有段时间没有更新博客了,最近比较忙,今天和大家分享一个使用Swift实现的新特性功能吧
- mysql创建存储过程(根据时间字段来更新数据)
- mysql 存储,更新 时间戳转换成timestamp格式