Unix Technical Forum

numbering rows on import from file

This is a discussion on numbering rows on import from file within the pgsql Sql forums, part of the PostgreSQL category; --> Greetings -- I have a huge table of the form (integer,integer,smallint,date). Its origin is an ASCII file which I ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-05-2008, 05:53 AM
Alexy Khrabrov
 
Posts: n/a
Default numbering rows on import from file

Greetings -- I have a huge table of the form
(integer,integer,smallint,date). Its origin is an ASCII file which I
load with \copy. Now I want to number the rows, adding an id column
as an autoincrement from a sequence. How should I do the import now
for the sequence to work -- should I add the id column last, so it
will not be filled by copy and presumably autoincrement?

Or, once the table is already in, can I add a column and force it to
be filled with consecutive numbers, effectively numbering the rows?

Cheers,
Alexy

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-05-2008, 05:53 AM
chester c young
 
Posts: n/a
Default Re: numbering rows on import from file


--- Alexy Khrabrov <deliverable@gmail.com> wrote:

> Now I want to number the rows, adding an id column
> as an autoincrement from a sequence. How should I do the import now
> for the sequence to work -- should I add the id column last, so it
> will not be filled by copy and presumably autoincrement?


use a sequence
restart sequence to 1 before copy
have column id default to nextval('seq')

when doing copy don't have a column matching your id (duh)



__________________________________________________ __________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i...Dypao8Wcj9tAcJ

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 05:53 AM
Scott Marlowe
 
Posts: n/a
Default Re: numbering rows on import from file

On Fri, May 2, 2008 at 3:26 PM, Alexy Khrabrov <deliverable@gmail.com> wrote:
> Greetings -- I have a huge table of the form
> (integer,integer,smallint,date). Its origin is an ASCII file which I load
> with \copy. Now I want to number the rows, adding an id column as an
> autoincrement from a sequence. How should I do the import now for the
> sequence to work -- should I add the id column last, so it will not be
> filled by copy and presumably autoincrement?


Add the sequence to the column. something like this:

create table mytable (id serial primary key, int1 integer, int2
integer, smallint1 smallint, date1 date);
copy mytable (int1,int2, smallint1, date1) from STDIN;
120,2304,4,'2007-01-01'
204,3204,2,'2007-01-02'
(and so on)
\.

with a lot of other dbs, and a lot of languages, you're taught to
perform discrete steps when operating on your data. Generally
speaking, PostgreSQL is much better at doing the most NOW, not later.
If you've got derived data you want to put into the table, put all the
data into a loading table, and transfer it to the real table with ONE
insert into select from query.

> Or, once the table is already in, can I add a column and force it to be
> filled with consecutive numbers, effectively numbering the rows?


Bad idea. As mentioned before, every update or insert, whether it
succeeds or not will create a dead row in the table. If you update
(or attempt to update) all rows in a 10,000,000 row table three times,
you now have 30,000,000 dead rows in your table.

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-05-2008, 05:53 AM
Steve Crawford
 
Posts: n/a
Default Re: numbering rows on import from file

Alexy Khrabrov wrote:
> Greetings -- I have a huge table of the form
> (integer,integer,smallint,date). Its origin is an ASCII file which I
> load with \copy. Now I want to number the rows, adding an id column
> as an autoincrement from a sequence. How should I do the import now
> for the sequence to work -- should I add the id column last, so it
> will not be filled by copy and presumably autoincrement?
>
> Or, once the table is already in, can I add a column and force it to
> be filled with consecutive numbers, effectively numbering the rows?
>

Depends on your goal.

Do you just want a unique ID, make the id column a serial. Done. If you
already have the table, add the column, update the table setting
id=nextval('the serial columns sequence name') before bringing in
additional data. If you were using plain \copy, you will now have to
name the input columns since you don't have data for the id column:
\copy (int1, int2, smallint3, date4) from ..... This is usually good
practice anyway as additions to your table or column ordering changes
won't affect your import.

Do you want the IDs to match row numbers in the source file? Use
something like nl or whatever scripting language you like to add numbers
in the source file.

Alternately, if the table is static, you can create a temporary sequence
to fill the id column on import.

You can also fill the id column after import if necessary by updating
the table setting id=nextval('yoursequence') but this will generate lots
of empty space by updating all tuples (not good with a "huge" table) and
may not associate tuples with source-file line-numbers.

Regardless of how you initially fill the id column, if you add/delete
records your numbers will not be consecutive. Deletes and rollbacks will
cause holes.

If you just need consecutive row-numbering on output (not in the table)
and if the row numbering doesn't need to match the same record each
time, you can create a temporary sequence and select
nextval('tempsequence'),.... from yourtable.

Cheers,
Steve


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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:13 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com