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