MySQL generated UID

16,362

Solution 1

If you don't want globally unique IDs, just a unique ID (a different number each time you call it), then you can create a table with only an integer auto_increment non-null column and issue the following two queries:

INSERT INTO tbl1 SET col1 = NULL;
SELECT LAST_INSERT_ID();

To start the number at something other than 1, for example, to start at 10 digits, set it like this:

ALTER TABLE tbl1 AUTO_INCREMENT = 1000000000

Solution 2

MySQL has a function for that. Try:

SELECT UUID();
Share:
16,362
André Figueira
Author by

André Figueira

I'm an experienced web application developer. I've worked for a large variety of clients some of which are quite high profile including Time Inc, Allianz and a few others. I currently work in London as a Senior Systems Engineer

Updated on June 04, 2022

Comments

  • André Figueira
    André Figueira about 2 years

    I am coding a messaging system and I don't want to have short IDs shown, is there any way that MySQL can generate a UID which is unique as UID suggests?

    I need to know how I can do this with ONLY MySQL, no PHP, JavaScript or anything else, just MySQL, if not possible, fine, I just want to make sure.

    Sorry, I should have made it more clear when I say UID I just mean "Unique ID" but I need it in a multidigit number only, e.g. 914888629, 3890692140

  • André Figueira
    André Figueira over 12 years
    Sorry I should have made it more clear, when I say UID I just mean Unique ID but I need it in a multidigit number only, e.g. 914888629, 3890692140
  • Devart
    Devart over 12 years
    Try to initialize auto_increment with multidigit number - ALTER TABLE table_name AUTO_INCREMENT = 100000000
  • André Figueira
    André Figueira over 12 years
    Thanks!god something that was right in front of me didn't even think of it.