Re: create table as vs. create table like - Mailing list pgsql-general

From Ivan Sergio Borgonovo
Subject Re: create table as vs. create table like
Date
Msg-id 20081212140754.6aeaba0d@dawn.webthatworks.it
Whole thread Raw
In response to Re: create table as vs. create table like  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
List pgsql-general
On Fri, 12 Dec 2008 13:25:07 +0100
"A. Kretschmer" <andreas.kretschmer@schollglas.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


pgsql-general by date:

Previous
From: "Gregory Williamson"
Date:
Subject: Re: create table as vs. create table like
Next
From: Tom Lane
Date:
Subject: Re: Question with combining ANY with ilike