views:

39

answers:

3
Create Table: CREATE TABLE `category` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `parent` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`),
  KEY `parent_idx` (`parent`),
  CONSTRAINT `category_parent_category_id` FOREIGN KEY (`parent`) REFERENCES `category` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8

I'm not sure whether a foreign key will imply an index?

EDIT

I don't see the supposed index :

mysql> show index from category;
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table    | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| category |          0 | PRIMARY    |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| category |          0 | name       |            1 | name        | A         |           0 |     NULL | NULL   |      | BTREE      |         |
| category |          1 | parent_idx |            1 | parent      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
3 rows in set (0.02 sec)
+2  A: 

In SQL Server: Foreign keys do not automatically create indexes. You will need to explicitly create an index where you want one. This is done because you don't necessarily want to index every FK, as it will add overhead to inserts.

Keith Rousseau
No, InnoDB does create an index automatically: "InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan... Such an index is created on the referencing table automatically if it does not exist." Source: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html. It is SQL Server that does not create indexes automatically on foreign keys.
Daniel Vassallo
I guess I just assumed that they would be the same in that regard. FWIW, I think SQL Server's behavior is better as it gives more control.
Keith Rousseau
+1  A: 

Yes, you are correct.

KEYparent_idx(parent) is redundant index.

MySQL will automactially created an index for FOREIGN KEY contraint.

From the MySQL manual:

InnoDB creates an index for the foreign key, it uses index_name for the index name.

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

Yada
But I don't see the index with `show index from table`
A: 

I'm not sure whether a foreign key will imply an index?

"InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan... Such an index is created on the referencing table automatically if it does not exist." (Source)

Therefore KEY parent_idx (parent) is redundant.

On the other hand note that if you were to add a foreign key constraint using the ALTER TABLE syntax, you would have to explicitly create the required indexes first.

Daniel Vassallo
But there is no such index by `show index from table`
You are not seeing another index because InnoDB will not create a new index for the foreign key if you had expliclty created one already. If you don't create `parent_idx`, InnoDB will create it for you (with a different name).
Daniel Vassallo