I do not see a foreign key index I created

advertisements

I've created a table with a unique index as well as a foreign index. When I add the unique key I get a success response from MySQL. Then, when I add the foreign key I also get a success response.

Here's the SQL for adding the foreign key:

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

However, I'm not seeing that foreign key on the table, so it seems like it's not being created successfully. But it's definitely there, b/c the cascade works when I delete the referenced customer_entity record.

Why isn't it showing in my index list? Both tables are InnoDB.

Here is the table structure and keys on the table:

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      |         |               |
+-------------------------------+------------+------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+


In MySQL land a foreign key is not an index, and won't show up in the list of indexes. MySQL's original vision didn't include foreign key table relationships, it only included per-table indexes to improve select performance improvements.

InnoDB was originally a third party "extension" (not sure of the right term in MySQL speak) that added less speedy, but more compliant tables that included foreign key relationships. Because they were never part of the original vision, they were never incorporated into the show index or describe table commands.

Checkout this older question for some ways to get a list of foreign keys on a table. Your choices are a slow query to the information_schema, or parsing the results of a SHOW CREATE TABLE command.