在PostgreSQL 8.3中增加非空和唯一约束的字段

时间:2010-10-23 20:41:16

标签: sql postgresql sql-update unique-constraint

我有一个带有“位置”列的表“items”。 position具有唯一且非null的约束。为了在位置x I处插入新行,首先尝试增加后续项目的位置:

UPDATE items SET position = position + 1 WHERE position >= x;

这会导致唯一的约束违规:

ERROR:  duplicate key value violates unique constraint

问题似乎是PostgreSQL执行更新的顺序。 PostgreSQL中的唯一约束< 9.0不可延迟,遗憾的是使用9.0目前不是一种选择。此外,UPDATE语句不支持ORDER BY子句,并且以下也不起作用(仍然重复键冲突):

UPDATE items SET position = position + 1 WHERE id IN (
  SELECT id FROM items WHERE position >= x ORDER BY position DESC)

有人知道一个不涉及迭代代码中所有项目的解决方案吗?

4 个答案:

答案 0 :(得分:3)

另一个表,具有多个唯一索引:

create table utest(id integer, position integer not null, unique(id, position));
test=# \d utest
      Table "public.utest"
  Column  |  Type   | Modifiers 
----------+---------+-----------
 id       | integer | 
 position | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id, "position")

一些数据:

insert into utest(id, position) select generate_series(1,3), 1;
insert into utest(id, position) select generate_series(1,3), 2;
insert into utest(id, position) select generate_series(1,3), 3;

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        2
  1 |        3
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

我创建了一个以正确顺序更新位置值的过程:

create or replace function update_positions(i integer, p integer) 
  returns void as $$
declare
  temprec record;
begin
  for temprec in 
    select * 
      from utest u 
      where id = i and position >= p 
      order by position desc 
  loop
    raise notice 'Id = [%], Moving % to %', 
      i, 
      temprec.position, 
      temprec.position+1;

    update utest 
      set position = position+1 
      where position=temprec.position and id = i;
  end loop;
end;
$$ language plpgsql;

一些测试:

test=# select * from update_positions(1, 2);
NOTICE:  Id = [1], Moving 3 to 4
NOTICE:  Id = [1], Moving 2 to 3
 update_positions 
------------------

(1 row)

test=# select * from utest order by id, position;
 id | position 
----+----------
  1 |        1
  1 |        3
  1 |        4
  2 |        1
  2 |        2
  2 |        3
  3 |        1
  3 |        2
  3 |        3
(9 rows)

希望它有所帮助。

答案 1 :(得分:2)


由于PostgreSQL支持全套事务DDL,因此您可以轻松地执行以下操作:

create table utest(id integer unique not null);
insert into utest(id) select generate_series(1,4);

该表现在看起来像这样:

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  1
  2
  3
  4
(4 rows)

现在整个魔术:

begin;
alter table utest drop constraint utest_id_key;
update utest set id = id + 1;
alter table utest add constraint utest_id_key unique(id);
commit;

之后我们有:

test=# \d utest
     Table "public.utest"
 Column |  Type   | Modifiers 
--------+---------+-----------
 id     | integer | not null
Indexes:
    "utest_id_key" UNIQUE, btree (id)

test=# select * from utest;
 id 
----
  2
  3
  4
  5
(4 rows)

这个解决方案有一个缺点:它需要锁定整个表,但这可能不是问题。

答案 2 :(得分:0)

'纠正者'解决方案可能是制定约束DEFERRABLE

ALTER TABLE channels ADD CONSTRAINT 
channels_position_unique unique("position") 
DEFERRABLE INITIALLY IMMEDIATE

然后在完成后将该约束设置为DEFERRED并将其设置回IMMEDIATE。

SET CONSTRAINTS channels_position_unique DEFERRED;
UPDATE channels SET position = position+1 
WHERE position BETWEEN 1 AND 10;
SET CONSTRAINTS channels_position_unique IMMEDIATE;

答案 3 :(得分:0)

Variant without altering table and drop constraint:

UPDATE items t1 
SET    position = t2.position + 1 
FROM   (SELECT position
    FROM   items 
    ORDER  BY position DESC) t2 
WHERE t2.position >= x AND t1.position = t2.position

Online example: http://rextester.com/FAU54991