使用Package的优点在于提供了必需的程序设计结构, 促进了模块化编程设计, 最重要的是Package断开了依赖链, 使得对某个数据库模式的改动不会导致整个模式的无效,从而避免了昂贵的重编译!
例如: 存在table t , procedure p1 p2, view v, function f, 它们之间引用关系如下:
select
name,type,referenced_name,referenced_type
from
user_dependencies
where referenced_owner = ' SCOTT '
order by name;
where referenced_owner = ' SCOTT '
order by name;
F FUNCTION T TABLE
P1 PROCEDURE V VIEW
P1 PROCEDURE V VIEW
P2 PROCEDURE P1 PROCEDURE
V VIEW T TABLE
V VIEW T TABLE
当alter table t add y number时, 依赖于t的所有对象(包括传递依赖的对象, 即p1依赖于t, p2依赖于p1)就全部无效, 需要重新编译(如果代码很复杂, 就会有较大的消耗)
select
user_objects.
object_name
,user_objects.object_type,user_objects.status
from
user_objects
F FUNCTION INVALID
P1 PROCEDURE INVALID
P1 PROCEDURE INVALID
P2 PROCEDURE INVALID
T TABLE VALID
V VIEW INVALID
T TABLE VALID
V VIEW INVALID
如果使用程序包之后table t , package pkg1,pkg2, view v, 可以发现PACKAGE BODY是依赖于PACKAGE, 而PKG2依赖于PKG1的PACKAGE, 而不是PACKAGE BODY
select
name,type,referenced_name,referenced_type
from
user_dependencies
where referenced_owner = ' SCOTT '
order by name;
where referenced_owner = ' SCOTT '
order by name;
PKG1 PACKAGE BODY V VIEW
PKG1 PACKAGE BODY PKG1 PACKAGE
PKG2 PACKAGE BODY PKG2 PACKAGE
PKG2 PACKAGE BODY PKG1 PACKAGE
V VIEW T TABLE
当alter table t add y number时,会发现PK1的PACKAGE BODY会无效, 但是PKG2的PACKAGE BODY是有效的
select
user_objects.
object_name
,user_objects.object_type,user_objects.status
from
user_objects
P1 PACKAGE BODY INVALID
P2 PACKAGE BODY VALID
P1 PACKAGE VALID
P2 PACKAGE VALID
T TABLE VALID
V VIEW INVALID
T TABLE VALID
V VIEW INVALID