SQL Foreign Key
SQL Foreign Key
A foreign key is a key used to link two tables together. This is sometimes called a referencing key.
Foreign Key is a column or a combination of columns whose values match a Primary Key in a
different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a
Foreign Key in the second table.
If a table has a primary key defined on any fields, then you can not have two records having the
same value of that fields.
Example:
Consider the structure of the two tables as follows:
CUSTOMERS table:
ORDERS table:
If ORDERS table has already been created, and the foreign key has not yet been set, use the
syntax for specifying a foreign key by altering a table.