timestamp存儲
⑴ datetime和timestamp的區別
datetime和timestamp是出現在MySQL和SqlServer資料庫中的。
MySQL中datetime和timestamp的區別:
(一)范圍不同。
「datetime」 以'YYYY-MM-DD HH:MM:SS'格式檢索和顯示DATETIME值。支持的范圍為'1000-01-01 00:00:00'到'9999-12-31 23:59:59'TIMESTAMP值不能早於1970或晚於2037
(二)儲存不同。
1,TIMESTAMP
①4個位元組儲存(Time stamp value is stored in 4 bytes)
②值以UTC格式保存( it stores the number of milliseconds)
③時區轉化 ,存儲時對當前的時區進行轉換,檢索時再轉換回當前的時區。
2,datetime
①8個位元組儲存(8 bytes storage)
②實際格式儲存(Just stores what you have stored and retrieves the same thing which you have stored.)
③與時區無關(It has nothing to deal with the TIMEZONE and Conversion.)
註:TIMESTAMP列的顯示格式與datetime列相同。
SqlServer中datetime和timestamp在中用法區別基本都差不多。
datetime :
SqlServer中用兩個 4 位元組的整數內部存儲 datetime 數據類型的值。第一個 4 位元組存儲 base date(即 1900 年 1 月 1 日)之前或之後的天數。基礎日期是系統參考日期。不允許早於 1753 年 1 月 1 日的 datetime 值。另外一個 4 位元組存儲以午夜後毫秒數所代表的每天的時間。
註:smalldatetime 數據類型存儲日期和每天的時間,但精確度低於 datetime。SQL Server 將 smalldatetime 的值存儲為兩個 2 位元組的整數。第一個 2 位元組存儲 1900 年 1 月 1 日後的天數。另外一個 2 位元組存儲午夜後的分鍾數。日期范圍從1900 年 1 月 1 日到 2079 年 6 月 6 日,精確到分鍾。
timestamp:
timestamp 這種數據類型表現自動生成的二進制數,確保這些數在資料庫中是唯一的。timestamp 一般用作給錶行加版本戳的機制。存儲大小為 8 位元組。