We have a large database with enquiries, each enquirys is referenced using a Guid. The Guid isn’t very customer friendly so we want to the additional 5 digit ‘human id’ (ok as we’ll very likely won’t have more than 99999 enquirys active at any time, and it’s ok if a humanuid reference multiple enquirys as they aren’t used for anything important).
1) Is there any way to have a IDENTITY column reset to 1 after 99999?
My current workaround to this is to use a INT IDENTITY(1,1) NOT NULL column and when presenting a HumanId take HumanId % 100000.
2) Is there any way to automatically ‘randomly distribute’ the ids over [0..99999] so that two enquirys created after each other don’t get the adjacent ids? I guess I’m looking for a two-way one-to-one hash function??
… Ideally I’d like to create this using T-SQL automatically creating these id’s when a enquiry is created.
If you absolutely need to do this in the database, then why not derive your human-friendly value directly from the GUID column?
This will give you a random-ish value between 0 and 99999, derived from the first 3 bytes of the GUID. If you want a larger, or smaller, range then adjust the divisor accordingly.