How to make unique random alphanumeric sequence in SQL Server

前端 未结 4 346
情话喂你
情话喂你 2021-01-16 20:21

I want to make unique random alphanumeric sequence to be the primary key for a database table.

Each char in the sequence is either a letter (a-z) or number (0-9)

4条回答
  •  借酒劲吻你
    2021-01-16 21:08

    It is a very bad idea to use random strings as a primary key.
    It will effect performance as well as storage size, and you will be much better of using an int or a bigint with an identity property.

    However, generating a random string in SQL maybe useful for other things, and this is why I offer this solution:

    Create a table to hold permitted char values.
    In my example the permitted chars are 0-9 and A-Z.

    CREATE TABLE Chars (C char(1))
    
    DECLARE @i as int = 0
    WHILE @i < 10
    BEGIN
      INSERT INTO Chars (C) VALUES (CAST(@i as Char(1)))
      SET @i = @i+1
    END
    
    SET @i = 65
    WHILE @i < 91
    BEGIN
      INSERT INTO Chars (C) VALUES (CHAR(@i))
      SET @i = @i+1
    END
    

    Then use this simple select statement to generate a random string from this table:

    SELECT TOP 10 C AS [text()]
    FROM Chars
    ORDER BY NEWID()
    FOR XML PATH('')
    

    The advantages:

    • You can easily control the allowed characters.
    • The generation of a new string is a simple select statement and not manipulation on strings.

    The disadvantages:

    • This select results with an ugly name (i.e XML_F52E2B61-18A1-11d1-B105-00805F49916B). This is easily solved by setting the result into a local variable.

    • Characters will only appear once in every string. This can easily be solved by adding union:

    example:

    SELECT TOP 10 C AS [text()]
      FROM (
        SELECT * FROM Chars
        UNION ALL SELECT * FROM Chars
      ) InnerSelect
      ORDER BY NEWID()
      FOR XML PATH('')
    

    Another option is to use STUFF function instead of As [Text()] to eliminate those pesky XML tags:

    SELECT STUFF((
     SELECT TOP 100 ''+ C 
     FROM Chars
     ORDER BY NEWID()
     FOR XML PATH('')
    ), 1, 1, '') As RandomString;
    

    This option doesn't have the disadvantage of the ugly column name, and can have an alias directly. Execution plan is a little different but it should not suffer a lot of performance lose.

    Play with it yourself in this Sql Fiddle

    If there are any more advantages / disadvantages you think of please leave a comment. Thanks.

提交回复
热议问题