如何在发生错误时抑制此表中的'id'增量?错误增加
db=> CREATE TABLE test (id serial primary key, info text, UNIQUE(info));
NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id"
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index "test_pkey" for table "test"
NOTICE: CREATE TABLE/UNIQUE will create implicit index "test_info_key" for table "test"
CREATE TABLE
db=> INSERT INTO test (info) VALUES ('hello') ;
INSERT 0 1
db=> INSERT INTO test (info) VALUES ('hello') ;
ERROR: duplicate key violates unique constraint "test_info_key"
db=> INSERT INTO test (info) VALUES ('hello') ;
ERROR: duplicate key violates unique constraint "test_info_key"
db=> INSERT INTO test (info) VALUES ('goodbye') ;
INSERT 0 1
db=> SELECT * from test; SELECT last_value from test_id_seq;
id | info
----+---------
1 | hello
4 | goodbye
(2 rows)
last_value
------------
4
(1 row)
你能解释为什么这是你的问题? – intgr 2011-03-30 20:04:31
当我将一个较小的远程数据库转储到一个更大的主数据库中时,我注意到了id序列中的跳转。有几个远程数据库,我需要达到高潮。原来我省略了表格布局中的'id',并仅依靠 – 2011-03-30 21:10:45
...原来我在表布局中省略了'id',并仅依赖于UNIQUE键值。这可防止在再次插入相同行时创建重复行。 – 2011-03-30 21:18:57