How to to create unique random integer ID for primary key for table?

MindGame picture MindGame · Nov 19, 2010 · Viewed 33.1k times · Source

I was wondering if anybody knew a good way to create a unique random integer id for a primary key for a table. I'm using MySQL. The value has to be integer.

Answer

Gary Green picture Gary Green · Nov 19, 2010

In response to: "Because I want to use that value to Encode to Base62 and then use that for an id in a url. If i auto increment, it might be obvious to the user how the url id is generated."

If security is your aim then using Base62, even with a "randomly" generated number won't help.

A better option would:

  • Do not re-invent the wheel -- use AUTO_INCREMENT
  • Then use a cryptographic hash function + a randomly generated string (hidden in the db for that particular url) to generate the final "unique id for that url"