Primary Key vs Foreign Key School Database Example के साथ
Primary key अपनी table में हर row को uniquely पहचानती है (roll number जैसी); foreign key वही value दूसरी table में store होकर दोनों को जोड़ती है. पूरा example.
The roll-number analogy
Both keys in real tables
CREATE TABLE students (
roll_no INT PRIMARY KEY, -- primary key: unique + not null
name VARCHAR(50),
class VARCHAR(10)
);
CREATE TABLE fees (
fee_id INT PRIMARY KEY AUTO_INCREMENT,
roll_no INT, -- same value, different table
amount DECIMAL(8,2),
paid_on DATE,
FOREIGN KEY (roll_no) REFERENCES students(roll_no) -- the connection
);
students fees
+---------+-------+------+ +--------+---------+---------+
| roll_no | name |class | | fee_id | roll_no | amount |
| 101 | Aman | 10 |<------| 1 | 101 | 1500.00 |
| 102 | Priya | 10 |<------| 2 | 102 | 1500.00 |
+---------+-------+------+ | 3 | 101 | 800.00 |
+--------+---------+---------+
Notice: roll_no 101 appears once in students (primary key — no repeats) but twice in fees (foreign key — repeats allowed, Aman paid twice). That asymmetry is the heart of the whole concept.
What the foreign key actually protects
INSERT INTO fees (roll_no, amount) VALUES (999, 500);
-- ERROR: Cannot add or update a child row:
-- a foreign key constraint fails
-- (roll number 999 does not exist in students!)
DELETE FROM students WHERE roll_no = 101;
-- ERROR: Cannot delete or update a parent row
-- (Aman still has fee records pointing to him!)
Comparison table
| Point | Primary key | Foreign key |
|---|---|---|
| Purpose | Uniquely identify rows in own table | Connect to another table's primary key |
| Duplicates | ❌ Never | ✅ Allowed (Aman pays many times) |
| NULL | ❌ Never | ✅ Allowed (unless you forbid it) |
| How many per table | Exactly one (can be composite) | Many allowed |
| Lives in | Parent table | Child table |
| Guarantees | Row identity | Referential integrity |
Two follow-ups interviewers add
- "Can a table have two primary keys?" No — only one primary key, but it can be a composite key made of multiple columns together, like PRIMARY KEY (roll_no, exam_id) in a marks table.
- "Can a foreign key be NULL?" Yes, unless you add NOT NULL — a NULL foreign key means "no parent linked yet", like a fee record entered before assigning the student.
- Interview line: "Primary key = identity within the table; foreign key = relationship between tables, enforcing referential integrity."
Roll-number वाली analogy
दोनों keys असली tables में
CREATE TABLE students (
roll_no INT PRIMARY KEY, -- primary key: unique + not null
name VARCHAR(50),
class VARCHAR(10)
);
CREATE TABLE fees (
fee_id INT PRIMARY KEY AUTO_INCREMENT,
roll_no INT, -- same value, दूसरी table
amount DECIMAL(8,2),
paid_on DATE,
FOREIGN KEY (roll_no) REFERENCES students(roll_no) -- connection
);
students fees
+---------+-------+------+ +--------+---------+---------+
| roll_no | name |class | | fee_id | roll_no | amount |
| 101 | Aman | 10 |<------| 1 | 101 | 1500.00 |
| 102 | Priya | 10 |<------| 2 | 102 | 1500.00 |
+---------+-------+------+ | 3 | 101 | 800.00 |
+--------+---------+---------+
ध्यान दें: roll_no 101 students में एक बार है (primary key — repeat नहीं) लेकिन fees में दो बार (foreign key — repeat allowed, Aman ने दो बार fees दी). यही asymmetry पूरे concept का दिल है.
Foreign key असल में क्या बचाती है
INSERT INTO fees (roll_no, amount) VALUES (999, 500);
-- ERROR: Cannot add or update a child row:
-- a foreign key constraint fails
-- (roll number 999 students में है ही नहीं!)
DELETE FROM students WHERE roll_no = 101;
-- ERROR: Cannot delete or update a parent row
-- (Aman के fee records अभी भी उसे point कर रहे हैं!)
Comparison table
| Point | Primary key | Foreign key |
|---|---|---|
| Purpose | अपनी table में rows की unique पहचान | दूसरी table की primary key से connection |
| Duplicates | ❌ कभी नहीं | ✅ Allowed (Aman कई बार pay करता है) |
| NULL | ❌ कभी नहीं | ✅ Allowed (जब तक आप मना न करें) |
| हर table में कितनी | Exactly एक (composite हो सकती है) | कई allowed |
| रहती कहां है | Parent table | Child table |
| Guarantee | Row identity | Referential integrity |
दो follow-ups जो interviewers जोड़ते हैं
- "क्या table में दो primary keys हो सकती हैं?" नहीं — सिर्फ एक primary key, लेकिन वह कई columns से मिलकर बनी composite key हो सकती है, जैसे marks table में PRIMARY KEY (roll_no, exam_id).
- "क्या foreign key NULL हो सकती है?" हां, जब तक NOT NULL न लगाएं — NULL foreign key का मतलब "अभी कोई parent linked नहीं".
- Interview line: "Primary key = table के अंदर identity; foreign key = tables के बीच relationship, जो referential integrity enforce करती है."
Frequently Asked Questions
Primary key और foreign key में क्या अंतर है?
Primary key अपनी table में हर row को uniquely पहचानती है (न duplicates, न NULL); foreign key दूसरी table का column है जो primary-key values store करके tables को जोड़ता है और referential integrity enforce करता है.
क्या foreign key में duplicate values हो सकती हैं?
हां. कई child rows same parent को reference कर सकती हैं — एक student की कई fee payments हो सकती हैं — जबकि primary key की तरफ value कभी repeat नहीं होती.
एक table में कितनी primary keys हो सकती हैं?
Exactly एक, लेकिन वह कई columns वाली composite key हो सकती है, जैसे PRIMARY KEY (roll_no, exam_id).