create table MYTABLE
{
MY_DATE int NOT NULL AUTO_INCREMENT,
NAME varchar(255) NOT NULL UNIQUE
};
INSERT INTO MYTABLE(NAME)values(jessica);
为什么会出现此错误?
ERROR 1364 (HY000): Field 'MY_DATE' doesn't have a default value
最佳答案
来自docs :
There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
我想你会发现,因为你没有索引 MY_DATE
,它可能会默默地忽略 AUTO_INCREMENT
选项(我不能保证,它是只是假设,但文档中的说明仍然相关)。
我在 AUTO_INCREMENT
上看到的所有样本都有(相当于):
PRIMARY KEY (MY_DATE)
或者,您可能在严格的 SQL 模式下运行。我见过的所有文档似乎都表明,在没有特定默认值的情况下,插入中未列出的列如果可为空,则为 NULL,如果不可为空,则为默认类型:
If you are not running in strict SQL mode, any column not explicitly given a value is set to its default (explicit or implicit) value. For example, if you specify a column list that does not name all the columns in the table, unnamed columns are set to their default values. Default value assignment is described in Section 10.1.4, “Data Type Default Values”.
对于整数类型,默认值为 0(无论如何都应该自动递增)。
但是,在严格的 SQL 模式下(来自 here):
an error occurs for transactional tables and the statement is rolled back.
关于sql - 错误 1364 (HY000) : Field 'MY_DATE' doesn't have a default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5000547/