我正在使用 "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/