Re: duplicating table - Mailing list pgsql-general

From Arguile
Subject Re: duplicating table
Date
Msg-id LLENKEMIODLDJNHBEFBOOEBJDPAA.arguile@lucentstudios.com
Whole thread Raw
In response to duplicating table  ("Johnson, Shaunn" <SJohnson6@bcbsm.com>)
List pgsql-general
Shaunn Johnson wrote
>
> Howdy:
> I know this will seem silly, but I'm trying to make a copy of a table
> with a few modifications.

[snip]

Both methods are possible, if you're created the table first then INSERT
INTO ... SELECT will work. Personally I don't often like using positional
inserts and that might be what's causing you to say "it doesn't work" (BTW
giving _why_ it doesn't work helps :). Try explicity stating what's going to
be inserted.

  INSERT INTO new_table (field1, field2, field3, etc.)
    SELECT field1, field2, field3, etc.
    FROM original_table
  ;

Alternately you can create the table using the SELECT ... INTO (or the SQL92
way CREATE TABLE .. AS SELECT). Then add the other attributes after.

  CREATE TABLE new_table AS SELECT * FROM original_table;
  ALTER TABLE new_table ADD field datatype [options];

This won't get the attribute ordering you want though w/o having to muck
about w/ pg_attributes.attnum (something I wouldn't suggest).



pgsql-general by date:

Previous
From: "Peter Darley"
Date:
Subject: Re: Performance tips
Next
From: Doug McNaught
Date:
Subject: Re: Performance tips