3

I have to create unique codes for each "company" in my database.

The only way I see this to be possible is to create a random number with rand() and then check if the number exists for this "company" in the DB, if it does recreate.

My question is: Is there not a better way to do this - a more efficient way. As if I am creating 10 000 codes and there are already 500 000 in the DB it's going to get progressively slower and slower.

Any ideas or tips on perhaps a better way to do it?

EDIT:

Sorry perhaps I can explain better. The codes will not all be generated at the same time, they can be created once a day/month/year whenever.

Also, I need to be able to define the characters of the codes for example, alpha numberic or numbers only

3
  • 1
    Please refer stackoverflow.com/questions/5612656/… hope this will you Commented Jan 18, 2013 at 8:10
  • Does it have to be codes? Could you get away with just using AUTO_INCREMENT in mysql? If not you could try a random with many enough digits (i.e. something that is so large that it will take a very long time before it comes to the problem where it slows down the system) Third would be combining random numbers with random picked characters to make for even more possibilities and less chance for it slowing the system. Commented Jan 18, 2013 at 8:11
  • This isn't a unique. The other question is about generating random NUMBERS only. Both answers are very different. Commented Nov 15, 2013 at 20:44

3 Answers 3

5

I recommend you to use "Universally Unique Identifier": http://en.wikipedia.org/wiki/Universally_unique_identifier to generate your random codes for each company. In this way you can avoid checking your database for duplicates:

Anyone can create a UUID and use it to identify something with reasonable confidence that the same identifier will never be unintentionally created by anyone to identify something else. Information labeled with UUIDs can therefore be later combined into a single database without needing to resolve identifier (ID) conflicts.

In PHP you can use function uniqid for this purpose: https://www.php.net/manual/en/function.uniqid.php

Sign up to request clarification or add additional context in comments.

2 Comments

Using a Uuid is good advice, but keep in mind that PHP's function uniqid() does not generate a real Uuid, instead it generates a value based on microtime, with a much smaller possible space (a Uuid has 128-bit).
Yes, I knowt tha uniqid() does not generate a real UUID. There is several other implementations, like this PECL Package: pecl.php.net/package/uuid
2

MySQL's UUID Function should help. http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid

INSERT INTO table (col1,col2)VALUES(UUID(), "someValue")

Comments

0

If the codes are just integers then use autoincrement or get the current max value and start incrementing it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.