我读到InnoDB会自动为外键创建索引。
但是我的一些外键在表中没有索引。请检查“pharmaceutical_id”外键字段。它没有索引。 MySQL Workbench是否会自动为外键创建索引? MySQL是否会自动为外键列创建索引? https://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
我已在
请查看https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html中第6条限制和条件。
但是我的一些外键在表中没有索引。请检查“pharmaceutical_id”外键字段。它没有索引。 MySQL Workbench是否会自动为外键创建索引? MySQL是否会自动为外键列创建索引? https://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
| pharmaceuticals_pharmaceuticalcode | CREATE TABLE `pharmaceuticals_pharmaceuticalcode` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`code_id` int(11) NOT NULL,
`pharmaceutical_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `pharmaceuticals_pharmaceuticalco_pharmaceutical_id_5ae1e77e_uniq` (`pharmaceutical_id`,`code_id`),
KEY `pharmaceuticals_phar_code_id_a7de9505_fk_human_api` (`code_id`),
CONSTRAINT `pharmaceuticals_phar_code_id_a7de9505_fk_human_api` FOREIGN KEY (`code_id`) REFERENCES `human_api_code` (`id`),
CONSTRAINT `pharmaceuticals_phar_pharmaceutical_id_04c18462_fk_pharmaceu` FOREIGN KEY (`pharmaceutical_id`) REFERENCES `pharmaceuticals_pharmaceutical` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=202770 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
我已在
pharmaceutical_id
和code_id
上添加了唯一约束,这可能导致没有为pharmaceutical_id
创建单独的索引,因为MySQL以B-Tree
方式管理这些索引,而唯一约束键的索引可以用于它。请查看https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html中第6条限制和条件。
但是,如果上述观点是正确的,那么下面表格模式中为什么会有一个InnoDB允许外键引用任何索引列或一组列。但是,在所引用的表中,必须存在一个索引,其中引用的列按相同顺序排列且为第一列。InnoDB添加到索引中的隐藏列也被视为索引列(请参见14.6.2.1节,“聚集和二级索引”)。
member_id
索引呢?因为在 patients_membercard_member_id_661ac31abca894ae_uniq
独特索引出现后,patients_membercard_b5c3e75b
索引就变得多余了。| patients_membercard | CREATE TABLE `patients_membercard` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `patients_membercard_member_id_661ac31abca894ae_uniq` (`member_id`,`name`),
KEY `patients_membercard_b5c3e75b` (`member_id`),
CONSTRAINT `patients_member_member_id_459e0d6970a32170_fk_patients_member_id` FOREIGN KEY (`member_id`) REFERENCES `patients_member` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1655520 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
PS:上面的表格,模式是由 Django
创建的,并且在数据库上没有进行手动操作。