1

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.

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
m_73
  • 569
  • 2
  • 5
  • 14

5 Answers5

5

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;
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    ... will create a **possibly** unique id. Really it just generating a roughly random string, which in general will be pretty unique - its just not garenteed to be. Calling it a UUID is a misnomer. – barryhunter Aug 12 '14 at 14:40
  • @barryhunter:- Indeed thats why I have written in my answer `which will create the unique ids` or is unique id misleading? – Rahul Tripathi Aug 12 '14 at 14:42
  • Yes, thats misleading. You calling a unique it, when it isnt, is wrong. It MIGHT be unique, but quite possibly wont be. Your function generates a random(ish) string, not a unique id, and certainly not a UUID. – barryhunter Aug 12 '14 at 14:46
2

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.

hsz
  • 148,279
  • 62
  • 259
  • 315
  • Ok. thanke you for your reply, do you know probably, which way exist to generate 8 letter unique string? I tested with so much php functions but they are not correct because of mysql insert if i do it with "while" of 3000 times... – m_73 Aug 12 '14 at 13:10
  • http://stackoverflow.com/questions/5444877/generating-a-unique-random-string-of-a-certain-length-and-restrictions-in-php – hsz Aug 12 '14 at 13:13
  • http://stackoverflow.com/questions/5438760/generate-random-5-characters-string?lq=1 – hsz Aug 12 '14 at 13:13
0

This is not possible.

The alternative is to use PHP to generate your unique number.

Cas Wolters
  • 371
  • 3
  • 11
  • Saying that something *is not possible* is a sentence that should be used with caution on a programers platform :) – Daniel W. Aug 12 '14 at 13:37
  • Agreed :-) But it is not possible with this function, and simply getting the first 8 characters greatly reduces the uniqueness of the string and therefor not a valid solution in my opinion. – Cas Wolters Aug 12 '14 at 13:40
  • I also agree with that and added your comment to my answer! – Daniel W. Aug 12 '14 at 13:47
0

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

Community
  • 1
  • 1
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78
0

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

Daniel W.
  • 31,164
  • 13
  • 93
  • 151