sql - 创建 FK 是否会自动索引该列?

标签 sql mysql

我正在使用 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/

相关文章:

sql - 使用 MSSQL GetDate() 时如何获取日期?

mysql 三个最后一个

php - 如何使用 Angularjs 操作从 SQL 返回的数据

mysql - 如果表存在,则从表中删除记录

python - Bottle 和 MySQLdb 中的 Int、Decimal 或 Datetime 类型执行不工作

mysql - 将多个 NOT IN 转换为 LEFT JOIN 以提高性能

sql - 选择计数、内部连接和 where 语句

php - 使用文件路径将 ECHO 图像保存在数据库中

mysql - 尝试从另一个表更新 MySQL 表中的数千行

mysql - 错误代码 : 1289. 'SHOW PROCEDURE|FUNCTION CODE' 功能已禁用;让它发挥作用