Foreign keys must be Index in mySQL?

后端 未结 3 1109
遥遥无期
遥遥无期 2021-02-14 11:47

I\'ve just created my first mySQL table on my own (other than using Joomla, Wordpress, etc.) and I am MS SQL developer for years but normally I can easily create a foreign key i

3条回答
  •  野的像风
    2021-02-14 12:13

    From the MySQL Reference Manual:

    InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. (This is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.) index_name, if given, is used as described previously.

    In contrast to SQL Server, which does not require FKs to be indexed... however the recommendations that I've seen suggest that you almost always being indexing your FKs even though it isn't required.

提交回复
热议问题