我想在data
和之间添加第二个外键约束containers
。当我删除一个容器时,链接到这个容器的数据也必须被删除。
表格:
mysql> DESCRIBE data;
+------------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------------------+----------------
| imei | varchar(15) | NO | MUL | NULL |
mysql> DESCRIBE containers;
+--------------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------------------+----------------+
| imei | varchar(15) | NO | MUL | NULL | |
我用这个语句创建了一个外键:
mysql> ALTER TABLE `data` ADD FOREIGN KEY (`imei`) REFERENCES `containers`(`imei`) ON DELETE CASCADE;
Query OK, 15168 rows affected (0.12 sec)
Records: 15168 Duplicates: 0 Warnings: 0
但尚未创建外键:
mysql> use INFORMATION_SCHEMA;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = 'containers';
+----------------+-------------+-----------------------+-----------------------+------------------------+
| TABLE_NAME | COLUMN_NAME | CONSTRAINT_NAME | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
+----------------+-------------+-----------------------+-----------------------+------------------------+
| container_logs | imei | container_logs_ibfk_1 | containers | imei |
+----------------+-------------+-----------------------+-----------------------+------------------------+
1 row in set (0.16 sec)
我究竟做错了什么?