Get a unique identifier without insertion

I'm looking for the best way to get a unique number (guaranteed, not some random string or current time in milliseconds and a reasonable length of about 8 characters) using MySQL (or other greeting options).

I just want to run some SELECT ... and always return a unique number without any insertion into the database. It’s just that it increases some stored value and returns a value and can process many requests at the same time, without significantly blocking the application.

I know that I can do something with combinations of random numbers with higher stems (for shorter lengths), which can make it very unlikely that they overlap, but do not guarantee it.

It just seems like there must be some easy way to get this.

To clarify...

I need this number to be short, as it will be part of the URL, and it is ok to request a line lock for a short period of time. What I was looking for, maybe some kind of team, that underhood is doing something like this ...

 LOCK VALUE RETURN VALUE++ UNLOCK VALUE 

Where VALUE is stored in the database, there may be a MySQL database.

+4
source share
4 answers

You are looking for UUID() .

http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid

 mysql> SELECT UUID(); -> '6ccd780c-baba-1026-9564-0040f4311e29' 

It will return a 128-bit hexadecimal number. You can tag if necessary.

+3
source

I'm not sure that I understand exactly, maybe something like this:

 SELECT ROUND(RAND() * 123456789) as id 

The more you make the number, the larger your identifier.

There is no guarantee of uniqueness, of course, this is a quick hack in the end, and you should implement code verification to deal with the randomness in which the duplicate is inserted, but maybe this will serve your purpose?

Of course, there are many other approaches for this.

You can easily use most scripting languages ​​to generate this for you, e.g. php:

 //Generates a 32 character identifier that is extremely difficult to predict. $id = md5(uniqid(rand(), true)); //Generates a 32 character identifier that is extremely difficult to predict. $id = md5(uniqid(rand(), true)); 

Then use $id in your request or whatever you need for your unique identifier. In my opinion, the advantage of this in the scripting language when interacting with the database is that it is easier to check for the purposes of use / use and act accordingly. For example, in your example, no matter what method you use, if you want to be 100% sure of the integrity of the data, you need to make sure that there are no duplicates of this identifier elsewhere. This is easier to do in a script than in SQL .

Hope this helps my friend, good luck!

+1
source

Is a unique number associated with a specific row in a table? If not, why not call rand (): select rand(); The return value is between zero and one, so scale as desired.

+1
source

Great question.

The shortest answer is simply not possible according to your specifications.

The long answer is the closest approach to this MySQL UUID , but it is neither short nor sortable (i.e.: the previous UUID value is larger / smaller than the previous one).

For UUID or not for UUID? - a good article describing the pros and cons regarding their use, also addressing some reasons why you may not have what you need

+1
source

Source: https://habr.com/ru/post/1387323/


All Articles