Tuesday, March 2, 2010

Update Data on insert/update

When we have to keep track of the row update, by having the timestamp of either created of the row or the update.


mysql> create table t (i int, d timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP);

mysql> insert into t (i) values (10);
mysql> insert into t (i) values (20);

mysql> select * from t;
+------+---------------------+
| i | d |
+------+---------------------+
| 10 | 2010-03-02 14:46:24 |
| 20 | 2010-03-02 14:46:27 |
+------+---------------------+

mysql> update t set i=30 where i=20;

mysql> select * from t;
+------+---------------------+
| i | d |
+------+---------------------+
| 10 | 2010-03-02 14:46:24 |
| 30 | 2010-03-02 14:50:52 |
+------+---------------------+

No comments:

Post a Comment