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.
💻 Live Code Editor
Is page ki language (MYSQL) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.