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

标签 postgresql postgresql-9.2 postgresql-9.3

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

相关文章:

r - 查询表并生成国家代码分组和绘图条形图的代码

postgresql - 无法使用 Yii 连接 PostgreSQL

sql - 如何在postgres中加入空值填充行

postgresql - 如何在 PostgreSQL 中使用 LIKE 语句在 WHERE 中使用 CAST 值?

python - Django 1.5 : OperationalError in windows when running "python manage.py syncdb" using postgresql-psycopg2

java - 将 java.sql.Connection 转换为 PGConnection

postgresql - Postgres 文本列只允许/转换为小写并且没有特殊字符,例如 : Ñöáè

sql - 如何更好的优化SQL查询?

Postgresql - 由于某些自动连接到数据库而无法删除数据库

postgresql - 刷新物化 View : Concurrency, 事务行为