i have a stack of messages in database table.
i want to send these messages by their priority so i added “Priority” column to “messages” table.
but what if i want to insert “cram” message between two messages and give the previous priority to this new message?.
Should i update all messages priority under this message.
So please give me the perfect design for my database table to support priority update.
Use a float column for Priority rather than an int.
Then, to insert a message between two others, assign the average of the two messages’ Priority values as the new message’s Priority. (E.g., to insert a cram message between a messages with Priority 2 and 3, assign it a Priority of 2.5).
By doing this, you don’t have to update any other messages’ priority, and you can continue to average/insert between those, etc. until you bump up against the decimal accuracy limits of a float (which will take awhile, especially if the raw Priority values tend to be small).
Or, add another column after Priority in the ORDER BY. In the simplest case, use bit column called “ShowAfter” with a default value of 0. When you insert a cram message, give it the same Priority as the message you want to see it after, but a [ShowAfter] value of 1.