Re: create table as vs. create table like

From: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: create table as vs. create table like
Date: 2008-12-12 13:07:54
Message-ID: 20081212140754.6aeaba0d@dawn.webthatworks.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 12 Dec 2008 13:25:07 +0100
"A. Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com> wrote:

> In response to Ivan Sergio Borgonovo :
> > I just noticed something I found "unexpected".
> >
> > CREATE TABLE LIKE let you specify DEFAULT and Co.
> > CREATE TABLE AS doesn't.
> >
> > Is there a one step way to clone a table?
>
> with or without data?
>
> create table new_table (like old_table)
> create table new_table as select * from old_table

They achieve partially what I'm looking for. Not that I really need
to save 100 bytes but as said it looked "unexpected" since most of
what's needed is almost there but is split across the 2 commands in
a "strange way".

create table like clones the "structure" including indexes, default
and constraint but not the content.

create table as clones the schema and the content but not the
indexes, default and constraints.

create table newtable (like oldtable including defaults, constraints,
indexes); -- I should test the syntax for multiple "including"

insert into newtable select * from oldtable;

Is the best thing that come close to cloning a table. On the top of
my head CHECKS will still miss.

Without cloning indexes, defaults, constraints create table as does
look really redundant (select into newtable) and create table like is
"almost there".

Gregory's remark about index creation is interesting... but well it
just would depend on the implementation.

--
Ivan Sergio Borgonovo
http://www.webthatworks.it

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-12-12 13:12:04 Re: Question with combining ANY with ilike
Previous Message Gregory Williamson 2008-12-12 12:46:35 Re: create table as vs. create table like