Howto do a recursive query?
Posted in 1997
Topics: Stored Procedures & SPL, Data Types & Schema Design, Triggers, Constraints & Referential Integrity
Hi
I have a set of data organised into a treelike structure.
I have to be able to do a query like this to the data:
Given a node in the tree, show me all nodes that are in the subtree
below the given node.
I was thinking about storing the data and the treestructure in
a table like:
CREATE TABLE LinkTree (
NodeNo SERIAL,
ParentNode INTEGER,
Info VARCHAR(10),
PRIMARY KEY (NodeNo),
FOREIGN KEY (ParentNode) REFERENCES LinkTree(ParentNode)
);
that is, each node would be given a number and the children of
that node would have a pointer to the parent.
Using this structure I should be able to select all nodes
that have the given node as a parent, and then recursively
execute this query again with using all the NodeNo from
the query as the given node. The query should terminate
when a query returns an empty result set.
Does this make any sense at all? If so, how should I
create something like this? I realise that I have to
use SPL, this wont be a oneliner in standard SQL :)
I somehow feel that recursion does not fit into the relational
model.
I'm implementing this in IUS, does it have any features
I should take advantage of?
TIA
Hrafnkell
--
//-----------------------//-------------------------------------------------
// Hrafnkell Eiriksson // Student of Computer- and Electrical engineering
// hkelle@rhi.hi.is // at the Univeristy of Iceland
// // "Blessed are they who go around in circles,
// Finger for PGP key // for they shall be known as Wheels"