Home » RDBMS Server » Server Administration » generating table scripts
generating table scripts [message #62385] Mon, 19 July 2004 03:03 Go to next message
Joy
Messages: 21
Registered: November 2001
Junior Member
Dear All,

Can anyone provide me a script or link to generate tall table scripts along with all constraints for a particular schema.

This can be done using Toad thru Export Table scripts option.

But we do not want to use Toad, instead generate a script to get the same required output.

Hoping a reply at the earliest.

Thanks and Regards,

Joy.

 

 
Re: generating table scripts [message #62386 is a reply to message #62385] Mon, 19 July 2004 04:06 Go to previous messageGo to next message
Daljit Singh
Messages: 290
Registered: October 2003
Location: Texas
Senior Member
Hi,

I really don't know why you dont want to use Exp/Imp, Ok apart from that you can use the following small code for that :

DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT');

But if you want to go for a PL/SQL code (I don't know why?) so u can refer follwing URL :

http://www.orafaq.com/scripts/sql/mktable.txt

Daljit Singh.
Re: generating table scripts [message #62387 is a reply to message #62385] Mon, 19 July 2004 04:24 Go to previous message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
--
-- Let this be a test case
--
dbadmin@constitution_lawd2 > create table sample (id number not null);

Table created.

dbadmin@constitution_lawd2 >  alter table sample add(constraint myconstraint unique(id));

Table altered.

dbadmin@constitution_lawd2 > alter table sample add (anotherid number);

Table altered.

dbadmin@constitution_lawd2 > create index myindex on sample(anotherid);

Index created.

--
-- with 9i, you can grab the DDL with dbms_metadata.
--
dbadmin@constitution_lawd2 > @s

  CREATE TABLE "DBADMIN"."SAMPLE"
   (    "ID" NUMBER NOT NULL ENABLE,
        "ANOTHERID" NUMBER,
         CONSTRAINT "MYCONSTRAINT" UNIQUE ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE
   ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"
 ;

  CREATE UNIQUE INDEX "DBADMIN"."MYCONSTRAINT" ON "DBADMIN"."SAMPLE" ("ID")
  PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"
 ;

  CREATE INDEX "DBADMIN"."MYINDEX" ON "DBADMIN"."SAMPLE" ("ANOTHERID")
  PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"
 ;

  ALTER TABLE "DBADMIN"."SAMPLE" ADD CONSTRAINT "MYCONSTRAINT" UNIQUE ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
  TABLESPACE "SYSTEM"  ENABLE
 ;

--
-- This is the script
--

dbadmin@constitution_lawd2 > get s
  1  set long 500000000000000
  2  SET HEAD off
  3  SELECT DBMS_METADATA.GET_DDL('TABLE',D.TABLE_NAME)||';' FROM DBa_tables D where OWNER='DBADMIN';
  4  SELECT DBMS_METADATA.GET_DDL('INDEX',D.INDEX_NAME)||';' FROM dba_indexes D where owner='DBADMIN';
  5* SELECT DBMS_METADATA.GET_DDL('CONSTRAINT',D.CONSTRAINT_NAME)||';' FROM DBa_constraints D where OWNER='DBADMIN';
dbadmin@constitution_lawd2 >

--
-- You can also use export/import to get these!
--
-- DO the export first
bash-2.03$  exp dbadmin@lawd file=mydmp.dmp owner=dbadmin

Export: Release 9.2.0.1.0 - Production on Mon Jul 19 08:23:02 2004

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Password:

Connected to: Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters and Oracle Data Mining options
JServer Release 9.2.0.1.0 - Production
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)

About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user DBADMIN
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user DBADMIN
About to export DBADMIN's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definitions
. about to export DBADMIN's tables via Conventional Path ...
. . exporting table                         SAMPLE          0 rows exported
EXP-00091: Exporting questionable statistics.
EXP-00091: Exporting questionable statistics.
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully with warnings.

--
-- do the import with show=y and redirect the output to a log file.
--

bash-2.03$ imp dbadmin@lawd show=y log=ddl.sql file=mydmp.dmp full=y

Import: Release 9.2.0.1.0 - Production on Mon Jul 19 08:25:14 2004

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.

Password:

Connected to: Oracle9i Enterprise Edition Release 9.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters 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 US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8ISO8859P1 character set (possible charset conversion)
 "CREATE TABLE "SAMPLE" ("ID" NUMBER NOT NULL ENABLE, "ANOTHERID" NUMBER)  PC"
 "TFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS"
 " 1 FREELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING NOCOMPRESS"
. . skipping table "SAMPLE"
 "CREATE UNIQUE INDEX "MYCONSTRAINT" ON "SAMPLE" ("ID" )  PCTFREE 10 INITRANS"
 " 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLES"
 "PACE "SYSTEM" LOGGING"
 "CREATE INDEX "MYINDEX" ON "SAMPLE" ("ANOTHERID" )  PCTFREE 10 INITRANS 2 MA"
 "XTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "
 ""SYSTEM" LOGGING"
 "ALTER TABLE "SAMPLE" ADD  CONSTRAINT "MYCONSTRAINT" UNIQUE ("ID") USING IND"
 "EX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FRE"
 "ELIST GROUPS 1) TABLESPACE "SYSTEM" LOGGING ENABLE "
Import terminated successfully without warnings.
bash-2.03$

Previous Topic: How i can drop redo log member with STALE status!
Next Topic: How to increase the user connections.
Goto Forum:
  


Current Time: Sat Jul 06 22:40:21 CDT 2024