jpa 每次启动都要创建外键索引_关于创建外键,创建索引的问题

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

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

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_name 为 field2 的索引,,这是什么原因??

我使用 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 的索引才对。???

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值