正如 Bobby 所建议的,SHOW 索引返回一个带有相关键索引的结果集,查看此页面以获取更多示例:
SHOW INDEXES FROM products_to_categories
WHERE Key_name = "PRIMARY"
AND Seq_in_index = '2';
+------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| products_to_categories | 0 | PRIMARY | 2 | category_id | A | 0 | NULL | NULL | | BTREE | |
+------------------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
但是,如果不超过以下解决方法,这可能需要尽可能多的代码。
检查您插入的哪个值与表中的现有行冲突:
SELECT code FROM myTable WHERE code = 'the_value_you_tried_to_insert';
或者
SELECT name FROM myTable WHERE name = 'the_value_you_tried_to_insert';
尽管不是一个非常优雅的解决方案。