【发布时间】:2011-01-20 01:48:00
【问题描述】:
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 将为您创建一个(使用不同的名称)。
标签: mysql foreign-keys indexing