agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
unique key problem on update
4+ messages / 2 participants
[nested] [flat]

* unique key problem on update
@ 2013-09-20 16:07 Gary Stainburn <gary.stainburn@ringways.co.uk>
  2013-09-20 16:26 ` Re: unique key problem on update Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Gary Stainburn @ 2013-09-20 16:07 UTC (permalink / raw)
  To: pgsql-sql

Hi folks.

I've got the table and data shown below. 

I want to add a new page after page 2 so I try to increase the sequence number 
of each row from page 3 onwards to make space in the sequence for the new 
record. However, I get duplicate key errors when I try. Can anyone suggest 
how I get round this.

Also, the final version will be put onto a WordPress web site which means I 
will have to port it to MYSQL which I don't know, so any solution that will 
work with both systems would be a great help.

Ta

Gary


stainburn=# \d skills_pages
                                    Table "public.skills_pages"
   Column    |         Type          |                          Modifiers                           
-------------+-----------------------+--------------------------------------------------------------
 sp_id       | integer               | not null default 
nextval('skills_pages_sp_id_seq'::regclass)
 sp_sequence | integer               | not null
 sp_title    | character varying(80) | 
 sp_narative | text                  | 
Indexes:
    "skills_pages_pkey" PRIMARY KEY, btree (sp_id)
    "skills_pages_sequence" UNIQUE, btree (sp_sequence)

stainburn=# select * from skills_pages;
 sp_id | sp_sequence |     sp_title     | sp_narative 
-------+-------------+------------------+-------------
     1 |          10 | Departments      | 
     2 |          20 | Interest Groups  | 
     3 |          30 | Customer Focused | 
     4 |          40 | Business Roles   | 
     5 |          50 | Commercial       | 
     6 |          60 | People Oriented  | 
     7 |          70 | Engineering      | 
(7 rows)

stainburn=# update skills_pages set sp_sequence=sp_sequence+10 where 
sp_sequence >= 30;
ERROR:  duplicate key value violates unique constraint "skills_pages_sequence"
stainburn=# 


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: unique key problem on update
  2013-09-20 16:07 unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2013-09-20 16:26 ` Thomas Kellerer <spam_eater@gmx.net>
  2013-09-20 16:30   ` Re: unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
  0 siblings, 1 reply; 4+ messages in thread

From: Thomas Kellerer @ 2013-09-20 16:26 UTC (permalink / raw)
  To: pgsql-sql

Gary Stainburn wrote on 20.09.2013 18:07:
> I want to add a new page after page 2 so I try to increase the sequence number
> of each row from page 3 onwards to make space in the sequence for the new
> record. However, I get duplicate key errors when I try. Can anyone suggest
> how I get round this.
>
> Also, the final version will be put onto a WordPress web site which means I
> will have to port it to MYSQL which I don't know, so any solution that will
> work with both systems would be a great help.
>

You need to define the primary key as deferrable:

create table skills_pages
(
  sp_id        serial not null,
  sp_sequence  integer not null,
  sp_title     character varying(80),
  sp_narative  text,
  primary key (sp_id) deferrable
);





-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: unique key problem on update
  2013-09-20 16:07 unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
  2013-09-20 16:26 ` Re: unique key problem on update Thomas Kellerer <spam_eater@gmx.net>
@ 2013-09-20 16:30   ` Gary Stainburn <gary.stainburn@ringways.co.uk>
  2013-09-20 16:42     ` Re: unique key problem on update Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Gary Stainburn @ 2013-09-20 16:30 UTC (permalink / raw)
  To: pgsql-sql; +Cc: Thomas Kellerer <spam_eater@gmx.net>

On Friday 20 September 2013 17:26:58 Thomas Kellerer wrote:
> You need to define the primary key as deferrable:
>
> create table skills_pages
> (
>   sp_id        serial not null,
>   sp_sequence  integer not null,
>   sp_title     character varying(80),
>   sp_narative  text,
>   primary key (sp_id) deferrable
> );

Cheers. I'll look at that. It's actually the second unique index that's the 
problem but I'm guessing I can set that index up as deferrable too.

Hopefully it'll work for mysql too.

-- 
Gary Stainburn
Group I.T. Manager
Ringways Garages
http://www.ringways.co.uk 


-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: unique key problem on update
  2013-09-20 16:07 unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
  2013-09-20 16:26 ` Re: unique key problem on update Thomas Kellerer <spam_eater@gmx.net>
  2013-09-20 16:30   ` Re: unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2013-09-20 16:42     ` Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 4+ messages in thread

From: Thomas Kellerer @ 2013-09-20 16:42 UTC (permalink / raw)
  To: pgsql-sql

Gary Stainburn wrote on 20.09.2013 18:30:
>> You need to define the primary key as deferrable:
>>
>> create table skills_pages
>> (
>>    sp_id        serial not null,
>>    sp_sequence  integer not null,
>>    sp_title     character varying(80),
>>    sp_narative  text,
>>    primary key (sp_id) deferrable
>> );
>
> Cheers. I'll look at that. It's actually the second unique index that's the
> problem but I'm guessing I can set that index up as deferrable too.

Ah, sorry didn't see that ;) but, yes it works the same way:

create table skills_pages
(
   sp_id        serial not null,
   sp_sequence  integer not null,
   sp_title     character varying(80),
   sp_narative  text,
   primary key (sp_id),
   unique (sp_sequence) deferrable
);

  
> Hopefully it'll work for mysql too.
No, it won't.

MySQL neither has deferrable constraints nor does it evaluate them on statement level (they are *always* evaluated row-by-row).






-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2013-09-20 16:42 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-09-20 16:07 unique key problem on update Gary Stainburn <gary.stainburn@ringways.co.uk>
2013-09-20 16:26 ` Thomas Kellerer <spam_eater@gmx.net>
2013-09-20 16:30   ` Gary Stainburn <gary.stainburn@ringways.co.uk>
2013-09-20 16:42     ` Thomas Kellerer <spam_eater@gmx.net>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox