如何在Spring Boot / Oracle中自动插入字符

发布于 2025-02-12 13:40:28 字数 325 浏览 0 评论 0原文

我想为特定产品生成代码“ AAA”。下一个条目将是“ AAB”。 并继续直到“ aaz”。一旦“ aaz”在新生成的密钥上,就会是“ aba”,依此类推。

AAA         ABA
AAB         ABB
AAC         ABC
AAD         ABD
AAE         ABE
.           .
.           .
.           .
AAZ        ABZ

上表是我需要的示例。我该如何在Spring Boot中进行? 我正在使用Spring-Boot作为后端技术,前端的角度和数据库的Oracle。

I would like to generate a code "AAA" for a specific product. And the next entry would be "AAB".
And goes on till "AAZ". Once "AAZ" is over the newly generated key would be "ABA" and so on.

AAA         ABA
AAB         ABB
AAC         ABC
AAD         ABD
AAE         ABE
.           .
.           .
.           .
AAZ        ABZ

The above table is an example that I need. How can I do it in spring boot?
I'm using spring-boot as backend technology, Angular for the frontend, and oracle for the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

開玄 2025-02-19 13:40:28

在Oracle中,从版本12中,使用Identity列生成顺序数字,然后使用虚拟列从数字创建字符序列:

CREATE TABLE table_name (
  id     NUMBER(5,0)
         GENERATED ALWAYS AS IDENTITY (START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 17575)
         PRIMARY KEY,
  str_id CHAR(3)
         GENERATED ALWAYS AS (
           CAST(
             CHR(65+TRUNC(id/POWER(26, 2)))
             || CHR(65+MOD(TRUNC(id/POWER(26, 1)), 26))
             || CHR(65+MOD(id, 26))
             AS CHAR(3)
           )
         )
)

但是,您可能会更好地使用基础数字列,然后在要 display 值时在前端中生成字符串值。

db<> fiddle

In Oracle, from version 12, use an IDENTITY column to generate sequential numbers and then use a virtual column to create the character sequence from the number:

CREATE TABLE table_name (
  id     NUMBER(5,0)
         GENERATED ALWAYS AS IDENTITY (START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 17575)
         PRIMARY KEY,
  str_id CHAR(3)
         GENERATED ALWAYS AS (
           CAST(
             CHR(65+TRUNC(id/POWER(26, 2)))
             || CHR(65+MOD(TRUNC(id/POWER(26, 1)), 26))
             || CHR(65+MOD(id, 26))
             AS CHAR(3)
           )
         )
)

However, you would probably be better to just use the underlying numeric column and then generate the string value in the front-end when you want to display the value.

db<>fiddle here

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