mysql - 我没有看到我创建的外键索引

标签 mysql magento indexing

我创建了一个带有唯一索引和外部索引的表。当我添加唯一键时,我从 MySQL 得到了成功响应。然后,当我添加外键时,我也得到了成功响应。

以下是添加外键的 SQL:

ALTER TABLE `rewards_customer_index_points` 
ADD CONSTRAINT FOREIGN KEY FK_CUSTOMER_INDEX_POINTS_CUSTOMER_ID(  `customer_id` ) 
REFERENCES  `customer_entity` (  `entity_id` ) 
ON DELETE CASCADE 
ON UPDATE CASCADE

但是,我没有在表上看到该外键,因此看起来它没有创建成功。但它肯定在那里,因为当我删除引用的 customer_entity 记录时,级联会起作用。

为什么它没有显示在我的索引列表中?两个表都是 InnoDB。

这是表结构和表上的键:

mysql> explain rewards_customer_index_points;
+-----------------+------------------+------+-----+-------------------+-----------------------------+
| Field           | Type              | Null | Key | Default           | Extra                           |
+-----------------+------------------+------+-----+-------------------+-----------------------------+
| index_points_id | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| customer_id     | int(10) unsigned | NO   | MUL | NULL              |                             |
| status          | int(11)          | NO   |     | 0                 |                             |
| points_positive | int(11)          | NO   |     | NULL              |                             |  
| points_negative | int(11)          | NO   |     | NULL              |                             |
| updated_at      | timestamp        | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------------+------------------+------+-----+-------------------+-----------------------------+


show index from rewards_customer_index_points;
+-------------------------------+------------+------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                         | Non_unique | Key_name               | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment     | Index_comment |
+-------------------------------+------------+------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| rewards_customer_index_points |          0 | PRIMARY                |            1 | index_points_id | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| rewards_customer_index_points |          0 | idx_customer_id_status |            1 | customer_id     | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| rewards_customer_index_points |          0 | idx_customer_id_status |            2 | status          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
+-------------------------------+------------+------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

最佳答案

首先,外键和唯一约束不是索引,它们是约束。但为了有效地实现此类约束,大多数 DBMS 使用索引。

在 MySQL 中,UNIQUE KEYUNIQUE INDEX 在表定义中是同一件事,它们的意思是:添加索引和唯一约束。

对于外键,情况有点不同。在 InnoDB 引擎中,当您​​添加 FOREIGN KEY 约束时,会在引用列上创建索引,但前提是索引尚不存在。

您有 idx_customer_id_status 索引,它是一个 (customer_id, status) 复合索引,该索引可用于外键约束。因此,没有创建额外的索引。

关于mysql - 我没有看到我创建的外键索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9707386/

相关文章:

Mysql:显示每个订单的每个客户的订单号

forms - Magento - 处理 block 形式的最佳方法?

python - Django/mysql中间表不插入数据

html - 将圆形 Logo 添加到 magento 2 标题

php - Magento 模型保存方法更新记录而不是插入

python - 如何从分区 DF(非唯一索引)中选择带有索引列表的数据?

java - 如何解析管道分隔的 Attribute=Values 对?

python - Pandas:删除带有某些日期的字符串

php - 如何在准备好的语句php中的不同行中插入多条记录

mysql - SQL Sum 每个团队的前 5 行