MySQL中的datetime與timestamp比較
相同
顯示
TIMESTAMP列的顯示格式與DATETIME列相同。換句話說,顯示寬度固定在19字符,并且格式為YYYY-MM-DD HH:MM:SS。
不同
范圍
datetime 以'YYYY-MM-DD HH:MM:SS'格式檢索和顯示DATETIME值。支持的范圍為'1000-01-01 00:00:00'到'9999-12-31 23:59:59'TIMESTAMP值不能早于1970或晚于2037
儲(chǔ)存
TIMESTAMP
1.4個(gè)字節(jié)儲(chǔ)存(Time stamp value is stored in 4 bytes)
2.值以UTC格式保存( it stores the number of milliseconds)
3.時(shí)區(qū)轉(zhuǎn)化 ,存儲(chǔ)時(shí)對(duì)當(dāng)前的時(shí)區(qū)進(jìn)行轉(zhuǎn)換,檢索時(shí)再轉(zhuǎn)換回當(dāng)前的時(shí)區(qū)。
datetime
1.8個(gè)字節(jié)儲(chǔ)存(8 bytes storage)
2.實(shí)際格式儲(chǔ)存(Just stores what you have stored and retrieves the same thing which you have stored.)
3.與時(shí)區(qū)無關(guān)(It has nothing to deal with the TIMEZONE and Conversion.)
實(shí)例對(duì)比
現(xiàn)在我來做個(gè)時(shí)區(qū)對(duì)他們的影響。
1.先插入一個(gè)數(shù)據(jù)insert into `t8` values(now(), now());
2.改變客戶端時(shí)區(qū)(東9區(qū),日本時(shí)區(qū))。
3.再次顯示插入的數(shù)據(jù),變化了,timestamp類型的數(shù)據(jù) 增加了 1個(gè)小時(shí)

接下來 討論一些timestamp 的其他的屬性
1.null 是否為空
timestamp 默認(rèn)允許為 “非空”(not null by default), 如果你在定義“ts TIMESTAMP DEFAULT NULL” 是非法的。 可以指定為空 null ,“ts TIMESTAMP NULL" ,這時(shí)可以在添加語句改變默認(rèn)值。
ts2 TIMESTAMP NULL DEFAULT 0, |
default (一個(gè)表中只能有一個(gè)列選擇下面其中一種)
default CURRENT_TIMESTAMP |
【編輯推薦】