Dear Jon:
I'm unsure what you mean "natural order." There is no such term with which
I'm familiar. The rows of a table are not stored in any particular order.
An order must be imposed by some kind of sorting. If the sorting is not
unique, then it does not completely order the rows.
You mention the problem with using an autonumber, that it would leave "gaps"
if rows were deleted. In the actual storage, these gaps may become filled
with new rows subsequently added. The order in which they are stored is up
to the mechanisms used by the database software to perform this, and can be
different from one database to another. They are not predictable or useful.
If an autonumber gives you the kind of sequence you want, except for the
gaps, then use an autonumber and then Rank the rows accordingly:
SELECT *,
(SELECT COUNT(*) + 1 FROM YourTable T1
WHERE T1.AutonumberCol < T.AutonumberCol)
AS Rank
FROM YourTable T
ORDER BY AutonumberCol
This will always give you a sequential numbering of the rows. However,
these Rank values will shift every time a row is deleted, so there are no
gaps, as you requested. It cannot be used as a permanent reference number,
as it may be constantly changing. You cannot have both.
If the "natural order" is the order in which the rows were added, put a
date/time stamp column in the table to contain this. You could than rank on
this date/time value, which is accurate enought that it may be unique.
Still, it is best to use a tie-breaker when ranking. Remember, if you have
multiple users, the order in which the rows appear will not respect which
user put them in. If several users are posting new rows to this table, they
will be intermingled according to how fast each user posts. In my opinion,
the "natural order" idea is rather deceptive, and likely to be
disappointing.
The bottom line is than any ordering of rows will be based on one or more
columns in the table. Whether this ordering is unique or not depends on
whether the columns used in the ordering form a unique value or not.
Is this at all helpful? How can I assist you further?
Tom Ellison