When Do We Need Variable-Length Records?
Scenario 1: Different Field Sizes
Student 1: Name="John" (4 chars)
Student 2: Name="Elizabeth" (9 chars)
Scenario 2: Multiple Record Types in One File
- Storing both student and instructor records together
Scenario 3: Repeating Fields
- A student can have multiple phone numbers
How to Store Variable-Length Records
Structure:
-
Fixed-Length Part: Contains metadata about variable parts
-
Variable-Length Part: Contains actual variable data
Example Record Structure:
Fixed Part:
- ID: 12345
- Age: 20
- Name offset: 20 (name starts at byte 20)
- Name length: 9 (name is 9 characters)
- Address offset: 29 (address starts at byte 29)
- Address length: 15 (address is 15 characters)
Variable Part:
- Bytes 20-28: "Elizabeth"
- Bytes 29-43: "123 Main Street"
Null Bitmap
For handling NULL values efficiently:
Bitmap: 01101000
Means: Field1=null, Field2=present, Field3=present, Field4=null, etc.
Slotted-Page Structure
Think of it like a parking lot:
-
Header Area: Directory showing where each car (record) is parked
-
Free Space: Empty parking spaces in the middle
-
Record Storage: Cars parked from the back of the lot
Structure:
[Header with record locations] [Free Space] [Records from end]
Example:
Block Header:
- Number of records: 3
- End of free space: byte 100
- Record 1: starts at byte 200, size 50
- Record 2: starts at byte 150, size 30
- Record 3: starts at byte 120, size 25
Free space: bytes 100-119
Records stored: bytes 120-250
Operations:
-
Insert: Add record at end of free space, update header
-
Delete: Mark record as deleted in header, may move other records to consolidate space