views:

36

answers:

2

Let's say that there is a file that contains an unsorted list of student information, which includes a student ID number as well as other information.

I want to make a program that retrieves student information based on student ID number. In order to make it efficient, I store the student IDs in a B-tree.

So when I enter a student ID number, it searches the B-tree to see if its there or not. It also does one more thing. If it finds the student ID number, then it also returns where in the file that student's information is. This is the secondary key. The program uses this information to locate the rest of the student's information and prints it to screen.

Can this be done? Is this how a b-tree works?

+1  A: 

A B-tree works by storing values against keys, and allowing you to find a given key and its associated value in logarithmic time. So the second thing isn't called a secondary key, it's just the value. In this case, it's an offset into a file, which makes it essentially a pointer to the value.

This is a perfectly reasonable and very common way to use a B-tree.

Marcelo Cantos
+1  A: 

What you describe has nothing to do with B-trees in particular (BTW, are you sure you understand what a B-tree is? Many people confuse them with binary trees), and the file position is not a "secondary key", it's a value that the student ID maps to.

However, it is true that relational databases internally work in a way quite similar to what you describe - database records are stored wherever there's free space, and indexes map values from the indexed column to the location of the record.

Michael Borgwardt
I know the difference between a B-tree and a binary tree. A B-tree can have more than one value in each node.
Phenom
@Phenom: A more important distinction is that B-Tree nodes can have more than 2 child nodes (typically many more).
Michael Borgwardt