MySQL field DATETIME truncates ISO8601

泄露秘密 提交于 2019-12-11 06:36:06

问题


Having MySQL/MariaDB table created like

CREATE TABLE `testTable` (
  `id` int(11) NOT NULL,
  `timeinfo` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

And inserted with full ISO8601 value like this:

MariaDB [test]> INSERT INTO testTable VALUES (1, '2500-12-31T00:00:00.000Z');
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [test]> show warnings;
+---------+------+-----------------------------------------------+
| Level   | Code | Message                                       |
+---------+------+-----------------------------------------------+
| Warning | 1265 | Data truncated for column 'timeinfo' at row 1 |
+---------+------+-----------------------------------------------+
1 row in set (0.00 sec)

MariaDB [test]> select * from testTable;
+----+---------------------+
| id | timeinfo            |
+----+---------------------+
|  1 | 2500-12-31 00:00:00 |
+----+---------------------+
1 row in set (0.01 sec)

I don't exactly see what is being truncated, the "Z" as in ZULU TimeZone ?


Is it possible to contain the information fully in MySQL/MariaDB field type?

If so which field data type should be used?


回答1:


The database expects another input that is why you get the warning

In MySQL DATETIME needs the following format

DATETIME - format: YYYY-MM-DD HH:MI:SS

This will not produce any warnings:

INSERT INTO testTable VALUES (, '2500-12-31 00:00:00');

Even with your warnings the database will fill you record.




回答2:


'Z' is a special time zone designator. It means "The time is in UTC timezone".

MySQL 5.7 datetime and timestamp types are unable to store timezone information. MySQL does not understand "Z".

If you need to store timezone, use additional field.



来源:https://stackoverflow.com/questions/45817809/mysql-field-datetime-truncates-iso8601

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!