Stay informed about any major changes. Click here to subscribe
About relationships in a database
A relationship works by matching data in key fields - usually a field
with the same name in both tables. In most cases, these matching fields
are the primary key from one table,
which provides a unique identifier for each record, and a foreign key
in the other table. A foreign key (FK) is a column or combination of columns
used to establish and enforce a link between the data in two tables. A
link is created between 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.
Although the primary purpose of a foreign key is to control the data
that can be stored in the foreign key table, it also controls changes
to data in the primary key table. The constraint enforces referential
integrity by ensuring that changes cannot be made to data in the primary
key table if those changes invalidate the link to data in the foreign
key table. If an attempt is made to delete the row in a primary key table
or to change a primary key value, the action will fail if the deleted
or changed primary key value corresponds to a value in the foreign key
of another table.
To change or delete a row in a foreign key constraint successfully,
you must first either delete the foreign key data in the foreign key table
or change the foreign key data in the foreign key table, thereby linking
the foreign key to different primary key data.