- 浏览: 1318886 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (351)
- Java General (37)
- .net General (2)
- Linux Toy (55)
- Oracle (81)
- Mysql (11)
- Programer Career (12)
- Oh, my living ! (2)
- Shell Script (8)
- Web Service (0)
- Linux Server (22)
- Php/Python/Perl (3P) (2)
- Javascript General (5)
- Saleforce Apex Dev (2)
- Web General (5)
- Xen & VM tech. (17)
- PSP (13)
- OpenSolaris (34)
- php (1)
- RAI/flex/action script (16)
- asterisk/CTI (7)
- 交互设计 (6)
- English (3)
- Lucene (1)
最新评论
-
GuolinLee:
markmark
JVM调优总结 -Xms -Xmx -Xmn -Xss -
di1984HIT:
写的太好啊。
JVM调优总结 -Xms -Xmx -Xmn -Xss -
javajdbc:
javajdbc 写道
JVM调优总结 -Xms -Xmx -Xmn -Xss -
javajdbc:
...
JVM调优总结 -Xms -Xmx -Xmn -Xss -
alvin198761:
非常感谢,国外的被封杀了,你这里还有一份
How to Convert An Image-Based Guest To An LVM-Based Guest
Starting from Oracle 9i Release 1, the DBMS_METADATA package has put an official end to all such scripting effort. This article provides a tour of the reverse engineering features of the above package, with a focus on generating the creation DDL of existing database objects. The article also has a section covering the issue of finding object dependencies.
Why do we need to reverse engineer object creation DDL
We need them for several reasons:
- Database upgrade from earlier versions when for various reason export-import is the only way out. But huge databases would require a precreated structure - importing data with several parallel processes into individual tables.
- Moving development objects into production. The cleanest method is to reverse engineer the DDL of the existing objects and run them in the production.
- For learning the various parameters that an object has been created with. When we create an object, we do not specify all the options, letting Oracle pick the defaults. We might want to view the defaults that have been picked up, or we might want to crosscheck the parameters of the object. For that we need Enterprise Manager, Toad, or some other tool, or self-developed queries in the data dictionary. Now DBMS_METADATA get the clean complete DDL with all options.
Modes of usage of the Metadata Package
- A set of functions that can be used with SQL. This is known as the browsing interface. The functions in the browsing interface are GET_DDL, GET_DEPENDENT_DDL, GET_GRANTED_DDL
- A set of functions that can be used in PLSQL, which is in fact a superset of (1). They support filtering, and optional turning on and turning off of some clause in the DDL. The flexibilities provided by the programmer interface are rarely required. For general use the browsing interface is sufficient - more so if the programmer knows SQL well.
Retrieving DDL information by SQL
As mentioned in the section above, GET_DDL, GET_DEPENDENT_DDL and GET_GRANTED_DDL are the three functions in this mode. The next few sections discuss them in detail. The objects on which the examples are tested are given in Table 9.
GET_DDL
The general syntax of GET_DDL is
GET_DDL(object_type, name, schema, version, model, transform).
Version, model and transform take the default values "COMPATIBLE", "ORACLE", and "DDL" - further discussion of these is not in the scope of this article.
object_type can be any of the object types given in Table 8 below. Table 1 shows a simple usage of the GET_DDL function to get all the tables of a schema. This function can only be used to fetch named objects, that is, objects with type N or S in Table 8. We will see in a later section how the "/" at the end of the DDL can be turned on by default.
Table 1 (DBMS_METADATA.GET_DDL Usage)
SQL> set head off
SQL> set long 1000
SQL> set pages 0
SQL> show user
USER is "REVRUN"
SQL>
SQL> select DBMS_METADATA.GET_DDL('TABLE','EMPLOYEE')||'/' from dual;
CREATE TABLE "REVRUN"."EMPLOYEE"
( "LASTNAME" VARCHAR2(60) NOT NULL ENABLE,
"FIRSTNAME" VARCHAR2(20) NOT NULL ENABLE,
"MI" VARCHAR2(2),
"SUFFIX" VARCHAR2(10),
"DOB" DATE NOT NULL ENABLE,
"BADGE_NO" NUMBER(6,0),
"EXEMPT" VARCHAR2(1) NOT NULL ENABLE,
"SALARY" NUMBER(9,2),
"HOURLY_RATE" NUMBER(7,2),
PRIMARY KEY ("BADGE_NO")
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"
/
GET_DEPENDENT_DDL
The general syntax of GET_DEPENDENT_DDL is
GET_DEPENDENT_DDL(object_type, base_object_name, base_object_schema,
version, model, transform, object_count)
Version, model and transform take the default values "COMPATIBLE", "ORACLE" and "DDL", and are not discussed futher. object_count takes the default of 10000 and can be left like that for most cases.
object_type can be any object of type D in Table 8. base_object_name is the base object on which the object_type objects are dependent.
The GET_DEPENDENT_DDL function allows the fetching of metadata for dependent objects with a single call. For some object types, other functions can be used for the same effect. For example, GET_DDL can be used to fetch an index by its name or GET_DEPENDENT_DDL can be used to fetch the same index by specifying the table on which it is defined. An added reason for using GET_DEPENDENT_DDL in this case might be that it gives the DDL of all dependent objects of that base object and the specific object type.
Table 2 shows a simple usage of GET_DEPENDENT_DDL.
Table 2 (GET_DEPENDENT_DDL example)
SQL> column aa format a132 SQL> SQL> select DBMS_METADATA.GET_DEPENDENT_DDL('TRIGGER','EMPLOYEE') aa from dual; CREATE OR REPLACE TRIGGER "REVRUN"."HOURLY_TRIGGER" before update of hourly_rate on employee for each row begin :new.hourly_rate:=:old.hourly_rate;end; ALTER TRIGGER "REVRUN"."HOURLY_TRIGGER" ENABLE CREATE OR REPLACE TRIGGER "REVRUN"."SALARY_TRIGGER" before insert or update of salary on employee for each row WHEN (new.salary > 150000) CALL check_sal(:new.salary) ALTER TRIGGER "REVRUN"."SALARY_TRIGGER" ENABLE
GET_GRANTED_DDL
The general syntax of GET_GRANTED_DDL is
GET_GRANTED_DDL(object_type, grantee, version, model, transform, object_count)
Version, model and transform take the default values "COMPATIBLE", "ORACLE" and "DDL", and need no further discussion.
object_count takes the default of 10000, and can be left like that for most cases.
grantee is the user who is granting the object_types. The object types that can work in GET_GRANTED_DDL are the ones with type G in Table 8. Table 3 shows a simple usage of the GET_GRANTED_DDL function.
Table 3 (GET_GRANTED_DDL Usage)
SQL> set long 99999 SQL> column aa format a132 SQL> select DBMS_METADATA.GET_GRANTED_DDL('OBJECT_GRANT','REVRUN_USER') aa from dual; GRANT UPDATE ("SALARY") ON "REVRUN"."EMPLOYEE" TO "REVRUN_USER" GRANT UPDATE ("HOURLY_RATE") ON "REVRUN"."EMPLOYEE" TO "REVRUN_USER" GRANT INSERT ON "REVRUN"."TIMESHEET" TO "REVRUN_USER" GRANT UPDATE ON "REVRUN"."TIMESHEET" TO "REVRUN_USER"
Table 4 below classifies some common objects as Dependent Object (D), Named Object (N) or Granted Object (G). Some objects exhibit more than one such property. For a complete list, refer to the Oracle Documentation. However, the list below will meet most requirements.
Metadata information retrieval by programmatic interface
The programmatic interface is for fine-grained detailed control on DDL generation. The list of procedures available for use in the programmatic interface is as follows:
- OPEN
- SET_FILTER
- SET_COUNT
- GET_QUERY
- SET_PARSE_ITEM
- ADD_TRANSFORM
- SET_TRANSFORM_PARAM
- FETCH_xxx
- CLOSE
To make use of this interface one must write a PLSQL block. Considering the fact that several CLOB columns are involved, this is not simple. However, the next section shows how to use the SET_TRANSFORM_PARM function in SQLPLUS in order to perform most of the jobs done by this interface. If one adds simple SQL skills to it, the programmatic interface can be bypassed in almost all cases. To get details of the programmatic interface, the reader should refer to the documentation.
Using the SET_TRANSFORM_PARAM function in SQL Session
This function determines how the output of the DBMS_METADATA is displayed. The general syntax is
SET_TRANSFORM_PARAM(transform_handle, name, value).
transform_handle for SQL Sessions is DBMS_METADATA.SESSION_TRANSFORM
name is the name of the transform, and value is essentially TRUE or FALSE.
Table 4 shows how to get the DDL of tables not containing the word LOG in a good indented form and with SQL Terminator without a storage clause.
Table 4 (SET_TRANSFORM_PARAM Usage)
SQL> execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false); PL/SQL procedure successfully completed. SQL> execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',true); PL/SQL procedure successfully completed. SQL> execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',true); PL/SQL procedure successfully completed. SQL> select dbms_metadata.get_ddl('TABLE',table_name) from user_tables 2 where table_name not like '%LOG'; CREATE TABLE "REVRUN"."EMPLOYEE" ( "LASTNAME" VARCHAR2(60) NOT NULL ENABLE, "FIRSTNAME" VARCHAR2(20) NOT NULL ENABLE, "MI" VARCHAR2(2), "SUFFIX" VARCHAR2(10), "DOB" DATE NOT NULL ENABLE, "BADGE_NO" NUMBER(6,0), "EXEMPT" VARCHAR2(1) NOT NULL ENABLE, "SALARY" NUMBER(9,2), "HOURLY_RATE" NUMBER(7,2), PRIMARY KEY ("BADGE_NO") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 TABLESPACE "SYSTEM" ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "SYSTEM" ; CREATE TABLE "REVRUN"."TIMESHEET" ( "BADGE_NO" NUMBER(6,0), "WEEK" NUMBER(2,0), "JOB_ID" NUMBER(5,0), "HOURS_WORKED" NUMBER(4,2), FOREIGN KEY ("BADGE_NO") REFERENCES "REVRUN"."EMPLOYEE" ("BADGE_NO") ENABLE ) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "SYSTEM" ; SQL>
Thus we see how a DDL requirement even with some filtering condition and a formatting requirement was met by the SQL browsing interface along with SET_SESSION_TRANSFORM.
Table 5 shows the name and meaning of the SET_SESSION_TRANSFORM parameters.
Table 5 (SET_SESSION_TRANSFORM "name" Parameters)
PRETTY (all objects) - If TRUE, format the output with indentation and line feeds. Defaults to TRUE. SQLTERMINATOR (all objects) - If TRUE, append a SQL terminator (; or /) to each DDL statement. Defaults to FALSE. DEFAULT (all objects) - Calling SET_TRANSFORM_PARAM with this parameter set to TRUE has the effect of resetting all parameters for the transform to their default values. Setting this FALSE has no effect. There is no default. INHERIT (all objects) - If TRUE, inherits session-level parameters. Defaults to FALSE. If an application calls ADD_TRANSFORM to add the DDL transform, then by default the only transform parameters that apply are those explicitly set for that transform handle. This has no effect if the transform handle is the session transform handle. SEGMENT_ATTRIBUTES (TABLE and INDEX) - If TRUE, emit segment attributes (physical attributes, storage attributes, tablespace, logging). Defaults to TRUE. STORAGE (TABLE and INDEX) - If TRUE, emit storage clause. (Ignored if SEGMENT_ATTRIBUTES is FALSE.) Defaults to TRUE. TABLESPACE (TABLE and INDEX) - If TRUE, emit tablespace. (Ignored if SEGMENT_ATTRIBUTES is FALSE.) Defaults to TRUE. CONSTRAINTS (TABLE) - If TRUE, emit all non-referential table constraints. Defaults to TRUE. REF_CONSTRAINTS (TABLE) - If TRUE, emit all referential constraints (foreign key and scoped refs). Defaults to TRUE. CONSTRAINTS_AS_ALTER (TABLE) - If TRUE, emit table constraints as separate ALTER TABLE (and, if necessary, CREATE INDEX) statements. If FALSE, specify table constraints as part of the CREATE TABLE statement. Defaults to FALSE. Requires that CONSTRAINTS be TRUE. FORCE (VIEW) - If TRUE, use the FORCE keyword in the CREATE VIEW statement. Defaults to TRUE.
DBMS_METADATA Security Model
The object views of the Oracle metadata model implement security as follows:
- Non-privileged users can see the metadata only of their own objects.
- SYS and users with SELECT_CATALOG_ROLE can see all objects.
- Non-privileged users can also retrieve object and system privileges granted to them or by them to others. This also includes privileges granted to PUBLIC.
- If callers request objects they are not privileged to retrieve, no exception is raised; the object is simply not retrieved.
- If non-privileged users are granted some form of access to an object in someone else's schema, they will be able to retrieve the grant specification through the Metadata API, but not the object's actual metadata.
Finding objects that are dependent on a given object
This is another type of requirement. While dropping a seemingly unimportant table or procedure from a schema one might like to know the objects that are dependent on this object.
The data dictionary view DBA_DEPENDENCIES or USER_DEPENDENCIES or ALL_DEPENDENCIES is the answer to these requirements. The columns of the ALL_DEPENDENCIES view are discussed in Table 6. ALL_DEPENDENCIES describes dependencies between procedures, packages, functions, package bodies, and triggers accessible to the current user, including dependencies on views created without any database links. Only tables are left out of this view. However for finding table dependencies we can use ALL_CONSTRAINTS. The ALL_DEPENDENCIES view comes to the rescue in the very important area of finding dependencies between stored code objects.
Table 6 (Columns of ALL_DEPENDENCIES table)
Column Description ------ ----------- OWNER Owner of the object NAME Name of the object TYPE Type of object REFERENCED_OWNER Owner of the parent object REFERENCED_NAME Type of parent object REFERENCED_TYPE Type of referenced object REFERENCED_LINK_NAME Name of the link to the parent object (if remote) SCHEMAID ID of the current schema DEPENDENCY_TYPE Whether the dependency is a REF dependency (REF) or not (HARD)
Table 7 below shows how to use the above view to get the dependencies. The example shows a case where we might want to drop the procedure CHECK_SAL, but we would like to find any objects dependent on it. The query below shows that a TRIGGER named SALARY_TRIGGER is dependent on it.
Table 7 (Use of the ALL_DEPENDENCIES view)
SQL> select name, type, owner 2 from all_dependencies 3 where referenced_owner = 'REVRUN' 4 and referenced_name = 'CHECK_SAL'; NAME TYPE OWNER ------------------------------ ----------------- ---------------------- SALARY_TRIGGER TRIGGER REVRUN
CONCLUSION
This article is intended to give the minimum effort answer to elementary and intermediate level object dependency related issues. For advanced object dependency issues, this article points to the solution. As Oracle keeps on upgrading its versions, it is clear that they will be upgrading the DBMS_METADATA interface and ALL_DEPENDENCIES view along with it. The solutions developed along those lines will persist.
Table 8 (Classifying common database objects as Named, Dependent, Granted and Schema objects)
CONSTRAINT (Constraints) SND DB_LINK (Database links) SN DEFAULT_ROLE (Default roles) G FUNCTION (Stored functions) SN INDEX (Indexes) SND MATERIALIZED_VIEW (Materialized views) SN MATERIALIZED_VIEW_LOG (Materialized view logs) D OBJECT_GRANT (Object grants) DG PACKAGE (Stored packages) SN PACKAGE_SPEC (Package specifications) SN PACKAGE_BODY (Package bodies) SN PROCEDURE (Stored procedures) SN ROLE (Roles) N ROLE_GRANT (Role grants) G SEQUENCE (Sequences) SN SYNONYM (Synonyms) S SYSTEM_GRANT (System privilege grants) G TABLE (Tables) SN TABLESPACE (Tablespaces) N TRIGGER (Triggers) SND TYPE (User-defined types) SN TYPE_SPEC (Type specifications) SN TYPE_BODY (Type bodies) SN USER (Users) N VIEW (Views) SN
Table 9 (Creation script of the REVRUN Schema)
connect system/manager drop user revrun cascade; drop user revrun_user cascade; drop user revrun_admin cascade; create user revrun identified by revrun; GRANT resource, connect, create session , create table , create procedure , create sequence , create trigger , create view , create synonym , alter session TO revrun; create user revrun_user identified by user1; create user revrun_admin identified by admin1; grant connect to revrun_user; grant connect to revrun_admin; connect revrun/revrun Rem Creating employee tables... create table employee ( lastname varchar2(60) not null, firstname varchar2(20) not null, mi varchar2(2), suffix varchar2(10), DOB date not null, badge_no number(6) primary key, exempt varchar(1) not null, salary number (9,2), hourly_rate number (7,2) ) / create table timesheet (badge_no number(6) references employee (badge_no), week number(2), job_id number(5), hours_worked number(4,2) ) / create table system_log (action_time DATE, lastname VARCHAR2(60), action LONG ) / Rem grants... grant update (salary,hourly_rate) on employee to revrun_user; grant ALL on employee to revrun_admin with grant option; grant insert,update on timesheet to revrun_user; grant ALL on timesheet to revrun_admin with grant option; Rem indexes... create index i_employee_name on employee(lastname); create index i_employee_dob on employee(DOB); create index i_timesheet_badge on timesheet(badge_no); Rem triggers create or replace procedure check_sal( salary in number) as begin return; -- Demo code end; / create or replace trigger salary_trigger before insert or update of salary on employee for each row when (new.salary > 150000) call check_sal(:new.salary) / create or replace trigger hourly_trigger before update of hourly_rate on employee for each row begin :new.hourly_rate:=:old.hourly_rate;end; /
发表评论
-
About Dedicated and Shared Server Processes
2010-11-29 15:46 1550一句话, shared server 就是为了省 SGA. ... -
oracle11GR2上建立一个新用户的过程,同时更改字符集.
2010-11-15 16:21 2915写道 SQL> create user mygmccr ... -
comment on table and column
2009-11-20 16:16 3358comment [Oracle SQL] ... -
解决ASM无法启动问题
2009-11-07 15:11 7110启动报错如下所 ... -
在Oracle中实现可扩展的多级编目结构
2009-10-23 13:49 13782009-10-16 ... -
用户帐号解锁
2009-10-21 08:06 1278SQL> alter user scott accoun ... -
按上下键调出 sqlplus 中的历史命令
2009-10-21 07:50 1983在sqlplus中不能按上下键不能显示出之前的命令, 也 ... -
简单的oracle物化视图
2009-09-28 22:29 1252物化视图是一种特殊的物理表,“物化”(Mate ... -
PL/SQL 总结(4)
2009-09-19 17:40 1064存储过程 create or replace PROCEDU ... -
PL/SQL 总结(3)
2009-09-19 17:40 1044使用游标 1)显示游标: CURSOR name_curs ... -
PL/SQL 总结(2)
2009-09-19 17:39 1079)将select 嵌入到PL/SQL中 ... -
PL/SQL 总结(1)
2009-09-19 17:38 1270我们开始学习PL/SQL PL/SQ ... -
Oracle 中的 Merge 语句
2008-07-29 15:45 1378Merge Statement Demo MERGE & ... -
SQL*Plus FAQ
2008-07-24 10:04 2168SQL*Pl ... -
Oracle Default Listener
2008-07-15 15:41 2281042 第23题 关于动态注册监听器 23.Your data ... -
自动安装 Oracle 数据库 10g 和 Red Hat Enterprise Linux
2008-07-13 09:52 2315自动安装 Oracle 数据库 10g 和 Red Hat ... -
在 Linux x86 上安装 Oracle 数据库 10g
2008-07-13 09:46 1343... -
Vmware server1.0 + Linux As4 + Oracle 10g RAC
2008-07-05 15:19 3242Vmware server1.0 + Linux A ... -
How To Set Up Oracle ASM on Ubuntu Gutsy Gibbon
2008-07-05 08:51 1961How To Set Up Oracle ASM on Ubu ... -
Installing Oracle10g R2 RAC on vmware suse
2008-07-04 10:47 4914Installing Oracle10g R2 RAC Par ...
相关推荐
Mastering Reverse Engineering
In Advanced Apple Debugging and Reverse Engineering, you’ll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but...
《Apple Debugging and Reverse Engineering V3.0》是一本深入探讨苹果系统调试和逆向工程的资源合集,包括PDF和epub格式的电子书,以及可能附带的代码示例。这本书旨在帮助读者提升使用LLDB(LLVM Debugger)进行...
软件破解经典Reverse Engineering of Object Oriented Code
Reverse Engineering of Object Oriented Code(Springer).rar
iOS App ReverseEngineering
**反向工程(Reverse Engineering)**是指通过分析一个已有的软件系统或产品来理解其工作原理和设计思想的过程。对于面向对象(OO)代码而言,反向工程的目标是提取出程序的设计结构、数据流以及控制流等关键信息,从而...
Anti-Reverse Engineering Guide Anti-debugging and anti-tracing techniques
In Advanced Apple Debugging and Reverse Engineering, you'll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but ...
Practical Reverse Engineering 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传...
"Secrets of Reverse Engineering"很可能是一份关于这一主题的深入学习资源,包含丰富的理论知识与实践技巧。 逆向工程的基本流程通常包括以下几个步骤: 1. 获取目标:这可能是一个可执行文件、库、固件或任何...
practical reverse engineering
In Advanced Apple Debugging and Reverse Engineering, you’ll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but...
RayWenderlich 出版的关於在apple 平台上开发除错及逆向工程的书 xcode 9, swift 4
Advanced Apple debugging & Reverse engineering 完整版加源码包
Introduction to Reverse Engineering Software.rar
The v2.0 release of Advanced Apple Debugging and Reverse Engineering brings everything up to the iOS 11 and Xcode 9.1 level.
Reversing Secrets of Reverse Engineering.rar
In Advanced Apple Debugging and Reverse Engineering, you’ll come to realize debugging is an enjoyable process to help you better understand software. Not only will you learn to find bugs faster, but...