Let??™s break each of these steps down into a little more detail to help you fully understand
them.
Step 1: Defining a Foreign Key Column in Your Table
Your first step is to define a foreign key column that maps a recursive relationship back to the
primary key of your table. Trees are really a way of describing a parent/child relationship. In
our comment board example, we need a way to know when a record in our comments table is
a topic, a thread within a topic, or a comment/reply within a thread. We could use an additional
field that described the type, but this only gets us halfway there, because we still wouldn??™t know
things like which thread a given comment relates to.
A better solution is to add a foreign key field that simply maps back to the primary key
within the same table. This way, our comments can each have a foreign key value that maps
to a specific thread??™s primary key. Our thread records would have a foreign key value that
maps to a specific topic??™s primary key and so on. With this design, we can tell the type of a specific
record by its depth in the tree and we can also tell what record a given comment, thread,
or topic is a child of (if any).
Pages:
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243