简体   繁体   中英

What is wrong with this mysql query?

This works:

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date)
)

...while this:

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date)
)

...results in:

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

I added primary key but still get error.

Quote Bug #45987, ERROR 1075 (42000): Incorrect table definition :

Actually, this is not a server bug, just a difference between MyISAM (assumed as default by that manual page) and InnoDB storage engine documented elsewhere: http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html

MyISAM is no longer the default engine; InnoDB is.

Also: http://bugs.mysql.com/bug.php?id=60104

Conclusion

InnoDB doesn't support AUTO_INCREMENT but no primary key being defined for the table, but MyISAM does. Choose which engine (MyISAM or InnoDB) you want to use, and deal with the CREATE TABLE statement accordingly.

Well I'm not a mysql expert, but

shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT

is an auto increment, not defined as Key....

Given the many unclear or misleading error messages we experience in this business, I can't imagine a clearer error message ....

+1 for using unsigned etc.. but you say... it can be not null... BUT by default its 0.. and for me is 0 always null ? and a primary key thats auto_increment is NEVER null.. Your pretty in the good direction but do it this way:

create table shoutbox_shout (
    shout_id int unsigned auto_increment primary key,
    user_id int unsigned not null,
    shout_date datetime,
    message mediumtest not null
)engine=innodb;

@Brandon_R: I get the same error using command line of MySQL 5.0.67-community-nt, using the code like this works though --

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  PRIMARY KEY (shout_id)
);

you MUST define an auto_increment column as a primary key/key

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  primary key (shout_id) -- primary key
)

or

CREATE TABLE shoutbox_shout (
  shout_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT UNSIGNED NOT NULL DEFAULT 0,
  shout_date INT UNSIGNED NOT NULL DEFAULT 0,
  message MEDIUMTEXT NOT NULL,
  KEY shout_date (shout_date),
  key (shout_id) -- key
)

You should also define your engine type - i would recommend innodb.

Nice to see you using unsigned integer datatypes without specifying silly optional display widths !!

Doesn't the error explain this succinctly? The AUTO_INCREMENT column — if you have one — must be a key column.

As you demonstrate, making it so fixes the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM