我有一个订单表,我想为用户提供一个唯一的订单代码,同时隐藏递增的身份整数主键,因为我不想透露已下订单的数量.
I have a table of orders and I want to give users a unique code for an order whilst hiding the incrementing identity integer primary key because I don't want to give away how many orders have been made.
确保代码唯一的一种简单方法是使用主键来确定代码.
One easy way of making sure the codes are unique is to use the primary key to determine the code.
那么我怎样才能将一个整数转换成一个友好的,比如说,八位字母数字代码,这样每个代码都是唯一的?
So how can I transform an integer into a friendly, say, eight alpha numeric code such that every code is unique?
推荐答案假设创建的订单总数不会接近池中标识符的总数,一个相当有效的技术是简单地生成一个随机标识符,看看它是否已经被使用;继续生成新的标识符,直到找到以前没有使用过的标识符.
Assuming the total number of orders being created isn't going to get anywhere near the total number of identifiers in your pool, a reasonably effective technique is to simply generate a random identifier and see if it is used already; continue generating new identifiers until you find one not previously used.
更多推荐
从主键生成唯一代码
发布评论