DDL操作内部执行原理
执行完exp/imp之后,可以在imp的log文件中查看到相关表的建表语句,和与该表相关的其他数据库对象的ddl语句,如索引、触发器等。
Ddl操作实际上在内部被转化一系列的dml语句进行执行
跟踪ddl操作,查看Oracle后台数据字典自动执行的内容:
scott@ORCL>conn / as sysdba;
已连接。
sys@ORCL>alter session set events '10046trace name context forever,level 12';
会话已更改。
sys@ORCL>create table trace_ddl asselect * from dba_users;
表已创建。
sys@ORCL>select value from v$diag_infowhere;
VALUE
----------------------------------------
d:\app\lenovo\diag\rdbms\orcl\orcl\trace
\orcl_ora_7460.trc
查看该文件中的内容:
首先记录的创建语句:
PARSING IN CURSOR #2 len=49 dep=0 uid=0oct=1 lid=0 tim=6439856921 hv=1016677043 ad='7ffbd8a2020' sqlid='6urykycy9khpm'
create table trace_ddl as select * fromdba_users
END OF STMT
向obj中增加记录的dml语句
PARSING IN CURSOR #4 len=216 dep=1 uid=0oct=2 lid=0 tim=6439892002 hv=714380553 ad='7ffc849c3c0' sqlid='4bjwv5sp99589'
insert intoobj$(owner#,name,namespace,obj#,type#,ctime,mtime,stime,status,remoteowner,linkname,subname,dataobj#,flags,oid$,spare1,spare2,spare3)values(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,:17,:18)
END OF STMT
更新con$表的信息:
PARSING IN CURSOR #4 len=61 dep=1 uid=0 oct=6 lid=0 tim=6439918579hv=4246113160 ad='7ffbdca6628' sqlid='bajr90ryjd2w8'
update con$ set con#=:3,spare1=:4 whereowner#=:1 and name=:2
END OF STMT
增加段信息,向seg$表插入数据的语句:
PARSING IN CURSOR #4 len=259 dep=1 uid=0oct=2 lid=0 tim=6440002337 hv=1814305607 ad='7ffbd89ed80' sqlid='g7mt7ptq286u7'
insert into seg$(file#,block#,type#,ts#,blocks,extents,minexts,maxexts,extsize,extpct,user#,iniexts,lists,groups,cachehint,hwmincr,spare1, scanhint, bitmapranges) values(:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15,:16,DECODE(:17,0,NULL,:17),:18,:19)
END OF STMT
向col$增加字段信息:
PARSING IN CURSOR #4 len=453 dep=1 uid=0oct=2 lid=0 tim=6440042013 hv=224718466 ad='7ffbdca27b8' sqlid='60uw2vh6q9vn2'
insert intocol$(obj#,name,intcol#,segcol#,type#,length,precision#,scale,null$,offset,fixedstorage,segcollength,deflength,default$,col#,property,charsetid,charsetform,spare1,spare2,spare3)values(:1,:2,:3,:4,:5,:6,decode(:5,182/*DTYIYM*/,:7,183/*DTYIDS*/,:7,decode(:7,0,null,:7)),decode(:5,2,decode(:8,-127/*MAXSB1MINAL*/,null,:8),178,:8,179,:8,180,:8,181,:8,182,:8,183,:8,231,:8,null),:9,0,:10,:11,decode(:12,0,null,:12),:13,:14,:15,:16,:17,:18,:19,:20)
END OF STMT
Oracle通过将ddl解析为dml操作,并且将这些操作全部记录在数据字典中,通过将这些信息反向解析,,可以得到原始的创建语句
通过dbms_metadata可以得到原始的创建语句:
sys@ORCL>SELECTDBMS_METADATA.GET_DDL('TABLE','TRACE_DDL') FROM DUAL;
DBMS_METADATA.GET_DDL('TABLE','TRACE_DDL')
--------------------------------------------------------------------------------
CREATE TABLE "SYS"."TRACE_DDL"
( "USERNAME" VARCHAR2(30)NOT NULL ENABLE,
"USER_ID" NUMBER NOT NULL ENABLE,
"PASSWORD" VARCHAR2(30),
"ACCOUNT_STATUS" VARCHAR2(32) NOT NULL ENABLE,
"LOCK_DATE" DATE,
"EXPIRY_DATE" DATE,
"DEFAULT_TABLESPACE" VARCHAR2(30) NOT NULL ENABLE,
"TEMPORARY_TABLESPACE" VARCHAR2(30) NOT NULL ENABLE,
"CREATED" DATE NOT NULL ENABLE,
"PROFILE" VARCHAR2(30) NOT NULL ENABLE,
"INITIAL_RSRC_CONSUMER_GROUP" VARCHAR2(30),
"EXTERNAL_NAME" VARCHAR2(4000),
"PASSWORD_VERSIONS"VARCHAR2(8),
"EDITIONS_ENABLED" VARCHAR2(1),
"AUTHENTICATION_TYPE" VARCHAR2(8)
)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 DEFAULTFLASH_CACHE DEFAULT CELL_FLA
SH_CACHE DEFAULT)
TABLESPACE "SYSTEM"
查看建表语句有另一个办法:
1. exp username/pwd@service_name file=?rows=n
2. imp username/pwd@service_name file=?show=y log=? tables=?
执行完exp/imp之后,可以在imp的log文件中查看到相关表的建表语句,和与该表相关的其他数据库对象的ddl语句,如索引、触发器等。
如果在imp的参数中以 fromuser, touser 来代替tables,会在log文件中看到该模式内所有对象的ddl语句。
相关阅读:
两台Oracle之间配置OGG-未配置同步DDL
Oracle GoldenGate 11g单向DDL配置实战
Oracle利用dbms_metadata.get_DDL查看DDL语句
Oracle使用系统级触发器禁用DDL语句
Oracle使用系统级触发器审计重要帐号的DDL语句