Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: numbering r...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 3448 of 3717
Post > Topic >>

Re: numbering rows on im****t from file

by scrawford@[EMAIL PROTECTED] (Steve Crawford) May 2, 2008 at 05:18 PM

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 im****t 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 im****t.

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 tem****ary sequence 
to fill the id column on im****t.

You can also fill the id column after im****t 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 tem****ary sequence and select 
nextval('tempsequence'),.... from yourtable.

Cheers,
Steve


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




 4 Posts in Topic:
numbering rows on import from file
deliverable@[EMAIL PROTEC  2008-05-02 14:26:34 
Re: numbering rows on import from file
chestercyoung@[EMAIL PROT  2008-05-02 15:33:39 
Re: numbering rows on import from file
scott.marlowe@[EMAIL PROT  2008-05-02 17:00:33 
Re: numbering rows on import from file
scrawford@[EMAIL PROTECTE  2008-05-02 17:18:27 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 14:26:03 CDT 2008.