Skip to main content

What is the difference between a primary key and a foreign key?

FAQ resource for your migration project.

FAQ resource for What is the difference between a primary key and a foreign key?.

Answer

A primary key uniquely identifies each row in a table and cannot be null or duplicated; it is the column (or set of columns) the database uses to address a specific record. A foreign key is a column in one table that references the primary key of another table, establishing a relationship and enforcing referential integrity so you cannot reference a row that does not exist. For example, an orders table might have a customer_id foreign key pointing to the customers table's primary key. Together they model relationships and keep the data consistent across tables.