兩種資料類型都以「YYYY-MM-DD HH:MM:SS」格式儲存數據,並包含日期和時間。儘管有這些相似之處,但它們還是有以下區別-
以上是DATETIME 之間的一些主要區別和TIMESTAMP 資料類型,以下範例將示範它-
mysql> Create table test_datetime(time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.44 sec) mysql> INSERT INTO test_datetime (time) values (CURRENT_TIMESTAMP); Query OK, 1 row affected (0.04 sec) mysql> Select * from test_datetime; +---------------------+ | time | +---------------------+ | 2017-11-14 17:29:03 | +---------------------+ 1 row in set (0.00 sec) mysql> Create table test_timestamp(time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); Query OK, 0 rows affected (0.64 sec) mysql> INSERT INTO test_timestamp (time) values (CURRENT_TIMESTAMP); Query OK, 1 row affected (0.06 sec) mysql> Select * from test_timestamp; +---------------------+ | time | +---------------------+ | 2017-11-14 17:29:50 | +---------------------+ 1 row in set (0.00 sec)
現在,在以下查詢中,我們已將時區更改為UTC-05:00,並且具有TIMESTAMP 資料類型的表的結果也發生了更改。
mysql> SET @@session.time_zone = '-5:00'; Query OK, 0 rows affected (0.00 sec) mysql> Select * from test_timestamp; +---------------------+ | time | +---------------------+ | 2017-11-14 06:59:50 | +---------------------+ 1 row in set (0.00 sec)
mysql> Select * from test_datetime; +---------------------+ | time | +---------------------+ | 2017-11-14 17:29:03 | +---------------------+ 1 row in set (0.00 sec)
以上是MySQL DATETIME 和 TIMESTAMP 資料型別有什麼差別?的詳細內容。更多資訊請關注PHP中文網其他相關文章!