non sequential unique key

• October 11th, 01:16 AM
barrioearth@gmail.com
non sequential unique key
My objective is to have a non-sequential unique key for each record in
a table. The key would be 5-digits long, comprised of letters and
numbers.

The reason for this is to have a more user-friendly key for users to
write down than the auto-increment key. Is there an algorithm to "map"
the auto-increment field to a non-sequential unique key that will not
create duplicates?

Or should I just generate a random 5-digit key, see if it already
exists in table, and repeat until a unique key is found?

-Brian

• October 11th, 03:04 PM
brian.scott.warren@gmail.com
Re: non sequential unique key
A 5-digit base 36 key would have somewhere around 60 million
permutations, so as long as you aren't updating too frequently and you
are ok with a max number of records being, I guess, around 1 million or
so, generating a random key and checking each time you add a record
should work ok.

Though, there are probably algorithms that would always ensure no
duplicates.

This wouldn't work for you because its sequential
1 - 00000
....
16 - 0000g
....
36 - 000010
....
11668264 - 6y3ag

but surely there's a way to do it without generating random values.

com wrote:

• October 11th, 07:55 PM
Jerry
Re: non sequential unique key
com wrote:
>
>[/ref]

How about something like this? Not guaranteed that you'll always have a
unique value, but with different seeds you should get a lot of them.

<?php

// Requires \$seed be passed. Probably the unique key for your row.

\$len = 5;
\$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

srand(\$seed);

\$key = "";
for (\$i = 0; \$i < \$len; \$i++)
\$key .= \$chars[rand(0, 35)];

?>

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
net
==================