MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
1PLs Company - #1Payday.Loans Agency - Loans online and near me $100-$2500 (Same Day)
Powered by MySQL
 
Home / Indexes / Question No: 155

What is foreign key in MySQL?

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

Answer No: 155

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.

Related MySQL FAQs to the Above FAQ

How-to-get-existing-indexes-of-the-table-in-MySQL How to get existing indexes of the table in MySQL?

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

How-many-types-of-indexes-or-keys-are-in-MySQL How many types of indexes or keys are in MySQL?

What-is-single-column-index-or-key-in-MySQL What is single column index or key in MySQL?

What-is-multi-column-index-or-key-in-MySQL What is multi column index or key in MySQL?

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

When-does-multi-column-index-come-into-use-in-MySQL When does multi column index come into use in MySQL?

What-are-best-practices-to-pick-columns-to-index What are best practices to pick columns to index?

What-are-advantages-and-disadvantages-of-indexes-in-MySQL What are advantages and disadvantages of indexes in MySQL?

How-to-remove-or-drop-indexes-in-MySQL How to remove or drop indexes in MySQL?

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

About FAQs: Recently Added FAQs

About MySQL FAQs: Site Map | Bookmark Us | Recommend this Site to Your Friend | Contact Us

Useful Links: Wikipedia.org | Oracle.com | w3schools.com | www.php.net | Github.com

© 2023  www.mysqlfaqs.net
All rights reserved.