I know that this sort of goes against the principles of a relational database but let me describe the situation.
I have a page where the user will place a number of items.
________________
| -Item1 |
| -Item2 |
| -Item3 |
| -Item4 |
|________________|
These items have must stay in a the order the user gives them. However this order may be changed an arbitrary number of times by the user.
________________
| -Item1 |
| -Item4 |
| -Item2 |
| -Item3 |
|________________|
Approach 1
My original thought was to give the items an index to represent thier place in the list
Page Item
----------- ---------------
FK | pid FK | pid
| name PK | iid
| index
| content
With this solution you can select items where pid = Page.pid
and order by index
which is convenient. However every time you change the order you have to change anywhere between one other item (best case) and all the other items (worst case).
Approach 2
I also considered making a "linked list" like data structure where each item points to the next item in the list.
Page Item
----------- ---------------
FK | pid FK | pid
| name PK | iid
| next
| content
This potentially makes changing the order less expensive but we would have to rely on front end programming to extract the order.
Is there an approach that I haven't thought of? Please let me know.
See Question&Answers more detail:os