Does MySQL index foreign key columns automatically?

MysqlDatabaseIndexing

Mysql Problem Overview


Does MySQL index foreign key columns automatically?

Mysql Solutions


Solution 1 - Mysql

Yes, but only on [tag:Innodb]. Innodb is currently the only shipped table format that has foreign keys implemented.

Solution 2 - Mysql

Apparently an index is created automatically as specified in the link robert has posted.

> InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. (This is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.) index_name, if given, is used as described previously.

InnoDB and FOREIGN KEY Constraints

Solution 3 - Mysql

Solution 4 - Mysql

You don't get the index automatically if you do an ALTER TABLE (instead of CREATE TABLE), at least according to the docs (the link is for 5.1 but it's the same for 5.5):

> [...] When you add a foreign key constraint to a table using ALTER TABLE, remember to create the required indexes first.

Solution 5 - Mysql

For those who are looking for quote from 5.7 docs:

> MySQL requires indexes on foreign keys and referenced keys so that > foreign key checks can be fast and not require a table scan. In the > referencing table, there must be an index where the foreign key > columns are listed as the first columns in the same order. Such an > index is created on the referencing table automatically if it does not > exist. This index might be silently dropped later, if you create > another index that can be used to enforce the foreign key constraint. > index_name, if given, is used as described previously.

Solution 6 - Mysql

As stated it does for InnoDB. At first I thought it was strange that many other (in particular MS SQL and DB2) doesn't. TableSpace scans are only better than index scans when there are very few table rows - so for the vast majority of cases a foreign key would want to be indexed. Then it kind of hit me - this doesn't necessarily mean it has to be a stand alone (one column) index - where it is in MySQL's automatic FK Index. So, may be that is the reason MS SQL, DB2 (Oracle I'm not sure on) etc leave it up to the DBA; after all multiple indexes on large tables can cause issues with performance and space.

Solution 7 - Mysql

Yes, Innodb provide this. You can put a foreign key name after FOREIGN KEY clause or leave it to let MySQL to create a name for you. MySQL automatically creates an index with the foreign_key_name name.

CONSTRAINT constraint_name
FOREIGN KEY foreign_key_name (columns)
REFERENCES parent_table(columns)
ON DELETE action
ON UPDATE action

Solution 8 - Mysql

It's not possible to get index key automatically use

ALTER TABLE (NAME OF THE TABLE) ADD INDEX (FOREIGN KEY)

Name of the table which you have created for example photographs and FOREIGN KEY for example photograph_id. The code should be like this

ALTER TABLE photographs ADD INDEX (photograph_id);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDónalView Question on Stackoverflow
Solution 1 - MysqlGrant LimbergView Answer on Stackoverflow
Solution 2 - MysqlMr AlexanderView Answer on Stackoverflow
Solution 3 - MysqlRobert GambleView Answer on Stackoverflow
Solution 4 - MysqlThomas LundströmView Answer on Stackoverflow
Solution 5 - MysqlFahmiView Answer on Stackoverflow
Solution 6 - MysqlWolf5370View Answer on Stackoverflow
Solution 7 - MysqlNavrattan YadavView Answer on Stackoverflow
Solution 8 - MysqlAli RazaView Answer on Stackoverflow