| | |
Random unique primary key - is this possible?
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2006
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
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:
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]
http://dev.mysql.com/doc/refman/4.1/...functions.html
You could do something like this:
MySQL Syntax (Toggle Plain Text)
SELECT ROUND(RAND() * 123456789) as id
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]
![]() |
Similar Threads
- help on error handling in mysql ,in primary key (MySQL)
- generate unique primary key (MySQL)
- HELP! Need someone that Knows SQL to tell Me How to Access a Primary Key (C)
- Need to make program access a data base primary key number entered by user (C)
- Primary Key Issue (Visual Basic 4 / 5 / 6)
Other Threads in the MySQL Forum
- Previous Thread: Need help setting up 1st database please
- Next Thread: Project help with database
Views: 9247 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws bizspark changingprices communityjournalism contentmanagement contractors copyright count crm data database design developer development distinct drupal dui ec2 eliminate email enter enterprise error facebook form foss gartner gnu government gpl greenit groklaw groupware hiring hyperic images innerjoins insert ip join journalism keyword kickfire laptop legal license licensing linux maintenance mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource operand oracle pdf penelope php priceupdating query referencedesign remove saas search select sharepoint simpledb sourcecode spotify sql statement sugarcrm techsupport thunderbird update virtualization





