gpt4 book ai didi

postgresql - 如何在 postgresql 中自动增加字母数字值?

转载 作者:行者123 更新时间:2023-11-29 11:19:39 24 4
gpt4 key购买 nike

我正在使用 "PostgreSQL 9.3.5"

我有一个包含 StackOverflowTable( (SoId,SoName,SoDob) ) .

我想要一个 Sequence generator对于列 SoId这是一个字母数字值。

我想在 postgresql 中自动增加一个字母数字值。

For eg : SO10001, SO10002, SO10003.....SO99999.

编辑:

如果明天我需要生成一个序列,可以是 SO1000E100, SO1000E101,...并具有良好的性能。那么最好的解决方案是什么!

最佳答案

使用序列和 id 的默认值:

postgres=# CREATE SEQUENCE xxx;
CREATE SEQUENCE
postgres=# SELECT setval('xxx', 10000);
setval
--------
10000
(1 row)

postgres=# CREATE TABLE foo(id text PRIMARY KEY
CHECK (id ~ '^SO[0-9]+$' )
DEFAULT 'SO' || nextval('xxx'),
b integer);
CREATE TABLE
postgres=# insert into foo(b) values(10);
INSERT 0 1
postgres=# insert into foo(b) values(20);
INSERT 0 1
postgres=# SELECT * FROM foo;
id | b
---------+----
SO10001 | 10
SO10002 | 20
(2 rows)

关于postgresql - 如何在 postgresql 中自动增加字母数字值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27121196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com