Reputation: 862
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.
Upvotes: 4
Views: 13098
Reputation: 385405
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.
Upvotes: 1
Reputation: 16569
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 !!
Upvotes: 1
Reputation: 10371
@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)
);
Upvotes: 1
Reputation: 332771
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
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.
Upvotes: 7
Reputation: 5509
+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;
Upvotes: 1
Reputation: 146603
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 ....
Upvotes: 2