如何自动生成4位销钉SQL?

发布于 2025-01-18 10:08:00 字数 587 浏览 2 评论 0原文

与这篇文章类似的想法: 如何生成 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

¢蛋碎的人ぎ生 2025-01-25 10:08:00

4 位 PIN 码不应该是唯一的,因为用户应该能够在需要时将其 PIN 码更改为他们想要的任何内容。而且,当用户数量超过 10'000 时,他们不可能是唯一的(根据鸽巢原理)。

因此,此代码:

UPDATE patrons SET pin=FLOOR(1000 + (RAND() *8999)) WHERE pin = ?

错误,因为当只有一个用户决定更改 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:

UPDATE patrons SET pin=FLOOR(1000 + (RAND() *8999)) WHERE pin = ?

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.

夏天碎花小短裙 2025-01-25 10:08:00

火灾:

create trigger client_pin
before insert on client
for each row set new.pin = 1000 + rand() * 9000;

请参见 live demo

create table client (
  id int auto_increment primary key,
  name text not null,
  pin int(4)
);

create trigger client_pin
before insert on client
for each row set new.pin = 1000 + rand() * 9000;

insert into client (name) values ('Adam');
insert into client (name) values ('Betty');

在插入物上创建一个触发 如果使用使用rand() * 10000计算PIN,请使用格式处理领先的零,例如通过

lpad(pin, 4, '0') as pin

选择PIN> PIN直接通过 格式处理。 。

Create a trigger the fires on insert:

create trigger client_pin
before insert on client
for each row set new.pin = 1000 + rand() * 9000;

See live demo of below:

create table client (
  id int auto_increment primary key,
  name text not null,
  pin int(4)
);

create trigger client_pin
before insert on client
for each row set new.pin = 1000 + rand() * 9000;

insert into client (name) values ('Adam');
insert into client (name) values ('Betty');

It would be better if pin was calculated using rand() * 10000 and you handled leading zeros with formatting, for example by selecting

lpad(pin, 4, '0') as pin

instead of selecting pin directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文