MySQL + SQL · Lesson 1
Primary Key Vs Foreign Key
The Difference
| Feature | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Uniquely identify a row | Link to another table |
| NULL allowed? | No | Yes |
| Duplicates? | No | Yes |
| How many per table | One | Many |
| Auto index | Yes | Index recommended |
Together in One Example
students(roll_no PK, name)
fees(fee_id PK, roll_no FK -> students.roll_no, amount)roll_no is PRIMARY in students and FOREIGN in fees.
Summary
- Same column can be a primary key in one table and a foreign key in another.
- PK: unique & not null; FK: link & can repeat/be null.