问题
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