A primary index is an ordered file whose records are of fixed length with two fields. The first field is the same as the primary key of the data file, and the second field is a pointer to the data block where that specific key can be found.
The primary index can be classified into two types
Let's assume a simple table named `Students` that stores data about students. The table has the following structure:
| RollNumber (Primary Key) | Name | Age |
|--------------------------|-------|-----|
| 1001 | Madhu | 20 |
| 1003 | Mahi | 22 |
| 1007 | Ramu | 21 |
| 1010 | Durga | 23 |
Assuming each block of our storage can hold two records, our blocks will be:
Dense Primary Index:
| Key (RollNumber) | Pointer to Block |
|------------------|------------------|
| 1001 | Block 1 |
| 1003 | Block 1 |
| 1007 | Block 2 |
| 1010 | Block 2 |
Sparse Primary Index:
| Key (RollNumber) | Pointer to Block |
|------------------|------------------|
| 1001 | Block 1 |
| 1007 | Block 2 |
In the dense primary index, there's an entry for every key value, whereas in the sparse primary index, only the first key value of each block (the block's anchor record) gets an entry in the index. In this way, sparse indexes are more compact but might require more sequential block reads if a search key isn't an anchor for a block.
However, while the primary index provides rapid access and ensures the uniqueness of records, it can be slower for insertions, updates, and deletions, as maintaining the sorted order of the index can be time-consuming.