Professional Web Applications Themes

non sequential unique key - MySQL

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...

  1. #1

    Default 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

    barrioearth@gmail.com Guest

  2. #2

    Default 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: 

    brian.scott.warren@gmail.com Guest

  3. #3

    Default 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)];

    echo "Your key is $key\n";


    ?>




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

Similar Threads

  1. Unique Form inserting into many tables using unique id
    By Gabo Navarro in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 0
    Last Post: September 16th, 06:25 PM
  2. Auto generate unique order number sequential
    By DutchCur in forum Macromedia ColdFusion
    Replies: 1
    Last Post: February 17th, 01:34 PM
  3. sequential functions?
    By originalknappster webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 5th, 09:17 PM
  4. sequential loss in jpeg
    By Eric Matthes in forum Adobe Photoshop Elements
    Replies: 5
    Last Post: August 30th, 04:51 PM
  5. Replies: 0
    Last Post: August 7th, 07:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139