Home‎ > ‎Database‎ > ‎

Foreign Key

In a relational Database a field that refers to another tables Primary Key with the same field type is known as a Foreign Key. A Foreign Key is used to create a relationship between multiple tables. 

There are three kinds of relationships that could be formed depending on how many tables the Foreign Key is used in.  These three relationships are known as one-to-one, one-to-many, and many-to-many.  These relationship allow Referential integrity which ensures no important information can be lost and that the tables relate logically. 

For example you have two tables a Distributor table which holds the distributors name and and unique ID and an Order table which contains all the distributors orders. The goal of a Foreign Key is to ensure that all orders must be associated with a distributor that is already a distributor in you Distributor table but at the same time one distributor can have many individual orders. So this is done by placing the Primary Key for the Distributor table in your Order table and this will be a Foreign Key in the Order table and would logically connect which order was shipped to which distributor. 

A table can have more than one Foreign Key that typically will lead to more than one referencing table. However in the case of “self-referencing” Foreign Key it may be chosen and located in the same table.     




    
References: Wikipedia Page SQL Foreign Key
Comments