Search code examples
mysqlrandominsertuniquevarchar

INSERT / UPDATE SQL random & unique VARCHAR


I need to be able to INSERT/UPDATE UNIQUE RANDOM UTF8 ALPHANUMERICAL VARCHAR 55 into a table field called 'key'.

Can't find out any good query example, does anyone can show me or link me something?


Solution

  • This answer is based on mysql.

    This select will create 55 char long random strings:

    select substr(concat(md5(rand()),md5(rand())),1,55);
    

    to fill your table column you might want to try out:

    create table example (keycol varchar(55));
    

    insert into example (keycol) values (substr(concat(md5(rand()),md5(rand())),1,55));

    The result will be:

    select keycol from example;
    
    +---------------------------------------------------------+
    | keycol                                                  |
    +---------------------------------------------------------+
    | 4517f4be669301a4a529b53fc18d646dec42d4d07d911d33a67c863 | 
    | 3caa1c98f0f9ee39515aa6f4ddb3f84fa41abd5392f610c5d24bcd9 | 
    | 8e52cb4ce29e58514671c9b68f19832f26ddf53f277621ac420bd2e | 
    | 3adcccfb6cb729ce1c0a14fb75f6fd54f58992dc0751527c969e007 | 
    | c28c5879589dc90f4fb0963673e5668fa5789d325423ba043e0243b | 
    | 8f7a2af97d73261008f0d0d7249480fde56a3a91f2ce6e8bf0b0070 | 
    | ff4f74f25b92da3eaab282218c23a75d4cfa77c8f8bfdf74d7ebdf9 | 
    +---------------------------------------------------------+