如何为表的主键创建唯一的随机整数ID? [英] How to to create unique random integer ID for primary key for table?

查看:77
本文介绍了如何为表的主键创建唯一的随机整数ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道一种为表的主键创建唯一的随机整数id的好方法.我正在使用MySQL.该值必须是整数.

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.

推荐答案

如果您愿意接受建议并且可以实施,请使用UUID. MySQL的UUID()函数将返回一个36个字符的值,该值可用于ID.

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.

如果您仍然想使用整数,我想您需要创建一个将在INSERT语句中使用的函数getRandID().此功能需要对现有ID进行随机+检查,以返回之前未使用过的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.

检查MySQL的RAND()功能.

Check RAND() function for MySQL.

这篇关于如何为表的主键创建唯一的随机整数ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆