如何在PostgreSQL中自动增加字母数字值? [英] How to Auto Increment Alpha-Numeric value in postgresql?
本文介绍了如何在PostgreSQL中自动增加字母数字值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 PostgreSQL 9.3.5
我有一个表(<< c $ c> StackOverflowTable )和列 (SoId,SoName,SoDob)
。
我想为列 SoId
的序列生成器
-n值。
我想在PostgreSQL中自动增加字母数字值。
I want to auto increment a Alpha-Numeric Value in postgresql.
For eg : SO10001, SO10002, SO10003.....SO99999.
编辑:
如果明天我需要生成一个序列,该序列可以是 SO1000E100,SO1000E101,...
并具有良好的性能。那么最好的解决方案是什么!
If tomorrow i need to generate a Sequence which can be as SO1000E100, SO1000E101,...
and which has a good performance. Then what is the best solution!
推荐答案
为ID使用序列和默认值:
Use sequences and default value for 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中自动增加字母数字值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文