Generate Create Table Statemnets for a Schema
Gattu, August 05, 2001 - 10:53 am UTC
You are really great tom.
exec :null ;
A reader, April 16, 2002 - 12:43 pm UTC
when I created the function it returned nothing
then I saw what you did
scott@ORA817.US.ORACLE.COM> select create_table( user, table_name )
2 from user_tables
3 where rownum < 5
4 /
CREATE_TABLE(USER,TABLE_NAME)
---------------------------------------------------------------------------------
--------------------------------------------------
scott@ORA817.US.ORACLE.COM> spool generate_table.lst
scott@ORA817.US.ORACLE.COM> exec null;
when I tried the same it worked.
Can you explain why in first place , the query ....
scott@ORA817.US.ORACLE.COM> select create_table( user, table_name )
2 from user_tables
3 where rownum < 5
4 /
.....did not return any output, and then
when you did exec :null is returned the output , why so
can you please explain the technical details
April 16, 2002 - 9:32 pm UTC
If you get my book -- i give you more information about dbms_output then anyone really needs to know (plus tons of other useful stuff).
basically -- dbms_output puts the data into an array. sqlplus "pulls" the data out and prints it AFTER an insert/update/delete/PLSQL block. It is not expecting a SELECT to generate any dbms_output (selects don't fire triggers, plsql isn't generally run from a select) hence plus doesn't "pull it".
By doing "exec null", you are running a plsql block -- plus says "ah, maybe there it something to print" and it finds your last block of stuff to print out.
Tom, is it possible
A reader, April 17, 2002 - 8:13 am UTC
to generate the create table statements for a whole schema, and not just a single table.
Do you have a script to do that, or can you tell us how to generate create table scripts for all the tables in a schema.
Thank you
April 17, 2002 - 12:20 pm UTC
Already answered:
Well, frankly, I would simply use:
exp userid=user/password rows=n grants=n indexes=n constraints=n owner=user
imp userid=user/password full=y indexfile=user.sql
then, user.sql will have everything you want.
.....
A thought
A reader, April 17, 2002 - 1:34 pm UTC
Export and Import are server side tools. We wouldnt normally have them installed on the client side. And most of the developers have no access to the database server.
In that context, how can we export from the client side to a client side directory. The objective is to export all the objects belonging to a particular users own schema.
Mind you we are not talking about any server side activity, and no DBA's involved in this.
Is this possible at all.
April 17, 2002 - 8:34 pm UTC
Sorry -- but I've never seen a development environment where the developers don't have access to EXP and IMP, I've just never seen it.
In 9i there is dbms_metadata that can be used. In 8i and before (and actually in 9i) exp is the way to go. I would let my developers have at it with these tools, make them available somehow.
what can be exported.
Kulguru, April 18, 2002 - 12:06 am UTC
Assuming that the developers had access to import and export. What kind of objects , which objects belonging to which schema can they export. Can they export objects belonging to other schemas, but have been grants access.
What is the developers try to export schemas from production database, would it not slow down production database performance.
and lastly, if a developer runs export from the client side, where will the dump file be created, the client side or the server side.
Thank you
April 18, 2002 - 8:19 am UTC
export is just a program. It runs queries (SQL). It retrieves data. It can do nothing that the developer could not do using SQLPlus. It is just a tool, it is not evil.
They can only export things to which they have been granted access. (just like they can only query things to which they have been granted access).
What if the developers log into production and issue "select count(*) from t, t, t, t, t" where t is a 100,000 row table? would not that slow down production -- probably. The point here is? They can log into production today and do select * from table, export is just another way to do that. If you are concerned about developers consuming too much resources on production we have resource profiles to limit that.
The dmp file is created where EXP is run. EXP is just a client program much like SQLPlus is.
Answer of above question
Riaz Shahid, April 18, 2002 - 5:20 am UTC
By default, the dmp file will be saved on client side. However u can give file path also to store file where u desire.
April 18, 2002 - 8:52 am UTC
And that will STILL be on the client side. The DMP file is ALWAYS saved on the client side.
How about other objects?
Michael Leung, April 25, 2002 - 11:55 pm UTC
Tom,
If I want to export triggers, functions and other objects also, can I use exp/imp?
Michael
April 26, 2002 - 8:22 am UTC
yes. exp gets all of those.
External Tables (organization external)
Another Reader, December 23, 2002 - 9:56 am UTC
I am still missing the external tables definition in the output of the imp utility.
I am working with 9.2
December 23, 2002 - 11:17 am UTC
<b>[tkyte@tkyte-pc-isdn 9iTrain]$ exp userid=/ tables=external_table
</b>
Export: Release 9.2.0.1.0 - Production on Mon Dec 23 11:21:25 2002
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
Export done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table EXTERNAL_TABLE
Export terminated successfully without warnings.
<b>[tkyte@tkyte-pc-isdn 9iTrain]$ imp userid=/ full=y show=y
</b>
Import: Release 9.2.0.1.0 - Production on Mon Dec 23 11:21:38 2002
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
Export file created by EXPORT:V09.02.00 via conventional path
import done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
. importing OPS$TKYTE's objects into OPS$TKYTE
"CREATE TABLE "EXTERNAL_TABLE" ("EMPNO" NUMBER(4, 0), "ENAME" VARCH"
"AR2(10), "JOB" VARCHAR2(9), "MGR" NUMBER(4, 0), "HIREDATE" DATE, "SAL" NUMB"
"ER(7, 2), "COMM" NUMBER(7, 2), "DEPTNO" NUMBER(2, 0)) ORGANIZATION EXTERNAL"
" (TYPE "ORACLE_LOADER" DEFAULT DIRECTORY "DATA_DIR" ACCESS PARAMETERS ( "
"fields terminated by ',' ) LOCATION ( 'emp.dat' ) ) "
Import terminated successfully without warnings.
can you show me a counter case?
External Tables (organization external) with indexfile=test.sql
Another Reader, December 27, 2002 - 6:04 am UTC
Your example is OK.
But, it's easier to use the schema generated with the indexfile option instead of show=y.
exp ROWS=N FULL=Y FILE=exptest
imp ROWS=N FULL=Y FILE=exptest INDEXFILE=indexschema.sql
Now I am missing the external tables in indexschema.sql
December 27, 2002 - 9:54 am UTC
Well, that is a horse of a different color -- I was merely demonstrating that if you IMPORT -- the external table is there. I wasn't aware at all that you wanted it with the indexfile statement -- that wasn't clear.
You can use dbms_metadata in 9ir2 (doesn't work properly for external tables in 9ir1) instead if you like as well.
Fraser Clegg, October 28, 2003 - 10:28 am UTC
Tom,
I'm having problems with the indexfile option (8.1.6.0 on Sun 5.8) - Many of my create/alter table statements are wrapping making the script almost unusable on a regular basis:
REM ALTER TABLE "GENEVA"."SMDEPENDENTLINKS" ADD PRIMARY KEY
REM ("DEPENDEEID","DEPENDENTID","ASSOCIATION_ID_","DEPENDEETYPE","DE
REM PENDENTTYPE") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
Is this normal behaviour - is there anyway round it.
Thanks,
Fraser
October 28, 2003 - 10:42 am UTC
hmm, i reproduced in 815 using your table/column names.
in 817, it doesn't reproduce. so, it looks like they fixed that in the terminal release of 8i. It actually appears to be an EXPORT issue as using 817's import on the 815 dmp file exhibits the problem but using 817's export/import it works.
I don't know of a workaround.
Data only?
A reader, June 10, 2004 - 12:43 pm UTC
Hi Tom,
Can EXP/IMP be used to export/import data only, without the DDL associated with the tables? Thanks.
Thanks, Tom.
A reader, December 21, 2004 - 5:58 pm UTC
Thanks tom for the script, This is very useful to me as specially for 8.1.7.4
Just one thing though, can you update the script with the partitioned table also
regards,
December 21, 2004 - 7:23 pm UTC
left for an exercise for the reader.........
Thanks, for the reply (817 specific)
A reader, December 22, 2004 - 10:56 am UTC
hi tom, thanks for reply,
actually I am trying to do that, I used
user_tables, user_tab_columns,user_tab_partitions
tables to generate this ddl,
what I am missing is , how do(from which table) I know
is this a hash/range/list partition ?
December 22, 2004 - 11:11 am UTC
user_tab_partitions. read about the data dictionary in the reference manual.
If you wanted to do this in like 5 seconds, you would just use exp/imp. perhaps if you brough that up to the person that pays for you -- they would agree that somehow access to that tool could be provided (given the significant cost savings involved)
thanks,
A reader, December 22, 2004 - 11:27 am UTC
Create Table Scripts
Martin, December 22, 2004 - 12:39 pm UTC
Tools like Toad (aarghh Tom...Don't hit me ;-) can do this kind of stuff perfectly for you, including storage etc
can not find the informaiton
A reader, February 01, 2005 - 2:14 pm UTC
I am not able to find out the type of partitiona
form user_tab_partitions as you've suggested and
also I was not able to join user_tab_columns and
user_tab_partition views to identify
ON which column the partition is definted ?
please guide.
Thanks
February 01, 2005 - 3:43 pm UTC
i'll go back to *the software DOES THIS ALREADY*.
sorry, I'm not writing ddl extraction scripts. The last time I did that was version 6.0. It was boring and non-productive then, it is even worse now.
jametong, February 01, 2005 - 9:22 pm UTC
user_part_tables Get the partition Type & the subpartition type
user_part_key_columns Get the partition key columns .
SQL> select table_name,partitioning_type,subpartitioning_type
2 from user_part_tables
3 where rownum <= 3
4 /
TABLE_NAME PARTITI SUBPART
------------------------------ ------- -------
TJW_SNAP_USER_0114 LIST NONE
ACC_BUSI_FUND_576_P RANGE NONE
TJW_VLR_2004 LIST NONE
SQL> select name,object_type,column_name
2 from user_part_key_columns
3 where rownum <= 3
4 /
NAME OBJEC
------------------------------ -----
COLUMN_NAME
------------------------------------------------------------------------------------------------------------------------
ACC_BUSI_FUND_576_P TABLE
DONE_DATE
IDX_PKG_HP_BAK_TELE INDEX
INS_DATE
INDX_TOTAL_BUSI_RECORD INDEX
OP_MON
SQL>
yep, thanks
A reader, February 03, 2005 - 2:55 pm UTC
Thanks, I found it in the OTN docs, but thanks for suggestions.
regards,
Generate Create Table statements
Ganesh, March 16, 2005 - 7:48 am UTC
Hi Tom
imp with indexfile option is excellent. It is very useful.
If we have entire database dump file then how can we generate only for specified user.
Becuase we got a dump file from another location. It has mulitple users in same dump file. how can I generate DDL for specifie user.
Thanks
Ganesh
March 16, 2005 - 8:34 am UTC
use fromuser
dbms_metadata
Magesh, March 16, 2005 - 2:21 pm UTC
Tom,
I have a question regarding extracting the ddl using dbms_metadata. If we use imp with indexfile parameter, we are able to get the ddl for all tables. Is this possible in dbms_metadata by running a simple query from sql prompt for all tables or do I have to give each table name individually(either through sql or loop using pl/sql)? From my understanding, dbms_metadata is capable of extracting only one object's ddl at a time.
March 17, 2005 - 7:47 am UTC
select dbms_metadata.get_dll( .... )
from user_tables
/
if you use a query that calls dbms_metadata N times, you get N ddl's
dbms_metadata
Magesh, March 17, 2005 - 8:56 am UTC
Tom,
Thanks for your response. I take that as dbms_metadata.get_ddl and not get_dll. I guess there is a misunderstanding here. I have 10 tables in my schema say scott and I want the ddl to be generated for all 10 tables. If I try your syntax by passing one table's name say EMP, the ddl for only that EMP is generated 10 times. That's not what I want, I want the ddl for all the 10 tables in the schema scott. How can I do that?
March 17, 2005 - 9:31 am UTC
thought that part would be obvious, pass the table name you retrieve as a parameter.
select dbms_metadata.get_ddl( 'TABLE', table_name ) from user_tables;
dbms_metadata available on 8.1.7?
Mat, March 17, 2005 - 11:21 am UTC
Is there a backport of dbms_metadata for 8i?
Regards.
DBMS_METADATA.GET_DDL output
Richard Brown, July 28, 2006 - 3:18 pm UTC
I am using the example I found in Oracle documentation to generate DDL to re-create all tables in a schema using DBMS_METADATA.GET_DLL.
set pagesize 0
set long 90000
execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_ALL_TABLES u
WHERE u.nested='NO'
AND (u.iot_type is null or u.iot_type='IOT');
execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'DEFAULT');
However, the output is not formatted in a way that is completly usable to create the table.
The lines are broken to make the create table statement incorrect.
Example:
CREATE TABLE "PROD2"."TRACEID"
( "G3E_ID" NUMBER(10,0) CONSTRAINT "MS_N_TRACEID_ID" NOT NULL ENABLE,
"G3E_TRACEID" NUMBER(10,0) CONSTRAINT "MS_N_TRACEID_TRACEID" NOT NULL ENABLE,
"G3E_NAME" VARCHAR2(255) CONSTRAINT "MS_N_TRACEID_NAME" NOT NULL ENABLE,
"G3E_TABLENAME" VARCHAR2(30) DEFAULT 'CONN_REC' CONSTRAINT "MS_N_TRACEID_TABL
AME" NOT NULL ENABLE,
"G3E_NODE1COLUMN" VARCHAR2(30) DEFAULT 'NODE1_ID' CONSTRAINT "MS_N_TRACEID_NO
1COLUMN" NOT NULL ENABLE,
"G3E_NODE2COLUMN" VARCHAR2(30) DEFAULT 'NODE2_ID' CONSTRAINT "MS_N_TRACEID_NO
2COLUMN" NOT NULL ENABLE,
"G3E_SEEDFID" NUMBER(10,0),
"G3E_STOPCRITERIA" VARCHAR2(512),
"G3E_FILTERCRITERIA" VARCHAR2(512),
"G3E_PATHCOST" VARCHAR2(80),
"G3E_GOALFID" NUMBER(10,0),
"G3E_GOALFNO" NUMBER(5,0),
"G3E_FROMFNOCOLUMN" VARCHAR2(30),
"G3E_TOFNOCOLUMN" VARCHAR2(30),
"G3E_REVERSECOLUMN" VARCHAR2(30),
"G3E_REVERSEDVALUE" VARCHAR2(80),
"G3E_FIDCONSTRAINED" NUMBER(1,0) DEFAULT 0 CONSTRAINT "MS_N_TRACEID_FIDCONSTR
NED" NOT NULL ENABLE,
"G3E_ITERATIONPROC" VARCHAR2(128),
"G3E_SEEDCIDVALUES" VARCHAR2(512),
"G3E_HINTCIDVALUES" VARCHAR2(512),
"G3E_HINT" VARCHAR2(20),
CONSTRAINT "MS_C_TRACEID_ID" CHECK (G3E_ID BETWEEN -2147483648 AND 214748364
ENABLE,
CONSTRAINT "MS_P_TRACEID" PRIMARY KEY ("G3E_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING
TABLESPACE "PROD2" ENABLE,
CONSTRAINT "MS_C_TRACEID_SEEDFID" CHECK (G3E_SEEDFID IS NULL OR (G3E_SEEDFID
ETWEEN -2147483648 AND 2147483647)) ENABLE,
CONSTRAINT "MS_C_TRACEID_GOALFID" CHECK (G3E_GOALFID IS NULL OR (G3E_GOALFID
ETWEEN -2147483648 AND 2147483647)) ENABLE,
CONSTRAINT "MS_C_TRACEID_FIDCONSTRAINED" CHECK (G3E_FIDCONSTRAINED IN (0, 1)
ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
TABLESPACE "PROD2"
MONITORING ;
When run generates this error
ENABLE,
*
ERROR at line 28:
ORA-00907: missing right parenthesis
If I add the right paren to line 28 I get this error
ETWEEN -2147483648 AND 2147483647)) ENABLE,
*
ERROR at line 33:
ORA-00920: invalid relational operator
Is there a way to get DBMS_METADATA.GET_DDL to generate the create table statement so I do not have hand modify the statement to get it to work?
July 28, 2006 - 8:50 pm UTC
set linesize much wider so it does not wrap, alias the column:
SELECT DBMS_METADATA.GET_DDL('TABLE',u.table_name) ddl <<<====
FROM USER_ALL_TABLES u
so you can:
SQL> set linesize 1000
SQL> column ddl format a100 word_wrapped
so it breaks more "intelligently" - not sure how the B in Between could be getting zapped at all. can you help us reproduce?
DBMS_METADATA.GET_DDL format correct
Richard Brown, July 31, 2006 - 1:30 pm UTC
Setting the linesize and setting the column size with no wrap resolved the problem. Thanks to all. Not sure how the B in Between got zapped either. I may have done something to the file while I was playing with it.
Reader, February 22, 2008 - 10:54 am UTC
Tom,
Is there any way to generate separate scripts for each table/view in a schema?
Thank you.
February 22, 2008 - 12:16 pm UTC
search this site for
dbms_metadata.get_ddl
to see some examples and then check out the docs for the same.
that was new in 9i, did not exist in 8i
how to get dbms_metadata in oracle 8.1.0.7
senthilmurugan, March 09, 2011 - 12:01 am UTC
Hi Tom ,
When i tried to get metadata of some tables in oracle 8.1.0.7 its throwing error.
Can we use this dbms_metadata.get_ddl in 8i or its used only from 9i.
Thank You ...
March 09, 2011 - 9:20 am UTC
not very intuitive
joeb, July 16, 2019 - 3:13 pm UTC
these answers are so lame.
July 17, 2019 - 7:23 am UTC
Thanks for your highly constructive and insightful comment.