I’m trying to create a foreign key with its index at the same time, and specify the name of both of them explicitly.
The manual says:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
index_name represents a foreign key ID. If given, this is ignored if
an index for the foreign key is defined explicitly. Otherwise, if
InnoDB creates an index for the foreign key, it uses index_name for
the index name.
So I tried the following:
ALTER TABLE MyTable
ADD CONSTRAINT FK_MyTable_Zone FOREIGN KEY
IDX_MyTable_Zone (zoneId)
REFERENCES Zone (id);
But SHOW CREATE TABLE shows that the index name IDX_MyTable_Zone has not been taken into account, and the foreign key name has been used for the index name as well:
CREATE TABLE `MyTable` (
`zoneId` int(10) unsigned NOT NULL,
KEY `FK_MyTable_Zone` (`zoneId`),
CONSTRAINT `FK_MyTable_Zone` FOREIGN KEY (`zoneId`) REFERENCES `Zone` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Did I miss something?
I’ve never used that syntax. I always create the index separately from the foreign key in order to control the name.
This should work for you: