Mysql – Why can’t I create a index using column definition in MySQL

indexMySQLmysql-5.7syntax

Consider the following create table syntax

CREATE TABLE test1 (f1 int primary key, f2 int, unique key(f2));

I can also write the above as follows

CREATE TABLE test1 (f1 int primary key, f2 int unique key);

Why can't I do the same for indexes?

CREATE TABLE test1 (f1 int primary key, f2 int, index(f2)); -- is valid
CREATE TABLE test1 (f1 int primary key, f2 int index); -- is invalid

I know that the later one is wrong as per syntax but can you detail the reasons on why MySQL chose to not allow index in column definition? While it's perfectly fine for unique keys what would be the problem for indexes?

Best Answer

Why can't I do the same for indexes?

They just haven't got around to implementing and testing that syntax extension yet, would be my guess. Support for such definitions may be added in a later version.

IIRC inline index definitions are not part of any of the SQL standards but are something that some DB engines have added for convenience, so if you wish to retain as much compatibility with a range of database back-ends this may be a consideration that stops you using inline index definitions. Other DBs do implement that syntax of course (SQL Server added both forms, the one mySQL supports and the one you are looking for, in the 2014 release with a few enhancements in 2016) but perhaps not in a directly compatible way.