I know that in SQL databases data is not stored in a particular order and that ordering is only done via ORDER BY statements.
What is the usual approach if I need to store data in a arbitrary order?
Example:
I have names and I want to store them in a certain arbitrary order like:
name
-----
foo
hello
bar
One approach would be to have an auxiliary field order like this:
order name
-----------
1 hello
0 foo
2 bar
then I can sort by the order field.
But if I need to insert another name (newname) that should be between foo and hello I need to change the order fields of hello and bar so the table would be this:
order name
-----------
2 hello
0 foo
3 bar
1 new
- Is this approach correct?
- Couldn't the renumbering process be very slow if the table contains many rows?
- Are there better approaches for this kind of problems?