如何自动生成4位销钉SQL?
与这篇文章类似的想法: 如何生成 4 位 PIN 码重复号码
但是,有没有办法在创建新用户时自动执行此操作,而不是将它们设置为随机?
我希望能够在我的系统中创建一个新用户,并让他们的 PIN 码自动随机生成 4 位数字。 AUTO_INCRMENT 是唯一的自动选项吗?
我用过这个:
UPDATE顾客SET pin=FLOOR(1000 + (RAND() *8999)) WHERE pin = ?
但这不是我想要的,因为它必须手动完成。我在 Java 程序的 SQL DAO 类中创建了一个方法,在创建用户后执行此操作,因此它确实有效,但如果我要在 Maria DB 中创建一个新用户,它只会自动递增每个值一个值时间。我希望无论我使用数据库的方式如何,随机生成都会发生。
我非常感谢一些帮助!
更新
我不需要密码是唯一的,因为用户还需要他们的图书卡号才能登录系统。
Similar idea to this post:
How do I generate a 4-digit pin no duplicating number
However is there a way to do this automatically when a new user is created rather then setting them to random?
I'd like to be able to create a new user in my system and have their pin number automatically randomize a 4 digit number. Is AUTO_INCREMENT the only automatic option?
I have used this:
UPDATE patrons SET pin=FLOOR(1000 + (RAND() *8999)) WHERE pin = ?
However it's not what I want as it has to be done manually. I have created a method in my SQL DAO class in my Java program to do this after a user is created so it does work, but if I was to create a new user in Maria DB it would just do the auto increment by one value each time. I would prefer if the random generation would happen whichever way I use my database.
I'd greatly appreciate some help!
Update
I do not need the pin to be unique as the user will also need their library card number as well in order to log into the system.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
4 位 PIN 码不应该是唯一的,因为用户应该能够在需要时将其 PIN 码更改为他们想要的任何内容。而且,当用户数量超过 10'000 时,他们不可能是唯一的(根据鸽巢原理)。
因此,此代码:
错误,因为当只有一个用户决定更改 PIN< 时,它会更改共享同一 PIN 的所有用户的 PIN /强>。应更改用户的 PIN,而不是号码。
我想说:随机生成默认 PIN,并提供一些熵,使下一个 PIN 码难以猜测,这是最好的。出于安全原因,自动增量是不好的,而标准随机生成器出于同样的原因也不好:下一个 PIN 值仅比增量更难猜测。这应该与根据用户请求更改 PIN 不同 - 在这种情况下,应使用用户提供的号码。
例如,PostgreSQL 有用于加密强度随机生成的
gen_random_bytes
。我建议在您选择的平台上寻找类似的东西。4-digit PINs are not supposed to be unique, because users should be able change their PINs when they want, to whatever they want. Moreover, when there are more than 10'000 users, they cannot be unique (by pigeonhole principle).
Thus, this code:
is wrong, because it will change the PINs of all users that share the same PIN, when only one user decides to change the PIN. The PIN should be changed for the user, and not for the number.
I would say: random generation of default PIN, with some provided entropy that would make the next PIN hard to guess, is the best. Auto-increment is bad for security reasons, and the standard random generators are bad for the same reason: the next PIN value is only slightly harder to guess than the increment. This should be different than changing PIN by user request — in this case the number provided by the user should be used.
PostgreSQL, for example, has
gen_random_bytes
for cryptographic-strength random generation. I suggest looking for something similar on your platform of choice.火灾:
请参见 live demo
在插入物上创建一个触发 如果使用
使用
rand() * 10000
计算PIN
,请使用格式处理领先的零,例如通过选择
PIN> PIN
直接通过 格式处理。 。Create a trigger the fires on insert:
See live demo of below:
It would be better if
pin
was calculated usingrand() * 10000
and you handled leading zeros with formatting, for example by selectinginstead of selecting
pin
directly.