M
M
McMike2019-04-18 11:50:56
PostgreSQL
McMike, 2019-04-18 11:50:56

How to choose a non-existent ID?

The task is to make an entry in some table with a random id from a given range (1000-100000 for example).
Of course, I can generate a random id on the php side, and if this is not yet in the database, then write it down. But as the table fills up, this approach will degrade. We need a query that can select a random number, one that is not in the corresponding table, i.e. choose from those missing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2019-04-18
@sim3x

https://medium.com/@emerson_lackey/postgres-random...
https://wiki.postgresql.org/wiki/Pseudo_encrypt

pseudo_encrypt(int) can be used as a pseudo-random generator of unique values. It produces an integer output that is uniquely associated to its integer input (by a mathematical permutation), but looks random at the same time, with zero collision. This is useful to communicate numbers generated sequentially without revealing their ordinal position in the sequence (for ticket numbers, URLs shorteners, promo codes...)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question