2

I'm looking to add a sort INT UNSIGNED field to my table so I can sort the items depending on their order without touching their id. Is it possible to give this column a value much like auto_increment but instead of incrementing by 1, MySQL increments by 10 or 20 or even 30? The large gaps allow me to shuffle their order.

I may be thinking about this the wrong way so please tell me what you think.

1
  • Don't you have an autoincrement id in the same table? Commented Nov 27, 2011 at 17:32

4 Answers 4

2

In fact, there is absolutely no need in "large gaps".
You can always swap 2 numbers - that's enough for the user-defined sorting.

Just update your sorting field to the same value with id initially and then move it to whatever position.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll just swap sort values then. The default of which would be their id.
2

You can do that by changing auto_increment_increment:

SET @@auto_increment_increment=10;

More information

1 Comment

@Arif one have to see the question first. And then decide, if it ever need a "perfect" but direct answer.
1

There is one issue: typically, for each table there can be only one column with auto_increment - if you have an id column that's also auto_increment - which really have much better reason to be auto_increment, this whole idea won't cut.

Secondly, it probably does not make much sense any way, since there is no reason for the latest inserted row (by default) to have the highest sorting order.

Comments

1

appart from setting the autoincrement to 10 like suggested you should also consider make it a unique key to speed up sorting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.