MySQL插入无效日期

时间:2009-08-25 05:08:04

标签: 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. 

ANSWER

+--------+--------+------------+
| 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之间插入日期的地方。但在考试中我该回答什么?

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

Found this reported as a bug.官方的回答是你可以做到,但它不受支持,而且你是独立的。

答案 2 :(得分:0)

考试和现实生活是不同的事情。如果在书中写下另一个,很难证明你的意见。但我会回答我自己测试过的。

答案 3 :(得分:0)

Mysql日期范围:1000-01-01至9999-12-31(参考)

在这里您尝试插入'999-12-31'。由于此日期不在上述范围内,因此mysql将值默认为0