How to to create unique random integer ID for primary key for table?

后端 未结 8 2141
庸人自扰
庸人自扰 2020-12-30 08:50

I was wondering if anybody knew a good way to create a unique random integer id for a primary key for a table. I\'m using MySQL. The value has to be integer.

8条回答
  •  忘掉有多难
    2020-12-30 09:24

    If your're open to suggestions and you can implement it, use UUIDs. MySQL's UUID() function will return a 36 chars value which can be used for ID.

    If you want to use integer, still, I think you need to create a function getRandID() that you will use in the INSERT statement. This function needs to use random + check of existing ids to return one that is not used before.

    Check RAND() function for MySQL.

提交回复
热议问题