MySQL Database learning



Home / Indexes / What is foreign key in MySQL?
Share this job:


What is foreign key in MySQL?

I need to clear my concept about foreign key. Can you explain, What is foreign key in MySQL?

FOREIGN KEY is used to establish the relationship among tables. FOREIGN KEY type can be Primary Key, Unique Key, or Normal Key of its table. Suppose, you want to manage data of authors and their books in the database. For this, you have a table named "author". The author table has columns like "author_id", "author_name" to store authors' info. To store books, you have another table named "books". The books table has columns like "book_id", "book_name", and "author_id".

Because you have to keep track of books of each author in the book table, you created same type column ("author_id") in book table as in the author table so that relation can be established between these two tables using "author_id" columns. In the narrated example, author_id column of author table can be type of Primary Key and author_id column in books table can be type of Normal Key. Terminologically author table will be called ' master table' and books table will be called ' foreign table'. The author_id column in author table is a primary key and author_id column in books is a foreign key.

Foreign Key can be described as follow: A foreign key is a key found in a given table that references a primary key found in another table. Such keys are used to identify a relationship among rows in different tables.





Indexes related other questions

What is index terminology or concept and how does index work in MySQL?

An INDEX is essentially an ordered (or indexed) subset of table columns, with each row entry pointing to its corresponding table row.We index the specific columns to keep them in organized versions. A ...

What is foreign key in MySQL?

FOREIGN KEY is used to establish the relationship among tables. FOREIGN KEY type can be Primary Key, ...

How to remove or drop indexes in MySQL?

MySQL allows to drop existing INDEXES and PRIMARY KEYs.  For this MySQL provides ALTER TABLE and DROP ...

What are advantages and disadvantages of indexes in MySQL?

Indexing is one of the more useful features of MySQL. MySQL allows several types of indexes like primary key index ...

What are best practices to pick columns to index?

Indexes can play an important role in query optimization and searching the results speedily from tables. So it is most important step to select which columns to be indexed. There are two major places where we ...

Is it possible to apply more than one keys on a single column?

Yes, it's possible that more than one keys can be applied to a given column. For example, a column that is a PRIMARY KEY or  any other type of KEY might also be part of other indexes. ...

How many types of indexes or keys are in MySQL?

MySQL allows four general types of indexes (keys). These indexes can be created either on single column or multi-columns however both ...

What is multi column index or key in MySQL?

Indexes is a useful feature to optimize searches.  We can make our SELECT queries faster using MySQL indexes. For this purpose, we create an index on single column or on combination of columns (multi-col ...

What is single column index or key in MySQL?

MySQL allows to create single column index and multi-column index to facilitate quick retrieval ...

What is partial-column and prefixed-column index or key in MySQL?

An  index is  used if a particular column in your  table will be the focus of a considerable number of your SELECT  queries. For example, suppose an buyer profile table consists of four co ...

When does multi column index come into use in MySQL?

Multi column index is a useful feature to optimize searches. MySQL allows to create an index either on ...

How to get existing indexes of the table in MySQL?

To view the keys or indexes on a table, MySQL provides the SHOW KEYS and SHOW INDEXES commands. You can use any of these commands. The result of both commands is the same. Take the following examples: ...