I like to use a 16-character base-60 varchar for my random primary keys. Here's a tiny table:
mysql> create table foo (k varchar(16), v varchar(255), primary key(k));
... and here's some rudimentary PHP, which will hit function k() until it finds an unused key.
<?php
require_once 'connect.php';
$v = 'A value that needs a unique random key.';
while (!mysql_query("INSERT INTO foo VALUES ('" . k() . "', '$v')")) {};
function k() {
$t = "0123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
$r = "";
for ($i = 0; $i < 16; $i++) {
$r .= substr($t, rand(0, 59), 1);
}
return $r;
}
?>
The chances of a collision are small--60 to the 16th--so k() almost never gets called twice. Bonus: if I ever need to make that key larger it's a varchar, so I can add bytes without scragging everything that's already in the table.