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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ryan
  • 41
  • 1
  • 3
  • 1
    Please refer http://stackoverflow.com/questions/5612656/generating-unique-random-numbers-within-a-range-php hope this will you – Roopendra Jan 18 '13 at 08: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. – John Mikael Gundersen Jan 18 '13 at 08:11
  • This isn't a unique. The other question is about generating random NUMBERS only. Both answers are very different. – Chuck Le Butt Nov 15 '13 at 20:44

3 Answers3

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: http://es1.php.net/manual/en/function.uniqid.php

m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • 2
    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). – martinstoeckli Jan 18 '13 at 10:07
  • Yes, I knowt tha uniqid() does not generate a real UUID. There is several other implementations, like this PECL Package: http://pecl.php.net/package/uuid – m4t1t0 Jan 18 '13 at 10:25
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")

dognose
  • 20,360
  • 9
  • 61
  • 107
0

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

Riho
  • 4,523
  • 3
  • 33
  • 48