SQL Interview Question

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

Updated 2026-07-10 · Beginner friendly
Quick answer

A primary key uniquely identifies each row in a table, cannot be null, and there is only one per table. A foreign key is a column in one table that points to the primary key of another table, which creates a link between the two. Primary keys enforce uniqueness, while foreign keys enforce relationships and referential integrity.

Primary key

It is the unique identity of a row, like a roll number for a student. No two rows can share it and it cannot be empty.

Foreign key

It connects tables. An orders table might store a customer id that refers to the primary key of the customers table, so each order knows which customer placed it.

CREATE TABLE customers (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

CREATE TABLE orders (
  id INT PRIMARY KEY,
  customer_id INT,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);
In the interview

A crisp answer is a primary key identifies a row uniquely, a foreign key links to another table's primary key. Adding that a foreign key protects referential integrity, so you cannot reference a customer that does not exist, shows real database sense.

Want the full SQL guide?

Read every SQL concept with notes, diagrams, and code in one place. Track your progress as you go.

Open the SQL guide All SQL questions