Data Modeling - Foreign Key Constraint
1 - About
A Foreign Key is:
- a column
- or combination of columns
used to establish and enforce a relationship (link) between data in two tables.
The link is created between the two tables by adding the column or columns that hold one table's primary key values to the other table. This column becomes a Foreign Key in the second table.
A foreign key may be enforced, e.g. by a foreign key constraint.
Referential Integrity is a data property whereby a Foreign Key in one table has a corresponding Primary key in a different table.
2 - Articles Related
3 - Example
An Foreign Key example of a database (with the scott schema from Oracle).
Consider a part of the EMP (Employe) table.
The column DPTNO from the EMP table refer to the column DEPTNO from the table DEPT (Department) below :
For each DPTNO in the table EMP, you must have a DPTNO in the table DEPT. We say then that :
- the column DPTNO from the table EMP is a foreign key.
- and then that the column DEPTNO is a primary key.
If it's not true, there is NO referential integrity.