账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于创建外键,创建索引的问题
    43
    0

    如下所示的三张表的创建过程, test1test2 分别有一个字段,然后作为各自表的主键,

    test3 中有两个字段,分别作为外键引用 test1test2 中的场景,

    mysql> create table test1(field1 int, primary key(field1));
    Query OK, 0 rows affected (0.04 sec)
    
    mysql> create table test2(field2 int, primary key(field2));
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> create table test3(field1 int ,field2 int,primary key(field1,field2),foreign key(field1) references test1(field1), foreign key(field2) references test2(field2));
    Query OK, 0 rows affected (0.06 sec)
    
    mysql> show index from test3;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | test3 |          0 | PRIMARY  |            1 | field1      | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    | test3 |          0 | PRIMARY  |            2 | field2      | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    | test3 |          1 | field2   |            1 | field2      | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    3 rows in set (0.00 sec)
    
    mysql>

    这里在最后使用 show index from test3 时,会出现一个 Key_namefield2 的索引,,这是什么原因??

    我使用 show create table test3 ,查看创建表的语句,MySQL 隐示的给我创建了该索引,感觉很奇怪

    CREATE TABLE `test3` (
      `field1` int(11) NOT NULL DEFAULT '0',
      `field2` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`field1`,`field2`),
      KEY `field2` (`field2`),
      CONSTRAINT `test3_ibfk_1` FOREIGN KEY (`field1`) REFERENCES `test1` (`field1`),
      CONSTRAINT `test3_ibfk_2` FOREIGN KEY (`field2`) REFERENCES `test2` (`field2`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    我尝试过很多创建表的操作,修改 primary key(field1, field2)primary key(field2,field1) ,即简单的调换顺序后,这里的索引由 field2 变为 field1

    例如,我不定义主键,定义两个外键,会有两个索引被创建:

    mysql> create table test3(field1 int ,field2 int,foreign key(field1) references test1(field1), foreign key(field2) references test2(field2));
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> show index from test3;
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | test3 |          1 | field1   |            1 | field1      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    | test3 |          1 | field2   |            1 | field2      | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    2 rows in set (0.00 sec)

    然后,我使用其中的一列作为主键,并且两列都为一个外键,会有一个索引,

    mysql> create table test3(field1 int ,field2 int,primary key(field1),foreign key(field1) references test1(field1), foreign key(field2) references test2(field2));
    Query OK, 0 rows affected (0.31 sec)
    
    # 查看创建语句如下
    CREATE TABLE `test3` (
      `field1` int(11) NOT NULL DEFAULT '0',
      `field2` int(11) DEFAULT NULL,
      PRIMARY KEY (`field1`),
      KEY `field2` (`field2`),
      CONSTRAINT `test3_ibfk_1` FOREIGN KEY (`field1`) REFERENCES `test1` (`field1`),
      CONSTRAINT `test3_ibfk_2` FOREIGN KEY (`field2`) REFERENCES `test2` (`field2`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    而在使用两列一起作为主键时,为什么还是会有一个field2 索引呢,

    在我的理解中,应该是两个才对,就算,因为主键的原因,那也应该是有既没有 field1 ,也没有 field2 ,只有一个 PRIMARY 的索引才对。???

    原始的场景不是这个样子,不过跟上述的示例能够表达我的疑惑了,

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 両両相爱 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部