Does MySQL index foreign key columns automatically?
MysqlDatabaseIndexingMysql 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.
Solution 3 - Mysql
Yes, see InnoDB and FOREIGN KEY Constraints.
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);