Random unique primary key - is this possible?

Reply

Join Date: Mar 2006
Posts: 1
Reputation: iempleh is an unknown quantity at this point 
Solved Threads: 0
iempleh iempleh is offline Offline
Newbie Poster

Random unique primary key - is this possible?

 
0
  #1
Mar 24th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Random unique primary key - is this possible?

 
0
  #2
Mar 24th, 2006
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:

  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]

Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 9247 | Replies: 1
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC