views:

118

answers:

1

I'm looking for opinions on how to model a directed graph that contains one special node.

Special node:

  1. Cannot have any edges leading to it.
  2. Cannot be removed.

Current design:

Tables: Nodes, Edges. Edges contains two columns; from_node_id and to_node_id, each referencing a record in the Nodes table.

Rather than storing the special node as the first record in the Nodes table, I decided not to keep a record for it at all, constructing it separately from any database queries. In the Edges table, NULL takes on a special meaning in from_node_id column, referring to the center node.

My motivation for using this design was that I wouldn't have to worry about protecting a center node record from deletion/modification or being referenced in the to_node_id column of the Edges table. This would also automatically prevent an edge from going from and to the same node. I realize there are some drawbacks to this design, such as not being able to make from_node_id and to_node_id a composite primary key, and probably many more.

I'm currently leaning towards making the center node an actual record and creating checks for that node in the relevant database methods. What's the best way to go about this design?

+2  A: 

I see some arguments against using NULL in this case.

  1. If nodes contain actual data you would have to hard-code data for the central node in the application.
  2. There will be trouble if the central node can be changed.
  3. The usual meaning of NULL is that there is no value or the value is unknown. Because of this another person who approaches the proposed design could find it unintuitive.

In other words I would prefer to have row in the database for the central node.

Adam Byrtek