oracle“创建或替换类型”和“创建或替换类型”有什么区别?和“类型 type_name 是...”句法

发布于 2024-08-10 02:55:28 字数 463 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喜你已久 2024-08-17 02:55:28

除了之前的回答之外,声明的范围也存在显着差异。

当您在 PL/SQL 中声明类型(例如示例中的记录类型)时,它只能在 PL/SQL 中使用。如果它是在过程或函数本地声明的,则它只能在该子程序中使用;如果它在包体中声明,则只能在该包内使用;如果它在包头中声明,则任何对包具有执行访问权限的 PL/SQL 代码都可以使用它。无论如何都不能在 SQL 语句中引用它,即使是嵌入在 PL/SQL 代码中的语句

当您创建对象类型或其他架构级类型定义(例如嵌套表)时,它可以在 SQL 和 PL/SQL 中使用。作为一个非常简单的示例,您可以将表定义基于对象定义:

SQL> CREATE OR REPLACE TYPE "TYPE_NAME1" AS OBJECT
  2  (
  3      temp_trans_id           number(10),
  4      trans_id                number(10),
  5      resion_id               number(10)
  6  )
  7  /

Type created.

SQL> create table type_name1_tab of type_name1;

Table created.

SQL> desc type_name1_tab
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TEMP_TRANS_ID                                      NUMBER(10)
 TRANS_ID                                           NUMBER(10)
 RESION_ID                                          NUMBER(10)

请参阅此处,获取有关 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:

SQL> CREATE OR REPLACE TYPE "TYPE_NAME1" AS OBJECT
  2  (
  3      temp_trans_id           number(10),
  4      trans_id                number(10),
  5      resion_id               number(10)
  6  )
  7  /

Type created.

SQL> create table type_name1_tab of type_name1;

Table created.

SQL> desc type_name1_tab
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 TEMP_TRANS_ID                                      NUMBER(10)
 TRANS_ID                                           NUMBER(10)
 RESION_ID                                          NUMBER(10)

See here for documentation on the CREATE TYPE statement and references to further documentation on the various uses of object types.

青朷 2024-08-17 02:55:28

记录类型是一种可以像记录一样使用的类型。它有一组类型字段,但仅此而已。
对象类型则相当不同。它也有一组字段,但它也可以包含将在对象实例的上下文中运行的可执行方法(是的,您也可以拥有静态方法)。它类似于Java中的对象。
与我见过的其他面向对象系统的一些(但肯定不是全部)差异:

  • 没有接口
  • 没有私有方法

在您的示例中,TYPE_NAME1new_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:

  • No interfaces
  • No private methods

In your example, TYPE_NAME1 and new_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文