database - Increment FK column value of all rows SQL -


as title says, want increment column value of rows of table in sql.

suppose have these data in tab_a , pk are: fk_a , fk_b , fk_c:

|fk_a|fk_b|fk_c| |1   |2   |2   | |1   |2   |3   | 

if following update statement:

update tab_a set fk_c = fk_c + 1 

my query complain saying data (1,2,3) exists, should not complain because update every row, hence row (1,2,3) should become (1,2,4).

how accomplish this?

the primary key should deferrable deferred:

create table tab_a(     fk_a int,      fk_b int,      fk_c int,      primary key (fk_a, fk_b, fk_c) deferrable deferred     );  insert tab_a values  (1, 2, 3), (1, 2, 4);  update tab_a set fk_c = fk_c + 1 returning *;   fk_a | fk_b | fk_c  ------+------+------     1 |    2 |    4     1 |    2 |    5 (2 rows) 

Comments