I am creating a database and would like to create a unique primary key that is randomly generated. Does MySQL have any feature that will do this?

I don't want to use an auto-increment, because when I have a page like "whatever.php?id=xxx", I don't want a user to be able to just guess another id.

I know I could program my own UID-generating script, but it would be much easier if the database could generate one itself, and I wouldn't have to worry about regenerating the same id.

The page isn't displaying anything that's confidential, so making users log in to view data is overkill. I just want a simple way to give certain information out only to certain people.

11 Years
Discussion Span
Last Post by Troy

MySQL does not have a direct function to produce this, but you may find the RAND() function helpful. Find RAND() on this page:

You could do something like this:

select ROUND(RAND() * 123456789) as id

The larger you make the number, the larger your id. No guarantees about uniqueness of course, but maybe this would serve your purpose?

Here is how I generate unique id's in PHP:

//Generates a 32 character identifier that is extremely difficult to predict.
$id = md5(uniqid(rand(), true));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.