Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 9EE9F2E0043 for ; Tue, 22 Apr 2008 08:09:52 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 68762-04 for ; Tue, 22 Apr 2008 08:09:39 -0300 (ADT) X-Greylist: delayed 00:06:40.898163 by SQLgrey-1.7.5 Received: from europa.inventum.cc (unknown [74.205.124.113]) by developer.postgresql.org (Postfix) with ESMTP id B56DA2E0044 for ; Tue, 22 Apr 2008 08:09:44 -0300 (ADT) Received: (qmail 15995 invoked from network); 22 Apr 2008 06:03:01 -0500 Received: from abts-ncr-dynamic-054.25.161.122.airtelbroadband.in (HELO ?127.0.0.1?) (122.161.25.54) by padamj.com with SMTP; 22 Apr 2008 06:03:00 -0500 Message-ID: <480DC5E1.5060508@inventum.cc> Date: Tue, 22 Apr 2008 16:32:57 +0530 From: Padam J Singh Organization: Inventum Technologies Pvt Ltd User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Nacef LABIDI CC: depesz@depesz.com, Roland Voegtli , pgsql-sql@postgresql.org Subject: Re: After updating dataset the record goes to the end of the dataset References: <3B256469-4A00-44C3-8EB8-F49FA9F7259A@scnat.ch> <20080422101523.GB17162@depesz.com> In-Reply-To: X-Enigmail-Version: 0.95.6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 080421-1, 04/21/2008), Outbound message X-Antivirus-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.773 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001, MIME_HTML_ONLY=1.672, RDNS_NONE=0.1 X-Spam-Level: * X-Archive-Number: 200804/136 X-Sequence-Number: 30675 Nacef,

Simply add a SERIAL column to your table. When you add a new row, a new value will be stored in this column if you do not specify the column name in the insert query.
In your select query, always order by this column.
When doing an update , do not update this column. When re-ordering, update this column to indicate order. So if you want to move a row up, issue two update queries, updating this column. Make sure you have some primary key in this table to uniquely identify these row.

Never rely on the internal implementation of a database to keep the data in the order you insert/update.

Padam

Nacef LABIDI wrote:
You are right about the fact that the rows don't keep the order they where created with at the start. I have verfied this.

I will explain more my case :

I am writing an application where here is some resources to plan events on. I want to provide the user with the ability to customize the order in which resources are displayed on the screen. So there is a mapping between the rows positions in the dataset and they display positions. After updating one of these resources the row jumps to the end of the dataset so does the resource on the display. And the order gets scrambled. I cannot set this order in the databe since it is customizable for each user.

When I have tested this with SQLServer it works well, since the rows doesn't change position on the DB.

I hope that you understand my issue and I will provide any explanations if someting isn't clear enough.

Thanks to all
Nacef

On Tue, Apr 22, 2008 at 12:15 PM, hubert depesz lubaczewski <depesz@depesz.com> wrote:
On Tue, Apr 22, 2008 at 12:10:41PM +0200, Nacef LABIDI wrote:
> Yes I don't issue any sort statement, and I indeed want the data to be show
> as it is stored in the database. But after updating a row (I don't update
> the ID, just some fields), it keeps its same place on the DB but jumps to
> the end of the dataset and by the way to the end of the DBGrid.

why do you think it stays in the same place in db?

besides - without "order by" you cannot depend on the order of rows.
basically i treat them as in "random" order (which is not true, but
helps me remember to never count on the "default" ordering.

depesz

--
quicksil1er: "postgres is excellent, but like any DB it requires a
highly paid DBA.  here's my CV!" :)
http://www.depesz.com/ - blog dla ciebie (i moje CV)


-- 
PGP Id 9EED2E09