By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,538 Members | 2,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,538 IT Pros & Developers. It's quick & easy.

Updating a nested set

P: n/a
I'm basing some work on Joe Celko's excellent idea of using nested sets
to represent an organizational structure as opposed to an adjacency
list. By and large it's a great idea, but not without its pitfalls. I'm
writing now to ask your collective opinion of the best practice.

I've got my nested set table, and since the left bound is unique, I'm
using it as a foreign key to the table that contains assignment
information. The problem is that if I add an element to the middle of
the nested set, the left bound change on the elements to the right of
the insert. This requires an update of the foreign key on the assignment

I see two options:
1. Update the foreign key on the assignment table with a multi-table
UPDATE as I nudge the necessary left bounds:

UPDATE elements, assignments
SET lft =3D IF( lft > ?, lft + 2, lft ),
rgt =3D IF( rgt >=3D ?, rgt + 2, rgt ), =3D IF( id > ?, id + 2, id )
WHERE rgt >=3D ?;
(given that '?' is the right node of the parent entitiy)

2. Don't use the left bound as the foreign key. While this seems
straight forward, it complicates queries like "all assignments for this
element and it's sub-elements."

I'm leaning toward #1, but I have the nagging feeling that it's more
complex than the example query is able to manage.
There could be a solution I haven't considered, as well... I'd love to
learn about it.


Jim Knepley
Network Security Engineering

MySQL General Mailing List
For list archives:
To unsubscribe:***********

Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.