X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 7D7FBD930B for ; Wed, 28 Sep 2005 18:29:12 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15414-05 for ; Wed, 28 Sep 2005 21:29:07 +0000 (GMT) Received: from smtp-send.myrealbox.com (smtp-send.myrealbox.com [192.108.102.143]) by svr1.postgresql.org (Postfix) with ESMTP id 3ECA8D6E02 for ; Wed, 28 Sep 2005 18:29:05 -0300 (ADT) Received: from [172.16.1.188] grzm [61.197.227.146] by smtp-send.myrealbox.com with NetMail SMTP Agent $Revision: 1.6 $ on Linux via secured & encrypted transport (TLS); Wed, 28 Sep 2005 15:28:04 -0600 In-Reply-To: <433AF11D.801@brandywine.com> References: <4339CD1B.10404@verizon.net> <433AF11D.801@brandywine.com> Mime-Version: 1.0 (Apple Message framework v734) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <211B0819-5AB1-4F45-BC8D-753457956688@myrealbox.com> Cc: fmiddleton@verizon.net, pgsql-sql@postgresql.org Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: changing a column's position in table, how do you do that Date: Thu, 29 Sep 2005 06:28:01 +0900 To: daryl@brandywine.com X-Mailer: Apple Mail (2.734) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.023 required=5 tests=[AWL=0.023] X-Spam-Level: X-Archive-Number: 200509/286 X-Sequence-Number: 22833 On Sep 29, 2005, at 4:38 , Daryl Richter wrote: > Ferindo Middleton Jr wrote: > >> Is there a way to change the position attribute of a column in a >> table? I have data that I need to import into various tables in my >> db on a consistent basis... I usually us e the COPY ... FROM query >> but I can't control the -order- of the fields my client dumps the >> data so I would like to be able to change the position the columns >> in my table to be able to better align the data to be imported >> with the format of my table. I was thinking I could do something >> like "ALTER TABLE ALTER COLUMN ... " or something like that to >> change the columns position in the table but I can't figure out how. >> Ferindo >> ---------------------------(end of >> broadcast)--------------------------- >> TIP 6: explain analyze is your friend >> > > Constantly changing the column order of a table doesn't make sense > (and is a pain!). > > I would suggest setting up a temporary staging table with generic > column names to take in the input data and then using SQL to move > it into its permanent location. Or specify the columns directly in the copy statement. Instead of copy foo from , do copy foo (bar,baz,bat) from . Then you only have to change the copy statement. While SQL does allow columns to be referenced by column order, it's far, far better to consider the columns of a table to be unordered and (nearly) always specify your columns explicitly. It gives you much more flexibility. Changing a few SQL statements to return (or insert) the columns how you want them is much easier than changing the underlying table structure and having to make sure *all* of your queries then fit the new table structure. Michael Glaesemann grzm myrealbox com