944,111 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 13815
  • MySQL RSS
Mar 24th, 2006
0

Random unique primary key - is this possible?

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iempleh is offline Offline
1 posts
since Mar 2006
Mar 24th, 2006
0

Re: Random unique primary key - is this possible?

MySQL does not have a direct function to produce this, but you may find the RAND() function helpful. Find RAND() on this page:
http://dev.mysql.com/doc/refman/4.1/...functions.html

You could do something like this:

MySQL Syntax (Toggle Plain Text)
  1. 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:
[PHP]
//Generates a 32 character identifier that is extremely difficult to predict.
$id = md5(uniqid(rand(), true));
[/PHP]

Reputation Points: 36
Solved Threads: 6
Posting Whiz
Troy is offline Offline
354 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: Need help setting up 1st database please
Next Thread in MySQL Forum Timeline: Project help with database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC