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
我不确定外键是否意味着索引?此处是否需要“父”的索引?
编辑
我没有看到所谓指数:
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)
@ user198729:您没有看到另一个索引,因为如果您已经明确创建了外键,InnoDB不会为外键创建新索引。如果你不创建'parent_idx',InnoDB会为你创建一个(使用不同的名称)。 – 2010-02-23 13:46:31