MySQL插入无效日期

标签 mysql date types

再说一次,这个qn是mysql认证指南书上的练习qns...

问题

Here's the structure of a table typetest with three columns (number, string, and dates). As- sume the server is running in MySQL's “forgiving” SQL mode.

mysql> DESCRIBE typetest;
+--------+---------------------+------+-----+---------+-------+
| Field | Type  | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| number | tinyint(3) unsigned | YES    |   | NULL  |   |
+--------+---------------------+------+-----+---------+-------+

You perform the following INSERT operation on table typetest:

INSERT INTO typetest VALUES (1000,'yoodoo','999-12-31');

What data values will actually be stored in the table? Provide a short explanation. 

回答

+--------+--------+------------+
| number | string | dates   |
+--------+--------+------------+
|   255 | yoodo | 0000-00-00 |
+--------+--------+------------+

The inserted number 1000 is too big to fit in the TINYINT UNSIGNED column, so the highest pos- sible value (255) is inserted. 'yoodoo' is too long for a CHAR(5) column and is thus truncated to five  characters.  '999-12-31' is  a  date  that  is  earlier  than  the  earliest  possible  DATE value ('1000-01-01'). This is interpreted as an invalid date, so the “zero” date is stored.

当我尝试将“999-12-31”插入日期时,我得到了 0999-12-31。我在认证指南的某些地方读到 mysql 可能能够插入 1000-01-01 到 9999-12-31 范围之外的日期。但是在考试中我要回答什么?

最佳答案

您可以阅读 Constraints on Invalid Data部分。不幸的是,它不能直接回答您的问题。它说“如果日期完全错误(服务器无法存储它),特殊的“零”日期值'0000-00-00'将存储在列中。”,我会说存储 0999 不是'不可能...

关于MySQL插入无效日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1326159/

相关文章:

mysql - 如何提高 MySQL 查询的性能

ruby-on-rails - Ruby stdlib 和 Rails stdlib 中的日期类不同

ios - 获取总数包括 2 个日期之间的月份

Scala 类型约束来检查参数值

Python ctypes 和 void const 指针

r - 确定数据框列的数据类型

java - 更新 MySQL 数据库中的数据

mysql - 是否可以在其他项目(Spring Boot)中拥有资源?

php - JSON 转换丢失一半数据?

sorting - SSRS tablix 排序不适用于日期列