Jump to content

Generate Random number unique in a database


jkkenzie

Recommended Posts

hi!, is it possible to use rand() to generate unique number that will be saved in a database as a primary key? Since i dont want to have numbers like 00001 incrementing on the table. They dont look like real account numbers... i need something like 45642  or 95452 and the like.

 

thanks in advance.

Link to comment
Share on other sites

The best solution would probably be to pre-generate a list of random numbers in a random database.

 

When a new user is made, you select the first row from the database, assign that id to the user, and delete the row.

 

Keep in mind, this could cause a race condition, so locking the table may be necessary.

 

This way, you don't have to worry about generating random numbers until you find one that isn't used. That solution wouldn't scale well.

 

I agree with Pikachu though, I think the problem you're trying to solve is better approached in a different way.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.