Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign keys must be Index in mySQL?

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 in MS SQL but I came across a difficulty or lack of knowledge here.

Here is my tables :

users

  1. user_id int primary auto_increment
  2. username varchar(20)
  3. password varchar(20)

posts

  1. post_id in primary auto_increment
  2. title varchar(100)
  3. message text
  4. user_id int

When I try to add a foreign key to users which refers to posts->user_id, I cannot see the posts->user_id option in the option list Relation_view window on mySQL panel.

I am wondering whether I should define posts->user_id as Index or something? If so, why?

like image 944
Tarik Avatar asked Jun 03 '11 17:06

Tarik


People also ask

Is index needed for foreign key?

No, there is no implicit index on foreign key fields, otherwise why would Microsoft say "Creating an index on a foreign key is often useful". Your colleague may be confusing the foreign key field in the referring table with the primary key in the referred-to table - primary keys do create an implicit index.

Are foreign keys automatically indexed MySQL?

Yes, Innodb provide this. You can put a foreign key name after FOREIGN KEY clause or leave it to let MySQL to create a name for you. MySQL automatically creates an index with the foreign_key_name name.

Are foreign keys automatically indexed SQL Server?

SQL Server will not automatically create an index on a foreign key. Also from MSDN: A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table.

Are foreign keys automatically indexed MariaDB?

If MariaDB automatically creates an index for the foreign key (because it does not exist and is not explicitly created), its name will be index_name . The referenced columns in the parent table must be a an index or a prefix of an index.


3 Answers

Short answer: Yes, MySQL forces you to index foreign key.

InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.

You can read more about foreign keys on MySQL documentation pages: http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html

like image 116
Crozin Avatar answered Sep 29 '22 03:09

Crozin


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.

like image 29
Michael Fredrickson Avatar answered Sep 29 '22 01:09

Michael Fredrickson


From the documentation:

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.

like image 25
King Skippus Avatar answered Sep 29 '22 03:09

King Skippus