Create unique random record number in MySQL

What’s the best way to create a random number in MySQL to be used as the unique record number.

Has to be unique and random so it can’t be guessed as part of a sequence.

Is there an option within MySQL or using PHP?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You want a UUID: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

Walter

On Mar 22, 2012, at 5:16 PM, David Owen wrote:

What’s the best way to create a random number in MySQL to be used as the unique record number.

Has to be unique and random so it can’t be guessed as part of a sequence.

Is there an option within MySQL or using PHP?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks. It does not return as quite a user friendly number as I’d hoped. I’ll have to think of a different way.

David

On 22 Mar 2012, at 21:24, Walter Lee Davis email@hidden wrote:

You want a UUID


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

What are you looking for? I have a little ruby method that can make a very nice alphanumeric code, or pure numeric, pretty much anything you like. It could be ported to anything, it’s a very neat method.

Walter

On Mar 22, 2012, at 6:03 PM, David Owen wrote:

Thanks. It does not return as quite a user friendly number as I’d hoped. I’ll have to think of a different way.

David

On 22 Mar 2012, at 21:24, Walter Lee Davis email@hidden wrote:

You want a UUID


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis email@hidden wrote:

What are you looking for?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If the numbers have to be truly unique and random, then you need to store the ones you’ve used already so you don’t repeat yourself. So assume you’ll need a database. Then you make a random key, test to see if it exists already, if it does, make another one, if not, store it in the database and use it.

I’m not near the computer with that project on it, but I’ll send it later.

Walter

On Mar 22, 2012, at 6:37 PM, David Owen email@hidden wrote:

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis email@hidden wrote:

What are you looking for?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Here’s the Ruby version:

def set_confirmation_code
code = Array.new(7) { (0…9).to_a[rand(10)] }.join.to_s
if Purchase.find_by_confirmation_code(code)
return self.set_confirmation_code()
else
self.confirmation_code = code
end
end

To do this in PHP/MySQL, I would use MyActiveRecord and something like the following:

require(‘config.inc.php’);
class Order extends MyActiveRecord {
}

function set_confirmation_code(){
$code = ‘’;
foreach(range(0,7) as $digit){
$code += (string) rand(0,9);
}
if(MyActiveRecord::FindFirst(‘Order’, ‘code = "’ . $code . ‘"’)){
set_confirmation_code();
}else{
$order = MyActiveRecord::Create(‘Order’, array(‘code’ => $code));
$order->save();
return $code;
}
}

Have a look at the Github project for MAR for more details about setting up a project with that framework. GitHub - walterdavis/myactiverecord: Fork of MyActiveRecord by Jake Grimley

Walter

On Mar 22, 2012, at 7:27 PM, Walter Lee Davis wrote:

If the numbers have to be truly unique and random, then you need to store the ones you’ve used already so you don’t repeat yourself. So assume you’ll need a database. Then you make a random key, test to see if it exists already, if it does, make another one, if not, store it in the database and use it.

I’m not near the computer with that project on it, but I’ll send it later.

Walter

On Mar 22, 2012, at 6:37 PM, David Owen email@hidden wrote:

I’m not entirely sure. Just some playing around with ideas which might come to use in a project in the pipeline.

Esentially the client needs to add a unique number at the end of an link in an email which would retrieve details on the site. I’m having a re-think as it just bound to go wrong on the client side. I’m thinking the only way would be a full registration login, etc. To do anything slightly clever.

David

On 22 Mar 2012, at 22:14, Walter Lee Davis email@hidden wrote:

What are you looking for?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options