I use to generate unique & random string mysql function UUID()
, it's possible to set length of this generated string? I need only 8 letters.
This insert i do with php insert function...
Thank you in advance.
I use to generate unique & random string mysql function UUID()
, it's possible to set length of this generated string? I need only 8 letters.
This insert i do with php insert function...
Thank you in advance.
It is not possible with the UUID as others have mentioned since it has a defined pattern as specified by the standard. However you can use this function in PHP, which will create the unique ids for you of letters and numbers
<?php
function generateUUID($length) {
$random = '';
for ($i = 0; $i < $length; $i++) {
$random .= rand(0, 1) ? rand(0, 9) : chr(rand(ord('a'), ord('z')));
}
return $random;
}
From MySQL manual
A UUID is a 128-bit number represented by a utf8 string of five hexadecimal numbers in aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee format
So, no - you cannot specify the UUID length because it has defined format. The only thing you can do is to substring value returned by UUID
function.
This is not possible.
The alternative is to use PHP to generate your unique number.
The UUID is a standard implementation:
Returns a Universal Unique Identifier (UUID) generated according to “DCE 1.1: Remote Procedure Call” (Appendix A) CAE (Common Applications Environment) Specifications published by The Open Group in October 1997 (Document Number C706, http://www.opengroup.org/public/pubs/catalog/c706.htm).
Maybe you could use some functions like rand
. To create something random. Or use php to generate 8 charachters. Check this topic: Generate random string from 4 to 8 characters in PHP
http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_uuid
How about
SELECT LEFT(UUID(), 8) FROM dual
Adding comment from Cas Wolters:
simply getting the first 8 characters greatly reduces the uniqueness of the string