Generate unique customer reference number


Generate unique customer reference number



I am working on my own small application which allows customers to create "jobs" which are stored in DB with UUID as the primary key. However when displaying jobs via UI I'd like a shorter job-id compared to the uuid ... something like JOB-123456



Just wondering whats the best way to generate these human readable reference numbers where the possibility of collisions are minimized ?





You'd better use database tools for that: sequences in Oracle or autoincrement column in MySQL
– Ivan
Jul 2 at 19:57





If you have a DB, I would use that.
– Peter Lawrey
Jul 2 at 19:58





The problem with revealing a DBMS-generated AutoNumber or AutoIncrement data type to an end-user is that you will not end up with sequential values and that will confuse the end user. Inevitably in any moderately complex system the DBMS will use a number in an intermediate operation or in a transaction and then that number will be unavailable forever.
– nicomp
Jul 2 at 20:03




2 Answers
2



It sounds like you want to roll your own toString() method, in which case you could pass in the UUID's and pull only the first/last 6 characters off of each.



You could do something similar git's shortened hashes, and show just a part of the UUID. For example:


$git log --pretty=format:'%h'
0970e05
7122fc7
b535c42
aeefb7c
4b67b04



So, if you have a UUID like 4e6bcef6-c28b-4316-af72-c143925e0c18, maybe you display 4e6b or 0c18. You're far more likely to get a collision this way than if you had shown the full UUID, but if it's a small enough application, it's still pretty unlikely.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?