Answer the question
In order to leave comments, you need to log in
What is the best way to generate a unique numeric number?
There is a certain product number, it must be unique
, you need to generate this number. how to generate - freedom, at least according to the rules, at least randomly,
for example
, they created the first:
Prefix-1323789634
second:
Prefix-7689465476
third:
Prefix-5478639379
it is important that this is not just one after the other, but some kind of random or rule.
and so that at one fine moment it doesn’t turn out that there is already such an exception in the database
- I thought through a sequence in the database, but only oracle has it outside the order, it doesn’t fit. Or is it possible to somehow generate a sequence randomly?
- doing random in the code, and then checking if it already exists in the database is also not very pleasant
- there was an idea to put somehow an account_id-another_id_of_something, but this data, I'm afraid, will grow strongly
Answer the question
In order to leave comments, you need to log in
In the vast majority of cases, the time in milli/nanomili/micro/nanoseconds of System.currentTimeMillis() or System.nanoTime() can be taken as a unique identifier. You can combine this time with a thread identifier (to avoid collisions when using multithreading) or even a server identifier if several servers are used (by the way, in this case it is not even necessary to synchronize the time on the machines).
ps there are sequences in postgres, just nothing prevents them from being implemented based on records in the table, just as almost all databases without sequences support autoincrement, respectively, implement a sequence based on them, deleting a smaller value after receiving a new one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question