如下所示的三張表的創(chuàng)建過程, test1
和 test2
分別有一個(gè)字段,然后作為各自表的主鍵,
test3
中有兩個(gè)字段,分別作為外鍵引用 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
時(shí),會(huì)出現(xiàn)一個(gè) Key_name
為 field2
的索引,,這是什么原因??我使用 show create table test3
,查看創(chuàng)建表的語句,MySQL 隱示的給我創(chuàng)建了該索引,感覺很奇怪
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
我嘗試過很多創(chuàng)建表的操作,修改 primary key(field1, field2)
為 primary key(field2,field1)
,即簡單的調(diào)換順序后,這里的索引由 field2
變?yōu)?field1
。
例如,我不定義主鍵,定義兩個(gè)外鍵,會(huì)有兩個(gè)索引被創(chuàng)建:
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)
然后,我使用其中的一列作為主鍵,并且兩列都為一個(gè)外鍵,會(huì)有一個(gè)索引,
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)
# 查看創(chuàng)建語句如下
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
索引呢,在我的理解中,應(yīng)該是兩個(gè)才對(duì),就算,因?yàn)橹麈I的原因,那也應(yīng)該是有既沒有 field1
,也沒有 field2
,只有一個(gè) PRIMARY
的索引才對(duì)。???
原始的場景不是這個(gè)樣子,不過跟上述的示例能夠表達(dá)我的疑惑了,
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。