Domanda

Can anyone tell me if MySQL does indexing for its foreign keys automatically or not?

My MySQL is using MyIsam Engine.

È stato utile?

Soluzione

MyISAM does not support foreign keys at all. From the manual:

For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it. ... At a later stage, foreign key constraints will be implemented for MyISAM tables as well.

This is for MySQL 5.6, the next version, so it is not implemented yet. The text is exactly the same for older versions.

This means that the foreign key construct is not used at all. You can specify it in your CREATE TABLE command but MySQL will silently ignore it. No index will be made out of it, and it won't be stored (so a SHOW CREATE TABLE command will not show that you tried to createa a foreign key).

If you need foreign key support, consider using the InnoDB storage engine instead. InnoDB creates indices automatically for foreign keys.

Altri suggerimenti

MyISAM engine doesn't support foreign keys, only the InnoDB engine:

http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

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 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top