oracle“创建或替换类型”和“创建或替换类型”有什么区别?和“类型 type_name 是...”句法
我是 pl/sql 新手。现在我有一个关于oracle类型的问题。我看到有两种类型的类型:
CREATE OR REPLACE TYPE "TYPE_NAME1" AS OBJECT
(
temp_trans_id number(10),
trans_id number(10),
resion_id number(10)
)
或者
type new_type_name is record(
column1 number,
column2 varchar2(50)
);
variable_name new_type_name;
有什么区别?非常感谢。
i'm a pl/sql newbie. now i have a question about oracle type. i saw there are two types of type :
CREATE OR REPLACE TYPE "TYPE_NAME1" AS OBJECT
(
temp_trans_id number(10),
trans_id number(10),
resion_id number(10)
)
or
type new_type_name is record(
column1 number,
column2 varchar2(50)
);
variable_name new_type_name;
what's the difference? great thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了之前的回答之外,声明的范围也存在显着差异。
当您在 PL/SQL 中声明类型(例如示例中的记录类型)时,它只能在 PL/SQL 中使用。如果它是在过程或函数本地声明的,则它只能在该子程序中使用;如果它在包体中声明,则只能在该包内使用;如果它在包头中声明,则任何对包具有执行访问权限的 PL/SQL 代码都可以使用它。无论如何都不能在 SQL 语句中引用它,即使是嵌入在 PL/SQL 代码中的语句。
当您创建对象类型或其他架构级类型定义(例如嵌套表)时,它可以在 SQL 和 PL/SQL 中使用。作为一个非常简单的示例,您可以将表定义基于对象定义:
请参阅此处,获取有关 CREATE TYPE 语句的文档以及有关对象类型的各种用途的进一步文档的参考。
In addition to the previous answer, there are significant differences in the scope of the declaration.
When you declare a type in PL/SQL, such as the record type in your example, it is only usable from PL/SQL. If it is declared locally to a procedure or function, then it can only be used within that subprogram; if it is declared in a package body it can only be used within that package; if it is declared in a package header it can be used by any PL/SQL code that has execute access to the package. In no way can it be referenced in SQL statements, even ones embedded in the PL/SQL code.
When you create an object type, or other schema-level type definitions such as nested tables, it is usable within both SQL and PL/SQL. As a very simple example, you can base a table definition on the object definition:
See here for documentation on the CREATE TYPE statement and references to further documentation on the various uses of object types.
记录类型是一种可以像记录一样使用的类型。它有一组类型字段,但仅此而已。
对象类型则相当不同。它也有一组字段,但它也可以包含将在对象实例的上下文中运行的可执行方法(是的,您也可以拥有静态方法)。它类似于Java中的对象。
与我见过的其他面向对象系统的一些(但肯定不是全部)差异:
在您的示例中,
TYPE_NAME1
和new_type_name
看起来非常相似,因为对于对象类型 (TYPE_NAME1),您尚未利用任何特定于对象类型的内容。请参阅有关对象类型的 Oracle 页面更多信息。
A record type is a type that can be used like a record. It has a set of typed fields, but that's about it.
An Object Type is rather different. It also has a set of fields, but it can also contain executable methods that will act in the context of an instance of your object (and yes, you can also have static methods). It is similar to an Object in Java.
Some (but certainly not all) differences from other object-oriented systems I've seen:
In your example,
TYPE_NAME1
andnew_type_name
seem very similar because for the Object Type (TYPE_NAME1), you have not taken advantage of anything specific to Object Types.See the Oracle page on Object Types for more info.