Update PK Violation - Mailing list pgsql-sql

From Franklin Haut
Subject Update PK Violation
Date
Msg-id 478D1FB5.2070003@gmail.com
Whole thread Raw
Responses Re: Update PK Violation  ("Scott Marlowe" <scott.marlowe@gmail.com>)
Re: Update PK Violation  ("Fernando Hevia" <fhevia@ip-tel.com.ar>)
Re: Update PK Violation  (Achilleas Mantzios <achill@matrix.gatewaynet.com>)
List pgsql-sql
Hi all,

i have a problem with one update sentence sql.

example to produce:

create table temp (num integer primary key, name varchar(20));

insert into temp values (1, 'THE');
insert into temp values (2, 'BOOK');
insert into temp values (3, 'IS');
insert into temp values (4, 'ON');
insert into temp values (5, 'THE');
insert into temp values (6, 'RED');
insert into temp values (7, 'TABLE');

-- now i need insert new row at position 4, for this i need increase the 
field 'num' 4 to 5, 5 to 6, 6 to 7 and 7 to 8
update temp set num = num + 1  where num > 5;
--  but this give an key violation error, because the postgresql try 
change row 4 to 5 and the 5 exist.

-- to contornate the situation i have make
update temp set num = 8 where num = 7;
update temp set num = 7 where num = 6;
update temp set num = 6 where num = 5;
update temp set num = 5 where num = 4;

-- so then i can insert the row...
insert into temp values (5, 'NOT');

-- and obtain the result what i need.
select num, name from temp
---result ------  1, 'THE'  2, 'BOOK'  3, 'IS'  4, 'NOT'  5, 'ON'  6, 'THE'  7, 'RED'  8, 'TABLE'


--- the big question is...  have one way to the command  (update temp 
set num = num + 1  where num > 5;) works ?
-- consideration, i can´t delete the primary key
-- using PG 8.2 / Windows

Thanks for all


Franklin


pgsql-sql by date:

Previous
From: "Joost Kraaijeveld"
Date:
Subject: Re: Is DATETIME an ANSI-SQL type?
Next
From: "Scott Marlowe"
Date:
Subject: Re: Update PK Violation