0

I've a simple question.

My database structure is:

id | name | left | right // no need to tell me that left, right are reserved keywords

The only data in the database:

1 | family | 1 | 2

Now, I want to add new child to the family: mother. So theoretically I should take the right value of the element to which I want to add the child and free-up some space after it.

UPDATE `hp_tree` SET `right`=`right`+2 WHERE `right` > 2;
UPDATE `hp_tree` SET `left`=`left`+2 WHERE `left` > 2;

And then simply insert the child mother:

INSERT INTO `hp_tree` SET `left`=2, `right`=3, `name`='Mother';

Now, the problem is, that this way the family (root element) right value is not updated. Am I doing it wrong?

Gajus
  • 69,002
  • 70
  • 275
  • 438

1 Answers1

1

Ok. I found this explained in Managing Hierarchical Data in MySQL. See the part starting with If we instead want to add a node as a child of a node that has no existing children.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Gajus
  • 69,002
  • 70
  • 275
  • 438