我使用的是mysql 5.5, 使用的engine是innodb, 在创建外键的时候,innodb帮着自动创建了索引. 
在创建外键但不指定其约束名字的情况下,外键名字和索引名字是不一样的;
在创建外键且指定约束名字的情况下,外键名字和索引名字是一样的。
我的问题是,创建外键的时候不指定约束名字,通过什么途径能够知道这个外键关联的索引是哪个?
目前我知道的方法是show create table tableName; 请问有没有其他的方法?非常感谢。
create database test; 
use test; 
create table t1(a int, b int, primary key(a,b)); 
create table t2(a int, b int, foreign key(a,b) references t1(a,b)); 
create table t3(a int, b int, constraint f_t3 foreign key(a, b) references t1(a,b)); 
show create table t2; 
show create table t3; mysql> show create table t2; 
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                    |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2    | CREATE TABLE `t2` (
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  KEY `a` (`a`,`b`),
  CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`a`, `b`) REFERENCES `t1` (`a`, `b`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)mysql> show create table t3; 
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                  |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t3    | CREATE TABLE `t3` (
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  KEY `f_t3` (`a`,`b`),
  CONSTRAINT `f_t3` FOREIGN KEY (`a`, `b`) REFERENCES `t1` (`a`, `b`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

解决方案 »

  1.   

    SELECT * FROM information_schema.key_column_usage 
    WHERE table_name='表名'
      

  2.   

    谢谢,但这个查到的是外键的信息呀mysql> SELECT * FROM information_schema.key_column_usage where table_name='t2' and table_schema='test';
    +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
    | CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | ORDINAL_POSITION | POSITION_IN_UNIQUE_CONSTRAINT | REFERENCED_TABLE_SCHEMA | REFERENCED_TABLE_NAME | REFERENCED_COLUMN_NAME |
    +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
    | def                | test              | t2_ibfk_1       | def           | test         | t2         | a           |                1 |                             1 | test                    | t1                    | a                      |
    | def                | test              | t2_ibfk_1       | def           | test         | t2         | b           |                2 |                             2 | test                    | t1                    | b                      |
    +--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+
    2 rows in set (0.01 sec)
      

  3.   

    再与`STATISTICS`表连接
    SELECT  * FROM information_schema.`STATISTICS` WHERE table_name='表名'
      

  4.   

    mysql> SELECT  CONSTRAINT_NAME FROM information_schema.KEY_COLUMN_USAGE  WHERE table_name='t2'; 
    +-----------------+
    | CONSTRAINT_NAME |
    +-----------------+
    | t2_ibfk_1       |
    | t2_ibfk_1       |
    +-----------------+
    2 rows in set (0.00 sec)
      

  5.   

    谢谢大家的帮助,我现在弄明白了。通过mysqldump我明白了克隆一个数据库表的外键如何操作。