如何在發生錯誤時抑制此表中的'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