Eri*_*ler 4 sql postgresql sql-update
我想更新表的几行中的主键。如果所有行都被更新,键将再次是唯一的,但是第一行的更新会导致与第二行的键暂时冲突。有没有一种优雅的方法来解决这个问题?
例子:
create table erichtest ( i integer, v varchar(200) );
alter table erichtest add constraint pk_erichtest primary key(i);
insert into erichtest values(1, 'Eins');
insert into erichtest values(2, 'Zwei');
update erichtest set i=i+1;
Run Code Online (Sandbox Code Playgroud)
错误:重复的键值违反了唯一约束“pk_erichtest”
b=# begin;
BEGIN
b=# alter table erichtest drop constraint pk_erichtest ;
ALTER TABLE
b=# alter table erichtest add constraint pk_erichtest primary key (i) DEFERRABLE INITIALLY IMMEDIATE;
ALTER TABLE
b=# set constraints pk_erichtest deferred ;
SET CONSTRAINTS
b=# update erichtest set i=i+1;
UPDATE 2
b=# select * from erichtest ;
i | v
---+------
2 | Eins
3 | Zwei
(2 rows)
b=# end;
COMMIT
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2643 次 |
最近记录: |