Archive for March 26th, 2009
Sorting lists: moving items
Comments enabled. I *really* need your comment
This is article 2 of 6 on linked lists in MySQL:
- Sorting lists
- Sorting lists: moving items
- Sorting lists: adding items
- Sorting lists: deleting items
- Sorting lists: moving blocks
- Sorting lists: deleting blocks
Today, I'll expain how to move items in these lists.
To move an item in the linked list we need to relink it. If we move item A after item B, we need to update at most three rows:
A'sparentis updated toBA's child'sparentis updated toA'sparentB's childparentis updated toA
B here may be a real row or a surrogate id of 0 which we use to designate the first row's parent.
Moving
means A after 0moving
.A to the top of the list
Unfortunately we cannot rely on a single statement to perform these updates, because we have a UNIQUE INDEX on parent.
Read the rest of this entry »
Subscribe in a reader