To understand how primary keys and foreign keys define relationships between tables in relational databases, it helps to consider referential integrity constraints.
Referential integrity constraints, also known as referential constraints or foreign key constraints, are rules requiring that foreign key values in one table match the primary key values in another table.
For example, in an employee database, a referential integrity constraint may specify that each employee in the EMPLOYEE table must be a member of an existing company department listed in the DEPARTMENT table.
In this case, the DEPARTMENT table’s primary key is a column storing unique department numbers, while the EMPLOYEE table’s foreign key is a column storing the same unique department numbers. These matching columns mean the tables adhere to the foreign key constraint.
Since the DEPARTMENT table is home to the primary key referenced by the EMPLOYEE table’s foreign key, the EMPLOYEE key is considered as having a dependency on the DEPARTMENT table. In turn, the DEPARTMENT table is considered the “parent table” in this table relationship.
Referential integrity constraints can block erroneous data insertions into databases. Such constraints, for instance, would prevent a user from adding an employee record to the EMPLOYEE table (as pictured below) if the department number in that employee record did not exist in DEPARTMENT TABLE.