views:

8674

answers:

3

I know the statement:

create table xyz_new as select * from xyz;

Which copies the structure and the data, but what if I just want the structure?

+4  A: 

Using sql developer select the table and click on the DDL tab

You can use that code to create a new table with no data when you run it in a sql worksheet

sqldeveloper is a free to use app from oracle.

If the table has sequences or triggers the ddl will sometimes generate those for you too. You just have to be careful what order you make them in and know when to turn the triggers on or off.

mugafuga
+18  A: 

Just use a where clause that won't select any rows:

create table xyz_new as select * from xyz where 1=0;
Jim Hudson
simple, and did the trick - thank you :o)
Andrew
This is a great, clean answer. Just want to remind that this will *not* include any constraints.. the new table won't even have a primary key.
JosephStyons
this will not replicate sequences or triggers either.
mugafuga
nor will the new table have any indexes - don't get caught out trying to do a big query on the new table :-)
hamishmcn
+4  A: 

I used the method that you accepted a lot, but as someone pointed out it doesn't duplicate constraints (except for NOT NULL, I think).

A more advanced method if you want to duplicate the full structure is:

SET LONG 5000
SELECT dbms_metadata.get_ddl( 'TABLE', 'MY_TABLE_NAME' ) FROM DUAL;

This will give you the full create statement text which you can modify as you wish for creating the new table. You would have to change the names of the table and all constraints of course.

(You could also do this in older versions using EXP/IMP, but it's much easier now.)

Dave Costa