开发者

ERROR 1364 (HY000): Field 'MY_DATE' doesn't have a default value

create table MYTABLE
{
    MY_DATE int NOT NULL AUTO_INCREMENT,
    NAME varchar(255) NOT NULL UNIQUE
开发者_JAVA技巧};
INSERT INTO MYTABLE(NAME)values(jessica);

Why do I get this error?

ERROR 1364 (HY000): Field 'MY_DATE' doesn't have a default value


From the docs:

There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.

I think you'll find that, because you're not indexing MY_DATE, it may be silently ignoring the AUTO_INCREMENT option (I can't guarantee that, it's just supposition, but the note in the documentation is still relevant).

All the samples I can see on the AUTO_INCREMENT stuff have (the equivalent of):

PRIMARY KEY (MY_DATE)

Alternatively, you may be running in strict SQL mode. All the docs I've seen seem to indicate that, in the absence of a specific default, unlisted columns in an insert will get NULL if they're nullable, or the type default if the not nullable:

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”.

For an integral type, that default is 0 (which should kick in the auto increment anyway).

However, in strict SQL mode (from here):

an error occurs for transactional tables and the statement is rolled back.


Which version of MySQL are you using?

I'm using 5.1.41 community and the create table SQL gives

SQL Error (1075): Incorrect table definition; there can be only one auto column and it must be defined as a key

Next correct it to the below

create table MYTABLE
 (
 MY_DATE int NOT NULL AUTO_INCREMENT primary key,
 NAME varchar(255) NOT NULL UNIQUE
 );

The insert statement

INSERT INTO MYTABLE(NAME)values(jessica);

Results in

SQL Error (1054): Unknown column 'jessica' in 'field list'

Because it has not been quoted. Once that is corrected to the below:

INSERT INTO MYTABLE(NAME)values('jessica');

It works. You don't have to supply any values for the auto_increment primary key, but you can.

Now, let's talk about MySQL quirks. You can optionally include the column, and set a value of NULL. You can also give it a SPECIFIC value and MySQL will happily use it, even if it is defined as auto_increment.

INSERT INTO MYTABLE(my_date,NAME)values(41,'jessica2');
INSERT INTO MYTABLE(my_date,NAME)values(null,'jessica3');
INSERT INTO MYTABLE(NAME)values('jessica4');
DELETE FROM MYTABLE where my_date=43;
INSERT INTO MYTABLE(my_date,NAME)values(3,'jessica5');
INSERT INTO MYTABLE(NAME)values('jessica?');
select * from mytable;

When you set the number specifically to 41, the next number becomes 42, then 43. It allows us to specifically use 3, even then though 43 was deleted before we used the specific number 3, the next value is still 44.

Output

MY_DATE  NAME
41       jessica2
42       jessica3
3        jessica5
44       jessica?


You don't supply a value for MY_DATE in your INSERT statement, and there is no default value defined. (And as paxdiablo points out, there can't be a default value defined in this situation.) Unintuitively, you do need to give a value for MY_DATE, and if that value is zero or null then MySQL will assign the next autoincrement value. Like this:

insert into mytable values(null,'jessica');


Edit this file /etc/mysql/mysql.conf.d/mysqld.cnf

add the below statement under [mysqld]

sql_mode =

This is working perfectly for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜