MySQL中的datetime與timestamp比較

來源:互聯網
上載者:User

標籤:style   http   color   os   io   strong   資料   2014   div   

相同

顯示

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

儲存

TIMESTAMP

1.4個位元組儲存(Time stamp value is stored in 4 bytes)

2.值以UTC格式儲存( it stores the number of milliseconds)

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

datetime

1.8個位元組儲存(8 bytes storage)

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

3.與時區不轉換(It has nothing to deal with the TIMEZONE and Conversion.)

執行個體對比

現在我來做個時區對他們的影響。

1.先插入一個資料insert into `t8` values(now(), now());

2.改變用戶端時區(東9區,日本時區)。

3.再次顯示插入的資料,變化了,timestamp類型的資料 增加了 1個小時

接下來 討論一些timestamp 的其他的屬性

1.null 是否為空白

timestamp 預設允許為 “非空”(not null by default), 如果你在定義“ts TIMESTAMP DEFAULT NULL” 是非法的。 可以指定為空白 null ,“ts TIMESTAMP NULL" ,這時可以在添加語句改變預設值。

ts2 TIMESTAMP NULL DEFAULT 0,ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP

default (一個表中只能有一個列選擇下面其中一種)

default CURRENT_TIMESTAMP

default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

ON UPDATE CURRENT_TIMESTAMP

ON UPDATE 見上2

MySQL中的datetime與timestamp比較

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.