两种数据类型都以“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中文网其他相关文章!