How to save an order (permutation) in an sql db
Posted
by Bendlas
on Stack Overflow
See other posts from Stack Overflow
or by Bendlas
Published on 2010-04-14T17:02:05Z
Indexed on
2010/04/14
17:13 UTC
Read the original article
Hit count: 163
I have a tree structure in an sql table like so:
CREATE TABLE containers (
container_id serial NOT NULL PRIMARY KEY,
parent integer REFERENCES containers (container_id))
Now i want to define an ordering between nodes with the same parent.
I Have thought of adding a node_index
column, to ORDER BY
, but that seem suboptimal, since that involves modifying the index of a lot of nodes when modifying the stucture.
That could include adding, removing, reordering or moving nodes from some subtree to another.
Is there a sql datatype for an ordered sequence, or an efficient way to emulate one? Doesn't need to be fully standard sql, I just need a solution for mssql
and hopefully postgresql
EDIT
To make it clear, the ordering is arbitrary. Actually, the user will be able to drag'n'drop tree nodes in the GUI
© Stack Overflow or respective owner