MySQL datetime && timestamp

MySQL datetime && timestampmysql

轉自:http://www.tech-recipes.com/rx/22599/mysql-datetime-vs-timestamp-data-type/ sql


The temporal data types in MySQL can be confusing. Hopefully, this example and discussion will help to explain the differences in the timestamp and datetime data types.數據庫

From the MySQL reference:this

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ‘YYYY-MM-DD HH:MM:SS’ format. The supported range is ‘1000-01-01 00:00:00′ to ‘9999-12-31 23:59:59′.spa


The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of ‘1970-01-01 00:00:01′ UTC to ‘2038-01-19 03:14:07′ UTC.設計

A major difference between these two data types is that TIMESTAMP data type values are converted from current time zone to UTC for storage purpose and converted back from UTC to current time zone when used. The datetime data type values are unchanged in relation to time zone.code

This example is a good exercise in demonstrating the difference between these two data types.orm

mysql> show variables like '%time_zone%';
+------------------+---------------------+
| Variable_name    |  Value              |
+------------------+---------------------+
| system_time_zone | India Standard Time |
| time_zone        | Asia/Calcutta       |
+------------------+---------------------+
2 rows in set (0.00 sec)

You can see our current time zone information. Under this environment, let us create a table with the two data types and populate it with the same temporal information.對象

create table datedemo
(
 mydatetime datetime,
 mytimestamp timestamp
);
Query OK, 0 rows affected (0.05 sec)
insert into datedemo values ((now()), (now()));
Query OK, 1 row affected (0.02 sec)
select * from datedemo;
+---------------------+---------------------+
| mydatetime          | mytimestamp         |
+---------------------+---------------------+
| 2011-08-21 14:11:09 | 2011-08-21 14:11:09 |
+---------------------+---------------------+
1 row in set (0.00 sec)

At this point the datetime and timestamp data types have remained the exact same values. Let us change the time zone see the results.ip

SET TIME_ZONE = "america/new_york";
Query OK, 0 rows affected (0.00 sec)
 select * from datedemo;
+---------------------+---------------------+
| mydatetime          | mytimestamp         |
+---------------------+---------------------+
| 2011-08-21 14:11:09 | 2011-08-21 04:41:09 |
+---------------------+---------------------+
1 row in set (0.00 sec)

The above example shows how the TIMESTAMP date type changed the values after changing the time-zone to ‘america/new_work’ where DATETIME is unchanged.

=================================

datetime

1. 佔用8個字節

2. 容許爲空值,能夠自定義值,系統不會自動修改其值。

3. 實際格式儲存(Just stores what you have stored and retrieves the same thing which you have stored.)

4. 與時區無關(It has nothing to deal with the TIMEZONE and Conversion.)

5. 不能夠設定默認值,因此在不容許爲空值的狀況下,必須手動指定datetime字段的值才能夠成功插入數據。

6. 能夠在指定datetime字段的值的時候使用now()變量來自動插入系統的當前時間。

結論:datetime類型適合用來記錄數據的原始的建立時間,由於不管你怎麼更改記錄中其餘字段的值,

datetime字段的值都不會改變,除非你手動更改它。


timestamp

1. 佔用4個字節

2. 容許爲空值,可是不能夠自定義值,因此爲空值時沒有任何意義。

3. TIMESTAMP值不能早於1970或晚於2037。這說明一個日期,例如'1968-01-01',雖然對於DATETIME或DATE值是有效的,但對於TIMESTAMP值卻無效,若是分配給這樣一個對象將被轉換爲0。

4.值以UTC格式保存( it stores the number of milliseconds)

5.時區轉化 ,存儲時對當前的時區進行轉換,檢索時再轉換回當前的時區。

6. 默認值爲CURRENT_TIMESTAMP(),其實也就是當前的系統時間。

7. 數據庫會自動修改其值,因此在插入記錄時不須要指定timestamp字段的名稱和timestamp字段的值,你只須要在設計表的時候添加一個timestamp字段便可,插入後該字段的值會自動變爲當前系統時間。

8. 之後任什麼時候間修改表中的記錄時,對應記錄的timestamp值會自動被更新爲當前的系統時間。

結論:timestamp類型適合用來記錄數據的最後修改時間,由於只要你更改了記錄中其餘字段的值,timestamp字段的值都會被自動更新。

================END================

相關文章
相關標籤/搜索