title: Substitute for MySQL's UUID Version 1 function?
body: <p><strong>Context</strong></p>
  
  <p>Web application, PHP 5, MySQL 5.0.91</p>
  
  <p><strong>The Problem</strong></p>
  
  <p>I recently switched from using an auto-incremented integer to a UUID as a primary key for some of my tables. When generating UUID's via MySQL's <a href="http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_uuid" rel="nofollow"><code>UUID()</code></a> function, they are extremely similar to one another:</p>
  
  <pre><code>| uuid                                 |
  ----------------------------------------
  | 1e5988da-afec-11e1-9877-5464f7aa6d24 |
  | 408092aa-afad-11e1-9877-5464f7aa6d24 |
    ^------^   ^^
    1      8   11-12
  </code></pre>
  
  <p>As you can see, only the first 8 characters and the 11th and 12th are different. I understand that UUID Version 1 uses a timestamp and hardware MAC address to generate the UUID. However, I am hesitant in using Version 1 because of these similarities (and the fact that the MAC address will never change, in my case). In addition, if the MAC address never changes, most of the UUID is useless and is wasting space.</p>
  
  <p><strong>My Custom UUID Function</strong></p>
  
  <p>As an experiment, I wrote a custom UUID-generator in PHP:</p>
  
  <pre><code>public static function GenerateUUID()
  {
      return
      substr(sha1(Account::GetUsername() . Account::GetUserID()), 18, 8) . "-" .
      substr(md5(time()), rand() % 28, 4) . "-" . 
      substr(md5(date("Y")), rand() % 28, 4) . "-" . 
      substr(sha1(rand()), 20, 4) . "-" . 
      substr(sha1(rand() % PHP_INT_MAX), 17, 12);
  }
  </code></pre>
  
  <p>A sample of the results:</p>
  
  <pre><code>| uuid                                 |
  ----------------------------------------
  | 574d18c2-5080-bac9-5597-45435f363ea1 |
  | 574d18c2-30d4-8b5b-4ffd-001744d3d287 |
  </code></pre>
  
  <p>Here, the first 8 characters are identical for the same user. This was intended, but not needed.</p>
  
  <p><strong>The Question</strong></p>
  
  <p>Is there a preferred/recommended way to generate a Version 4 or Version 5 UUID <em>within a MySQL query</em>?</p>
  
  <p>If not, is it acceptable to generate a custom UUID within PHP (as above) that does not conform to a specification?</p>
  
  <p><strong>Restrictions</strong></p>
  
  <ul>
  <li>I am using a shared hosting plan with command-line access, but cannot modify the existing MySQL installation.</li>
  <li>I would prefer avoiding third-party packages/libraries.</li>
  </ul>
  
  <p><strong>Notes</strong></p>
  
  <ul>
  <li>I do not and will not be performing merging, synchronization, or other operations that require a GUID that contains the MAC address. That is not the issue here.</li>
  </ul>