【发布时间】:2012-03-14 17:55:29
【问题描述】:
我创建了一个具有唯一索引和外部索引的表。当我添加唯一键时,我得到了 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 | | |
+-------------------------------+------------+------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
【问题讨论】:
-
很可能,您的表不是 InnoDB 表。见:stackoverflow.com/questions/8960418/…
-
感谢 Roland,抱歉,我本来打算在我的初始帖子中包含此内容但忘记了,所以我现在正在编辑它。实际上,这两个表都是 InnoDB。
-
请执行 SHOW CREATE TABLLE 以确定是否创建了外键。