從Oracle9i開始Oracle提供了一個新的系統包DBMS_METADATA,可以用於提取對象創建的DDL語句。 這個Package功能極其強大,我們來看看它的使用方法.
1.獲得表的創建語句.
SQL> desc dbms_metadata FUNCTION ADD_TRANSFORM RETURNS NUMBER ... FUNCTION GET_DDL RETURNS CLOB Argument Name Type In/Out Default ------------------------------ ----------------------- ------ -------- OBJECT_TYPE VARCHAR2 IN NAME VARCHAR2 IN SCHEMA VARCHAR2 IN DEFAULT VERSION VARCHAR2 IN DEFAULT MODEL VARCHAR2 IN DEFAULT TRANSFORM VARCHAR2 IN DEFAULT .... SQL> set long 2000
SQL> select dbms_metadata.get_ddl(@#TABLE@#,@#TEST@#) from dual;
DBMS_METADATA.GET_DDL(@#TABLE@#,@#TEST@#)
--------------------------------------------------------------------------------
CREATE TABLE "SYS"."TEST"
( "OWNER" VARCHAR2(30),
"OBJECT_NAME" VARCHAR2(128),
"SUBOBJECT_NAME" VARCHAR2(30),
"OBJECT_ID" NUMBER,
"DATA_OBJECT_ID" NUMBER,
"OBJECT_TYPE" VARCHAR2(18),
"CREATED" DATE,
"LAST_DDL_TIME" DATE,
"TIMESTAMP" VARCHAR2(19),
"STATUS" VARCHAR2(7),
"TEMPORARY" VARCHAR2(1),
"GENERATED" VARCHAR2(1),
"SECONDARY" VARCHAR2(1)
) 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"
SQL>
2.進一步的,可以通過dbms_metadata獲得更全面的DDL語句
Oracle提供一個全面的范例:
$ORACLE_HOME/rdbms/demo/mddemo.sql
包括索引、授權、觸發器等所有DDL語句都可以被提取。