A key is a field used to identify records in a table or to establish a connection between tables. For example: Primary Key and Foreign Key.
Keys are important because they help us:
A Primary Key is a field that uniquely identifies each record in a table.
Example: Consider the following Students table:
| Student_ID | Student_Name | Class |
|---|---|---|
| 101 | Rahul | 10 |
| 102 | Priya | 10 |
| 103 | Aman | 10 |
Here, Student_ID can be used as the Primary Key because every student has a different ID.
A Primary Key is required to identify each record uniquely.
Example: Suppose two students have the same name:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
| 102 | Rahul |
The Name field cannot uniquely identify the students. But: Rahul → Student_ID 101 Rahul → Student_ID 102
Therefore, Student_ID is set as the Primary Key.
A Null value indicates that a field has no value stored (nothing has been entered).
It is not Zero (0). It is not Blank (“ ”).
The AutoNumber data type is often used for a Primary Key because Access automatically generates a unique number for each new record.
A Foreign Key is a field in one table that is linked to the Primary Key of another table. It is used to establish a relationship between tables.
Example of Foreign Key: Suppose we have two tables.
| Student_ID | Student_Name | Class |
|---|---|---|
| 101 | Rahul | 10 |
| 102 | Priya | 10 |
| 103 | Aman | 10 |
Here: Student_ID → Primary Key
| Fee_ID | Student_ID | Amount |
|---|---|---|
| 1 | 101 | ₹2500 |
| 2 | 102 | ₹2500 |
| 3 | 103 | ₹2500 |
Here: Fee_ID → Primary Key Student_ID → Foreign Key
The Student_ID in the Fees table connects to Student_ID in the Students table. Students.Student_ID → Fees.Student_ID
| Primary Key | Foreign Key |
|---|---|
| Identifies | Connects |
| Uniquely identifies a record in its table. | Links one table to another table. |
| Contains unique values. | May contain repeated values. |
| Cannot contain Null values. | May contain Null values. |
| There is only one Primary Key per table. | A table can have one or more Foreign Keys. |
Referential Integrity ensures that relationships between related tables remain valid and consistent.
A relationship is a connection between tables through related fields (Foreign Keys).
We need relationships to connect to the information stored in separate tables. A Foreign Key connects two tables.
In a One-to-One relationship, one record in one table is related to only one record in another table.
In a One-to-Many relationship, one record in one table can be related to many records in another table.
In a Many-to-Many relationship, many records in one table can be related to many records in another table.