B
B
Bogdan2019-07-11 22:24:00
PostgreSQL
Bogdan, 2019-07-11 22:24:00

Generate unique value?

Hello. Can you please tell me the best way to solve this problem? There is a field in the table 'serialNumber' STRING(9), and when creating a record, it is necessary to generate a unique value that is not in the database, the size of the field is 9 characters, and the possible characters are "0-9".
As an option, of course, you can create a random number in a trigger, in a loop, and then make a selection in the table for the presence of this number, until the selection returns an empty record.
But maybe there is a better solution?
P.S. There is one nuance, the numbers should not be in a row, it will be something like a serial number for a license

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2019-07-11
@bogdan_uman

create sequence serial_number_seq minvalue 1 maxvalue 999999999;

Make a new number - nextval('serial_number_seq')
Guaranteed to be unique. To the line, if you really want, you can bring yourself

A
Alexander, 2019-07-12
@NeiroNx

take a consecutive digit, take salt1 and salt2, add salt1 to the digit - take md5, add salt2 to the value - take md5 again
You can do more nesting - the main thing is not to "piss" the function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question