Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

12.1.1 SQL and the Adjacency List Model > 12.1.1 SQL and the Adjacency List Mod... - Pg. 229

220 CHAPTER 12: GRAPHS IN SQL REFERENCES Nodes (node_id), end_node_id INTEGER NOT NULL REFERENCES Nodes (node_id), << other attributes of the edge >>, PRIMARY KEY (begin_node_id, end_node_id)); Technically, the begin_node_id can be the same as the end_node_id, and we can have a node without any edges. They are easy to diagram (see Figure 12.1). "Other attributes of the edge" are usually called a weight. These attributes model distance or travel time for maps, electrical resistance for circuits, cost of a process in workflow networks, and so forth. They are usually expressed as a numeric value on some scale and we want to do computations with them. Likewise, "other attributes of the node" are usually a name (say, "5-ohm resistor" in a circuit diagram) or where the weight (travel distance in a road map) is kept in the schema. 12.1.1 SQL and the Adjacency List Model There are only two approaches with an adjacency list model of a graph. You can use procedural code, which has two more options--a procedure or a cursor--or you can use a recursive CTE, but it is not recommended. Recursion is usually slow, and most SQL products choke at a certain depth, usually some power of two. Procedural approaches are usually direct translations of known algorithms from your favorite procedural programming languages into SQL/PSM. You replace the arrays with tables that mimic arrays. A A Figure 12.1