dbTalk Databases Forums  

easiest way to clone a table?

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss easiest way to clone a table? in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mh@pixar.com
 
Posts: n/a

Default easiest way to clone a table? - 09-23-2008 , 07:14 PM






Given a table FOO, what is the easiest way to create a
table COPY_OF_FOO that has identical schema, indexes, etc.?

Preferrably something that can be scripted or performed
programatically, and not via Toad, etc.
etc.

Many TIA!
Mark.


--
Mark Harrison
Pixar Animation Studios

Reply With Quote
  #2  
Old   
Malcolm Dew-Jones
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-23-2008 , 08:39 PM






mh (AT) pixar (DOT) com wrote:
: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.


I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. Otherwise just run the result multiple times
until you don't get any more errors.


Reply With Quote
  #3  
Old   
Malcolm Dew-Jones
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-23-2008 , 08:39 PM



mh (AT) pixar (DOT) com wrote:
: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.


I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. Otherwise just run the result multiple times
until you don't get any more errors.


Reply With Quote
  #4  
Old   
Malcolm Dew-Jones
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-23-2008 , 08:39 PM



mh (AT) pixar (DOT) com wrote:
: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.


I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. Otherwise just run the result multiple times
until you don't get any more errors.


Reply With Quote
  #5  
Old   
Malcolm Dew-Jones
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-23-2008 , 08:39 PM



mh (AT) pixar (DOT) com wrote:
: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.


I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. Otherwise just run the result multiple times
until you don't get any more errors.


Reply With Quote
  #6  
Old   
Mark D Powell
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-24-2008 , 02:43 PM



On Sep 23, 9:39*pm, yf... (AT) vtn1 (DOT) victoria.tc.ca (Malcolm Dew-Jones)
wrote:
Quote:
m... (AT) pixar (DOT) com wrote:

: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.

I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. *Otherwise just run the result multiple times
until you don't get any more errors.
No need to loop. The get_dependent_ddl procedure will produce DDL for
all indexes of a table.

select

dbms_metadata.get_dependent_ddl('INDEX',upper('&tb l_nm'),upper('&tbl_owner'))
from
sys.dual;

HTH -- Mark D Powell --


Reply With Quote
  #7  
Old   
Mark D Powell
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-24-2008 , 02:43 PM



On Sep 23, 9:39*pm, yf... (AT) vtn1 (DOT) victoria.tc.ca (Malcolm Dew-Jones)
wrote:
Quote:
m... (AT) pixar (DOT) com wrote:

: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.

I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. *Otherwise just run the result multiple times
until you don't get any more errors.
No need to loop. The get_dependent_ddl procedure will produce DDL for
all indexes of a table.

select

dbms_metadata.get_dependent_ddl('INDEX',upper('&tb l_nm'),upper('&tbl_owner'))
from
sys.dual;

HTH -- Mark D Powell --


Reply With Quote
  #8  
Old   
Mark D Powell
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-24-2008 , 02:43 PM



On Sep 23, 9:39*pm, yf... (AT) vtn1 (DOT) victoria.tc.ca (Malcolm Dew-Jones)
wrote:
Quote:
m... (AT) pixar (DOT) com wrote:

: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.

I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. *Otherwise just run the result multiple times
until you don't get any more errors.
No need to loop. The get_dependent_ddl procedure will produce DDL for
all indexes of a table.

select

dbms_metadata.get_dependent_ddl('INDEX',upper('&tb l_nm'),upper('&tbl_owner'))
from
sys.dual;

HTH -- Mark D Powell --


Reply With Quote
  #9  
Old   
Mark D Powell
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-24-2008 , 02:43 PM



On Sep 23, 9:39*pm, yf... (AT) vtn1 (DOT) victoria.tc.ca (Malcolm Dew-Jones)
wrote:
Quote:
m... (AT) pixar (DOT) com wrote:

: Given a table FOO, what is the easiest way to create a
: table COPY_OF_FOO that has identical schema, indexes, etc.?

: Preferrably something that can be scripted or performed
: programatically, and not via Toad, etc.
: etc.

: Many TIA!
: Mark.

I have used code like the following to generate table create statements.
If I want to create the tables using new names I usually pass the result
through a perl script, but you can also manipulate the result directly
with SQL functions such as REPLACE as part of the select.

col ddl format a400

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SEGMENT_ATTRIBUTES',false);

execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SE SSION_TRANSFORM
,'SQLTERMINATOR',true);

select dbms_metadata.get_ddl('TABLE',table_name) DDL from user_tables
where table_name like 'whatever%' order by table_name
/

The resulting scripts would build indexes for PK,UK, and FK constraints,
but not other indexes.

You can use dbms_metadata to get code for many objects besides tables, so
I would look at the parameters (assuming they exist) to use when looping
over user_indexes.

When you create the tables then some won't create due to FK dependencies.
If you looked at user_dependencies you could order the result so that you
don't get those errors. *Otherwise just run the result multiple times
until you don't get any more errors.
No need to loop. The get_dependent_ddl procedure will produce DDL for
all indexes of a table.

select

dbms_metadata.get_dependent_ddl('INDEX',upper('&tb l_nm'),upper('&tbl_owner'))
from
sys.dual;

HTH -- Mark D Powell --


Reply With Quote
  #10  
Old   
mh@pixar.com
 
Posts: n/a

Default Re: easiest way to clone a table? - 09-24-2008 , 03:49 PM



Malcolm Dew-Jones <yf110 (AT) vtn1 (DOT) victoria.tc.ca> wrote:
Quote:
You can use dbms_metadata to get code for many objects besides tables, so
This is what I needed... thanks!!

--
Mark Harrison
Pixar Animation Studios


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.