TIMESTAMP 类型表示的是一个时间点,这个时间点所表明的时间跟它的表示方式无关,就像一个数值跟用几进制表示无关同样。举个例子,若是一个时间戳用 CST 表示是 2018-11-22T08:00:00+08:00
,那么这个时间戳所表明的时间点也能够表示为 2018-11-22T00:00:00Z
。在 MySQL 中,无论当前系统时区是哪一个时区,TIMESTAMP 类型都会以 UTC 时区存储以当前时区显示,而 DATETIME 保存的是一个时间表示,不会作这种转换。再来看一个例子,系统当前时区为 CSTcode
查看时区信息it
MariaDB [devkit]> show variables like 'time_zone'; +---------------+--------+ | Variable_name | Value | +---------------+--------+ | time_zone | +08:00 | +---------------+--------+ 1 row in set (0.001 sec)
建立表并插入数据table
MariaDB [devkit]> CREATE TABLE `test_table` ( -> `id` int(11) NOT NULL AUTO_INCREMENT, -> `timestamp` timestamp NULL DEFAULT NULL, -> `datetime` datetime DEFAULT NULL, -> PRIMARY KEY (`id`) -> ) ENGINE=InnoDB; Query OK, 0 rows affected (0.055 sec) MariaDB [devkit]> insert into `test_table` (`timestamp`, `datetime`) values ('2018-11-22 08:00:00', '2018-11-22 08:00:00'); Query OK, 1 row affected (0.005 sec)
查看数据test
MariaDB [devkit]> select * from `test_table`; +----+---------------------+---------------------+ | id | timestamp | datetime | +----+---------------------+---------------------+ | 1 | 2018-11-22 08:00:00 | 2018-11-22 08:00:00 | +----+---------------------+---------------------+ 1 row in set (0.000 sec)
修改时区并再次查看数据date
MariaDB [devkit]> set time_zone = 'UTC'; Query OK, 0 rows affected (0.000 sec) MariaDB [devkit]> select * from `test_table`; +----+---------------------+---------------------+ | id | timestamp | datetime | +----+---------------------+---------------------+ | 1 | 2018-11-22 00:00:00 | 2018-11-22 08:00:00 | +----+---------------------+---------------------+ 1 row in set (0.000 sec)
比较 2 次的显示能够发现,TIMESTAMP 类型的值在不一样的时区下显示不一样,无论哪一种表示指向的都是同一个时间点。DATETIME 类型的值在不一样的时区下显示相同,能够理解为表明的是时钟上的一个固定的时间表示。select