Oracle - 为什么我应该使用包而不是独立的过程或函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12934984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Oracle - Why should I use packages instead of standalone procedures or functions
提问by Mikayil Abdullayev
I searched google but did not find any satisfying answer as to why I should use packages.
我搜索了谷歌,但没有找到关于为什么我应该使用包的任何令人满意的答案。
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.
我知道一个包是一组过程、函数和不同的变量。据我了解,它有点对应于 OOP 中的对象。但当然,没有什么比实例化包的不同实例更好的了,这样每个实例将具有不同的属性值并表现不同。
Then what is the advantage of using packages when I can just create a standalone procedure and call it independently?
那么当我可以创建一个独立的过程并独立调用它时,使用包有什么好处呢?
回答by APC
Packages provide the following advantages:
软件包具有以下优点:
- Cohesion: all the procedures and functions relating to a specfic sub-system are in one program unit. This is just good design practice but it's also easier to manage, e.g. in source control.
- Constants, sub-types and other useful things: there's more to PL/SQL than stored procedures. Anything we can define in a package spec can be shared with other programs, for instance user-defined exceptions.
- Overloading: the ability to define a procedure or function with the same name but different signatures.
- Security: defining private procedures in the package body which can only be used by the package because they aren't exposed in the specification.
- Sharing common code: another benefit of private procedures.
- We only need to grant EXECUTE on a package rather than on several procedures.
- 内聚性:与特定子系统相关的所有程序和功能都在一个程序单元中。这只是一个很好的设计实践,但它也更容易管理,例如在源代码控制中。
- 常量、子类型和其他有用的东西:PL/SQL 不仅仅是存储过程。我们可以在包规范中定义的任何内容都可以与其他程序共享,例如用户定义的异常。
- 重载:定义具有相同名称但不同签名的过程或函数的能力。
- 安全性:在包体中定义私有过程,这些过程只能被包使用,因为它们没有在规范中公开。
- 共享公共代码:私有程序的另一个好处。
- 我们只需要对一个包而不是几个过程授予 EXECUTE 权限。
回答by maialithar
As described in Oracle docs, packages are good because of:
如Oracle docs 中所述,包之所以好是因为:
- modularity
- easier application design
- information hiding
- added functionality
- better performance
- 模块化
- 更简单的应用程序设计
- 信息隐藏
- 附加功能
- 更好的性能
Details on each reason are explained in docs.
文档中解释了每个原因的详细信息。