m_73
m_73

Reputation: 569

It's possible to set length on "UUID()"?

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.

Upvotes: 1

Views: 12810

Answers (5)

Daniel W.
Daniel W.

Reputation: 32290

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

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172448

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;
}

Upvotes: 5

Roel Veldhuizen
Roel Veldhuizen

Reputation: 4723

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

Upvotes: 0

Cas Wolters
Cas Wolters

Reputation: 371

This is not possible.

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

Upvotes: 0

hsz
hsz

Reputation: 152216

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.

Upvotes: 2

Related Questions