Oracle - 为什么我应该使用包而不是独立的过程或函数

2021-12-24 00:00:00 oracle database-design plsql

我搜索了谷歌,但没有找到任何关于为什么我应该使用包的令人满意的答案.

I searched google but did not find any satisfying answer as to why I should use packages.

我知道一个包是一组过程、函数和不同的变量.据我了解,它有点对应于 OOP 中的对象.但是当然,没有什么比实例化一个包的不同实例更好的了,这样每个实例将具有不同的属性值并表现出不同的行为.

I know that a package is a bundle of procedures, functions and different variables. As I understand it sort of corresponds to object in OOP. But of course there's nothing like instantiating different instances of a package so that each instance would have different property values and behave differently.

那么当我可以创建一个独立的过程并独立调用它时,使用包有什么好处?

Then what is the advantage of using packages when I can just create a standalone procedure and call it independently?

推荐答案

软件包具有以下优点:

  1. 内聚:与特定子系统相关的所有程序和功能都在一个程序单元中.这只是一个很好的设计实践,但它也更容易管理,例如在源代码控制中.
  2. 常量、子类型和其他有用的东西:PL/SQL 不仅仅是存储过程.我们可以在包规范中定义的任何内容都可以与其他程序共享,例如用户定义的异常.
  3. 重载:定义具有相同名称但不同签名的过程或函数的能力.
  4. 安全性:在包体中定义私有过程,只能由包使用,因为它们没有在规范中公开.
  5. 共享公共代码:私有程序的另一个好处.
  6. 我们只需要对一个包而不是几个过程授予 EXECUTE.

相关文章