pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stuart McGraw <smcg2297@frii.com>
To: pgsql-sql@postgresql.org
Subject: Re: changing multiple pk's in one update
Date: Mon, 13 Apr 2009 11:20:01 -0600
Message-ID: <49E37441.6030301@frii.com> (raw)
In-Reply-To: <dcc563d10904101120y49f00977nc74132e982518444@mail.gmail.com>
References: <49DC1034.1060705@frii.com>
	<dcc563d10904101120y49f00977nc74132e982518444@mail.gmail.com>

Scott Marlowe wrote:
> 2009/4/7 Stuart McGraw <smcg2297@frii.com>:
>> Hello all,
>>
>> I have a table with a primary key column
>> that contains sequential numbers.
>>
>> Sometimes I need to shift them all up or down
>> by a fixed amount.  For example, if I have
>> four rows with primary keys, 2, 3, 4, 5, I
>> might want to shift them down by 1 by doing:
>>
> 
> Generally speaking, when you need to do this more than once or twice
> in the lifetime of your data, there's something wrong with your data
> model.

Generally speaking, I agree.  But in this case I am not sure
how to do it any better.  Simplified, the database models 
a dictionary.  An entry in the dictionary can have a number
senses where each sense is a sentence that gives the meaning 
of the sense.  The order of the senses is significant.  The 
sense table has three columns, an fk to the dictionary entry 
(id number) it is part of, a sense number (small number from
1 to the number of sentences in the entry) and the sentence 
text.  The pk is of course the entry id and the sense number.
There are other tables that have fk's to the senses.

I could dispense with the sentence number and use the 
sentence text as the second part of the composite key but 
the sentence text is far more volatile than the number, and 
can be very long making it quite unwieldy to use as a key.

I guess I could add an "order"[1] column and use the sense number 
as a surrogate partial key to avoid the need for key renumbering,
but all the api's (and the normal human way of thinking) are based
on "sense number 1 of entry x", "sense number 2 of entry y", so
one would need to maintain "order" as a gapless sequence (or add 
a new mapping layer to map from/to a arbitrary monotonic sequence
to a 1,2,3,... sequence) -- the gain doesn't seem that big.

Or maybe there is some other approach?

[1] yes, I know "order" is a reserved word.
 





view thread (15+ messages)  latest in thread

Message-ID: <49E37441.6030301@frii.com>
Permalink:  ../49E37441.6030301@frii.com/
Also on:    postgresql.org/message-id/49E37441.6030301@frii.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: smcg2297@frii.com
  Subject: Re: changing multiple pk's in one update
  In-Reply-To: <49E37441.6030301@frii.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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