我正在使用 mySQL 5.1,我想知道,在 FK 关系中定义列后,是否有必要在列上添加 CREATE INDEX 语法。常识告诉我,如果一个列是一个键(外部或其他),那么它需要被索引——但你永远不知道......
我是否必须在我的 FK 上显式创建索引 - 或者创建 FK 是否隐式地在列上创建索引?
最佳答案
引用 MySQL 手册的这一页:13.6.4.4. FOREIGN KEY Constraints :
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.
关于sql - 创建 FK 是否会自动索引该列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936222/