Bug #77538 MyISAM accepts table definition that is no longer considered as correct
Submitted: 29 Jun 2015 11:06 Modified: 29 Jun 2015 11:25
Reporter: Marcin Szumilak Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: MyISAM storage engine Severity:S2 (Serious)
Version:5.1/5.5/5.6/5.7 OS:Linux
Assigned to: CPU Architecture:Any

[29 Jun 2015 11:06] Marcin Szumilak
Description:
MySQL accepts CREATE TABLE with PRIMARY KEY definition for MyISAM tables that is not considered as a valid PRIMARY KEY anymore.

Issue applies to MyISAM table with auto_increment column specified as a second column for primary key.

CREATE TABLE test (
col1 VARCHAr(20), col2 INT(5) AUTO_INCREMENT,
PRIMARY KEY (col1,col2)
) ENGINE=MyISAM;

mysql> ALTER TABLE test ENGINE=InnoDB;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

DROPping primary key is also not possible.
mysql> ALTER TABLE test DROP PRIMARY KEY;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

The only way to fix issue is to drop primary key and redefine it at the same step:
mysql> ALTER TABLE test DROP PRIMARY KEY, ADD PRIMARY KEY (col2, col1);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

Creating table with that type of incorrect PK definition is not possible for InnoDB and HEAP storage engines.

How to repeat:
mysql> CREATE TABLE test (
col1 VARCHAr(20), col2 INT(5) AUTO_INCREMENT,
PRIMARY KEY (col1,col2)
) ENGINE=MyISAM;
Query OK, 0 rows affected (0.02 sec)

mysql> ALTER TABLE test ENGINE=InnoDB;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

mysql> ALTER TABLE test DROP PRIMARY KEY, ADD PRIMARY KEY (col2,col1);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE test ENGINE=InnoDB;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> 

Suggested fix:
better checking table definition as it is done for InnoDB and HEAP storage engines.
[29 Jun 2015 11:25] MySQL Verification Team
Thank you for the bug report. Verified as describe.