tags:

views:

66

answers:

1

I am using mySQL 5.1 and am wondering, is it necessary to add CREATE INDEX syntax on a column after you have defined it in a FK relationship. Common sense tells me that if a column is a key (foreign or otherwise), then it needs to be indexed - but you never know ...

Do I have to EXPLICITLY create an index on my FK - or does creating an FK implicitly creates an index on the column ?

+7  A: 

Quoting this page of the MySQL manual : 13.6.4.4. FOREIGN KEY Constraints :

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.

Pascal MARTIN
MySQL is unusual in this regard, although I wish other products would do the same.
Bob Jarvis
+1 One interesting effect is that on MyISAM (which doesn't support foreign keys), declaring a foreign key still creates an index.
Bill Karwin
It does not require a *unique* index?(!)
Alex Brasetvik
@Alex: Right, a foreign key does not have to be unique. If it did, you could never have a many-to-one relationship.
Bill Karwin