Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 836 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Does a foreign key automatically create an index?

#11
It depends. On MySQL an index is created if you don't create it on your own:
>MySQL requires that foreign key columns be indexed; if you create a table with a foreign key constraint but no index on a given column, an index is created.

Source:

[To see links please register here]


The same for MySQL 5.6 eh.
Reply

#12
A foreign key is a constraint, a relationship between two tables - that has nothing to do with an index per se.

However, it makes a lot of sense to index all the columns that are part of any foreign key relationship. An FK-relationship will often need to look up a relating table and extract certain rows based on a single value or a range of values.

So it makes good sense to index any columns involved in an FK, but an FK per se is not an index.

Check out Kimberly Tripp's excellent article ["When did SQL Server stop putting indexes on Foreign Key columns?"][1].

**Updated 5/31/2023**
For those reading this now, I (the original poster) wanted to update Marc's excellent answer to point out that Entity Framework Core [now auto generates indexes][2] for foreign keys discovered by convention. Most helpful. Not sure how far back that feature exists, I'm using EF Core 7.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through