GET_DDL提取建表语句:ddl

创建对象的语句就是了
提取表
set line 200 pages 50000 wrap on long 999999 serveroutput on
SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') FROM DUAL;
CREATE TABLE "SCOTT"."EMP"
   (    "EMPNO" NUMBER(4,0),
        "ENAME" VARCHAR2(10),
        "JOB" VARCHAR2(9),
        "MGR" NUMBER(4,0),
        "HIREDATE" DATE,
        "SAL" NUMBER(7,2),
        "COMM" NUMBER(7,2),
        "DEPTNO" NUMBER(2,0),
         CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
  STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
  PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
  BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"  ENABLE,
         CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
          REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
   ) SEGMENT CREATION IMMEDIATE
  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 FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
  TABLESPACE "USERS"
 
---有没有感觉太乱了,逗---
 
--输出信息采用缩排或换行格式化
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'PRETTY', TRUE);
--确保每个语句都带分号
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'SQLTERMINATOR', TRUE);
--关闭表索引、外键等关联(后面单独生成)
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'CONSTRAINTS', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'REF_CONSTRAINTS', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'CONSTRAINTS_AS_ALTER', FALSE);
--关闭存储、表空间属性
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'STORAGE', FALSE);
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'TABLESPACE', FALSE);
--关闭创建表的PCTFREE、NOCOMPRESS等属性
EXEC DBMS_METADATA.set_transform_param(DBMS_METADATA.session_transform, 'SEGMENT_ATTRIBUTES', FALSE);

set line 200 pages 50000 wrap on long 999999 serveroutput on
*******查询表请使用:关闭创建表的PCTFREE、NOCOMPRESS等属性+换行格式化+分号
 select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') FROM DUAL;
 
*****查询索引  ==没啥好办法,上方:换行+分号吧
select dbms_metadata.get_ddl('SEQUENCE','SYSTEM_GRANT','SYS') FROM DUAL;
 
*****查询索引的元数据:
select dbms_metadata.get_ddl('INDEX','EMP_EMAIL_UK','SCOTT')||';' FROM DUAL;
 
*****表空间的元数据:
select dbms_metadata.get_ddl('TABLESPACE','UNDO2') from dual;
 
 
**效果:

SQL> select dbms_metadata.get_ddl('TABLE','EMP','SCOTT') FROM DUAL;

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

--------------------------------------------------------------------------------

  CREATE TABLE "SCOTT"."EMP"    (    "EMPNO" NUMBER(4,0),         "ENAME" VARCHAR2(10),         "JOB" VARCHAR2(9),         "MGR" NUMBER(4,0),         "HIREDATE" DATE,         "SAL" NUMBER(7,2),         "COMM" NUMBER(7,2),         "DEPTNO" NUMBER(2,0)    ) ;

 
posted @ 2017-10-24 17:11  绿茶有点甜  阅读(2439)  评论(0编辑  收藏  举报