Study Notes: Primary Keys and Foreign Keys
Program: NCO (National Cyber Olympiad)
Subject: Software Applications & Programming
Topic: Relational Databases – Primary Keys and Foreign Keys
1. Simple Explanation
Imagine a database as a digital filing cabinet used by a school.
- A Primary Key is like a Student ID Card Number. Every student has one, no two students share the same ID, and it can never be left blank. It uniquely identifies who the person is.
- A Foreign Key is like the Student ID listed on a Library Book checkout form. It links the book record back to the specific student who borrowed it. It points to a Primary Key in another table to connect the two pieces of information together.
In computer science, databases store data in tables (rows and columns). To connect these tables logically without repeating data, we use Primary and Foreign keys.
2. Key Points
Primary Key (PK)
- Definition: A column (or a set of columns) that uniquely identifies each row in a database table.
- Characteristics:
- Uniqueness: No two rows can have the same Primary Key value.
- Not Null: It cannot contain empty (
NULL) values; every row must have one. - Limit: A table can have only one Primary Key (though it can consist of multiple columns combined, known as a Composite Primary Key).
- Olympiad Tip: Look for words like “uniquely identifies”, “cannot be NULL”, and “only one per table” in questions.
Foreign Key (FK)
- Definition: A column (or columns) in one table that refers to the Primary Key in another table.
- Characteristics:
- Relationship: It creates a link between two tables (Parent and Child).
- Duplicates Allowed: Unlike a Primary Key, a Foreign Key can have duplicate values (e.g., many students can be in the same grade or borrow multiple books).
- Nulls Allowed: Unless restricted, a Foreign Key can often be empty (e.g., a student hasn't borrowed a book yet).
- Referential Integrity: A Foreign Key value must match an existing Primary Key in the related table (you can't assign a Student ID of a non-existent student to a library book).
- Olympiad Tip: Look for phrases like “establishes a relationship between tables”, “references a primary key”, and “referential integrity”.
3. Worked Example
Let’s look at two tables in a school database: Students and Scores.
Table 1: Students (Parent Table)
| Student_ID (PK) | Name | Age |
|---|---|---|
| S101 | Alice | 14 |
| S102 | Bob | 15 |
| S103 | Charlie | 14 |
- Primary Key:
Student_ID. It uniquely identifies each student. You cannot have two students with IDS101.
Table 2: Scores (Child Table)
| Score_ID (PK) | Student_ID (FK) | Subject | Marks |
|---|---|---|---|
| 501 | S101 | Math | 95 |
| 502 | S101 | Science | 88 |
| 503 | S102 | Math | 92 |
- Primary Key:
Score_ID(identifies each score entry). - Foreign Key:
Student_ID. Notice howS101appears twice because Alice took two subjects. This column links the exam scores back to theStudentstable. - Violation Example: If you tried to insert a score with
Student_ID = S999, the database would reject it becauseS999does not exist in theStudentstable. This protects referential integrity.
4. Quick Revision Summary
| Feature | Primary Key (PK) | Foreign Key (FK) |
|---|---|---|
| Primary Purpose | Uniquely identify a record in a table. | Link two tables together. |
| Allows NULL values? | ❌ No (Must always have data). | ✔️ Yes (Usually allowed). |
| Allows Duplicates? | ❌ No (Must be unique). | ✔️ Yes (Multiple rows can link to the same parent). |
| Count per Table | Only 1 per table. | Can have multiple per table. |
| Source | Generated within its own table. | Borrowed from |