mysql中timestamp的使用
mysql> create table t1 (
-> id mediumint(9) not null auto_increment,
-> name char(11) default null,
-> rq timestamp default current_timestamp on update current_timestamp,
-> primary key (id)
-> ) ;
query ok, 0 rows affected (0.04 sec)
mysql> select * from t1; +----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 1 | a | 2008-03-22 10:36:30 |
| 2 | b | 2008-03-22 10:36:33 |
+----+------+---------------------+
2 rows in set (0.00 sec)
mysql> update t1 set name='f'where id=2;
query ok, 1 row affected (0.00 sec)
rows matched: 1 changed: 1 warnings: 0
mysql> select * from t1;
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 1 | a | 2008-03-22 10:36:30 |
| 2 | f | 2008-03-22 10:37:04 |
+----+------+---------------------+
2 rows in set (0.00 sec)
注意:id=2的字段的日期字段值由「2008-03-22 10:36:33」自動變為了「」2008-03-22 10:37:04!
★思考:rq欄位自動變化有什麼好處呢?
★解決:現在要根據兩個表找出哪些記錄變化了,哪些記錄是增加的。
演示步驟:
★將t1表備份為t2表,注意備份表t2與t1表中的rq字段值也是完全相同的:
mysql> create table t2 as select * from t1
query ok, 2 rows affected (0.08 sec)
records: 2 duplicates: 0 warnings: 0
mysql> select * from t2;
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 1 | a | 2008-03-22 10:36:30 |
| 2 | f | 2008-03-22 10:37:04 |
+----+------+---------------------+
2 rows in set (0.00 sec)
★修改id=2的字段:
mysql> update t1 set name='g'where id=2;
query ok, 1 row affected (0.01 sec)
rows matched: 1 changed: 1 warnings: 0
★增加新記錄:
mysql> insert into t1(name) values('h');
query ok, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 1 | a | 2008-03-22 10:36:30 |
| 2 | g | 2008-03-22 10:43:38 |
| 3 | h | 2008-03-22 10:44:46 |
+----+------+---------------------+
3 rows in set (0.00 sec)
★查出新增加的記錄(id不在t2表中的):
mysql> select a.*
-> from t1 a
-> where not exists
-> (
-> select b.*
-> from t2 b
-> where a.id=b.id
-> );
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 3 | h | 2008-03-22 10:44:46 |
+----+------+---------------------+
1 row in set (0.00 sec)
★說明:查詢哪些記錄是被修改過的(id相同而rq不同)
mysql> select a.*
-> from t1 a
-> where exists
-> (
-> select b.*
-> from t2 b
-> where a.id=b.id
-> and a.rq!=b.rq
-> )
-> ;
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 2 | g | 2008-03-22 10:43:38 |
+----+------+---------------------+
1 row in set (0.00 sec)
★同理,也可以刪除哪些記錄是被刪除的(id在t2中卻不在t1中的):
mysql> delete from t1 where id=2;
query ok, 1 row affected (0.00 sec)
mysql> select a.*
-> from t2 a
-> where not exists
-> (
-> select b.*
-> from t1 b
-> where a.id=b.id
-> );
+----+------+---------------------+
| id | name | rq |
+----+------+---------------------+
| 2 | f | 2008-03-22 10:37:04 |
+----+------+---------------------+
1 row in set (0.00 sec)
說明:
1、在資料倉儲系統的設計中,經常會將系統1的定期送到系統2中中,但是系統1中的資料有增加的、刪除的和修改的。其中,哪些記錄修改的,一直是比較難於解決的問題。
2、在oracle、sqlserver都有類似的字段,所以上面的方法是通用的。
MYSQL中TIMESTAMP型別的預設值
mysql中timestamp型別的預設值 mysql中timestamp型別可以設定預設值,就像其他型別一樣。表 table create table t1 create table t1 p c int 11 not null,p time timestamp not null default ...
MySQL中TIMESTAMP型別的使用說明
今天使用了一下mysql中的timestamp型別,以往儲存時間都是使用整型的unix時間戳,而今天的表結構發生了變化,下面分享一下timestamp型別的基本使用方法。字段 updatetime 型別 timestamp 長度 空 預設 current timestamp 屬性 on update...
mysql表中時間timestamp設計
如圖所示,mysql資料庫中,當欄位型別為timestamp時,如果預設值取current timestamp,則在insert一條記錄時,end time的值自動設定為系統當前時間,如果勾選了 on update current timestamp 則時間欄位會隨著update命令進行實時更新,即...