Dana Holt writes:

 > Can I automatically generate a random, unique, integer value
 > in a certain range when inserting data into a column using SQL?

"Random"? Like how "random"?

* Math.random() random? And still unique? That's really tough..
* Or just "you don't care, but not sequential" "random"?
* Or just "unique, and you don't care if it's sequential or not,
   but you just don't want to be bothered to start from where
   you left off, or worry about other client hosts" "random"?

Generally, you'd want to use UUIDs for the simplest case of this, but the
"standard" UUID algorithms (DCE, etc.) generate 128-bit UUIDs (and so are 
best used as CHAR(36) (with the standard DCE representation) or CHAR(32) if 
you simply encode the whole thing as a single hex string.

Or use two 64-bit INTs as the primary key, and split the 128-bit key among 
them.

You can fake 64-bit "U"UIDs for limited scenarios (like within your own
intranet, etc.) by cooking up an ID based on, e.g., 16 bits of IP
address, 32 bits of a start time (of the database), and 16 bits of
counter (where you carry over any counter overflows into the start
timestamp part). Vary the parameters, of course, based on your intranet
situation.. Or some such method..


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to