MySQL - there can be only one auto column and it must be defined as a key

chris (2006-09-17 15:49:14)
36727 views
1 replies

This error usually occurs when you create a new mysql table, but forget to specify that a column should be the primary key. Here's an example:

Here you can see the user trying to create a table called 'history':

mysql> create table history(hi_id int auto_increment, hi_message text, hi_datetime datetime, hi_user text);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

The user has specified the hi_id table to be auto_increment'ed, assuming that mysql will automatically understand that column to be the primary key. This is wrong. You must specify keys at the table creation stage. Here is the corrected statement:

mysql> create table history(hi_id int auto_increment primary key, hi_message text, hi_datetime datetime, hi_user text);
Query OK, 0 rows affected (0.00 sec)

And it works..

christo
Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks
comment
mark morss
2012-05-11 15:40:49

this is wrong

This error usually occurs when you create a new mysql table, but forget to specify that a column should be the primary key. Here's an example:

Here you can see the user trying to create a table called 'history':

mysql> create table history(hi_id int auto_increment, hi_message text, hi_datetime datetime, hi_user text);
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

The user has specified the hi_id table to be auto_increment'ed, assuming that mysql will automatically understand that column to be the primary key. This is wrong. You must specify keys at the table creation stage. Here is the corrected statement:

mysql> create table history(hi_id int auto_increment primary key, hi_message text, hi_datetime datetime, hi_user text);
Query OK, 0 rows affected (0.00 sec)

And it works..

christo


This is wrong. Routinely it is necessary to define and auto incrementing column and only later say that it is part of a multi-variable primary key. Further it is always possible in MyISAM tables to define an auto-incrementing variable that is not a primary key. This error arises because someone has InnoDB as his default storage engine and assumes instead that the default is MyISAM.
reply icon