Forum Moderators: coopster
I'm generating a six-digit random number (using mt_rand()) to be used in a db tables primary key column for my table records. Six digits would certainly seem long enough to ensure that no two primary keys would be the same, but depending upon the number of records in that table, I'm starting to have my doubts.
What do the good folks here say about this?
Is a six digit mt_rand() number random enough? Or, once the number is generated, should I query the table and compare the number just generated against the existing PKs to ensure uniqueness before inserting a new record into the table?
Neophyte
The client specification for this project dictates that a random and unique six-digit number be generated for each record which would be used as a "client ID number"
generate your random number list in advance and store it in a table `random_numbers` with fields `number_id` and `random_number` - that way any auto_increment field of another table can be translated into a unique random_number easily.
How do you foresee its implementation
Direct mapping would mean:
main table named `clients` contains `rowid` (auto_increment)
To get client_id:
SELECT * FROM `clients`,`random_numbers` WHERE `random_numbers`.`rowid`=`clients`.`rowid` AND ....
i.e. don't actually store the random number in the `clients` table at all but 'translate' from the auto_incremented number to the respective random number as required.
This shifts the database load to the output/display end rather than the assignment/data end and so keeps your `clients` table itself with a good efficient index which can be used internally for joins to other client-related tables (rather than using the random number for the join).