Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 135

Primary Key vs Foreign Key

In this lesson: Difference between primary key and foreign key with comparison table, MySQL examples and output. Read in 3 min. In Hindi & English.

Two keys that work together

The primary key and foreign key are the two most important keys in a relational database. A primary key identifies each row in a table; a foreign key connects one table to another. Together they let you split data into clean tables and still link everything correctly.

💡 Primary key = unique id inside a table. Foreign key = a link to another table's primary key.

Primary key

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

The primary key (id) uniquely identifies each customer. It cannot be null, cannot repeat, and there is only one per table. Every table should have a primary key.

Foreign key

CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    amount INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Here customer_id is a foreign key that points to customers.id. It links each order to a customer, so you do not repeat the customer's name and details in every order.

The foreign key stores just the id of the related row. To see full details, you join the tables:

SELECT orders.id, customers.name, orders.amount
FROM orders
JOIN customers ON orders.customer_id = customers.id;

This connects each order back to its customer through the key — no duplicated data, always consistent.

Referential integrity

A foreign key enforces referential integrity: you cannot insert an order with a customer_id that does not exist in customers. MySQL rejects it. This stops "orphan" rows and keeps the data trustworthy. A foreign key can be null (optional link), but a primary key never can.

Side-by-side

PointPrimary keyForeign key
PurposeIdentify each rowLink to another table
Null allowedNoYes (optional link)
How many per tableOneMany
DuplicatesNot allowedAllowed
Points toNothing (it is the id)Another table's key

Common mistakes

  • No primary key. Every table needs one to identify rows.
  • Mismatched types. The foreign key column must match the referenced key's type.
  • Referencing a non-unique column. A foreign key must point to a primary or unique key.
  • Deleting referenced rows carelessly. Removing a customer with orders can break integrity unless handled.

Summary

  • A primary key uniquely identifies each row; never null, one per table.
  • A foreign key links a table to another table's key.
  • Foreign keys can be null and a table can have many.
  • They enforce referential integrity, preventing orphan rows.
  • Together they let you split data cleanly and join it back.

Next, see all key types in keys in DBMS explained, or enforce rules with referential integrity.

Frequently Asked Questions

What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row in its own table and cannot be null. A foreign key is a column in one table that refers to the primary key of another table, creating a link between them. One identifies; the other connects.
Can a table have more than one foreign key?
Yes. A table can have many foreign keys, each pointing to a different table. For example, an orders table can have a customer_id (to customers) and a product_id (to products). But a table has only one primary key.
Can a foreign key be null?
Yes, a foreign key can be null if the relationship is optional. For example, an employee row might have a null manager_id if they have no manager. A primary key, however, can never be null.
What is referential integrity?
Referential integrity means a foreign key must point to a value that actually exists in the referenced table (or be null). It stops you from adding an order for a customer that does not exist, keeping data consistent.
Does a foreign key have to reference a primary key?
It must reference a column that is unique in the other table — usually the primary key, but a unique key also works. This guarantees each foreign key value matches exactly one row in the referenced table.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicER Diagram Symbols and Examples Your progress: 135 / 151 (89%) · Lesson 136 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free