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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.