Unique, unpredictable, 12 digit, integer id

前端 未结 7 1091
被撕碎了的回忆
被撕碎了的回忆 2021-01-13 06:56

How would I go about generating this... I want to keep my primary key sequential and have a 12 digit unique pin generated for each new object added to the database.

7条回答
  •  囚心锁ツ
    2021-01-13 07:18

    Maybe you can use UUID_SHORT(). Not 12 digits long, but still could be a viable option:

    mysql> select uuid_short();
    +-------------------+
    | uuid_short()      |
    +-------------------+
    | 22048742962102272 |
    +-------------------+
    

    So:

    INSERT INTO `table` (`id`, `text`) VALUES (UUID_SHORT(), 'hello world!');
    

    Note: If you really want to have exactly 12 digits, then don't even try to substring the result, if would not ensure the uniqueness of the identifier and may cause collisions.

提交回复
热议问题