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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1671466/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:20:45  来源:igfitidea点击:

what is the difference between oracle "create or replace type" and "type type_name is..." syntax

sqloracleplsqltypes

提问by Just a learner

i'm a pl/sql newbie. now i have a question about oracle type. i saw there are two types of type:

我是 pl/sql 新手。现在我有一个关于 oracle类型的问题。我看到有两种类型

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.

有什么不同?万分感谢。

回答by Dave Costa

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.

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

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 和 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)

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

有关CREATE TYPE 语句的文档以及对有关对象类型的各种用途的进一步文档的引用,请参见此处

回答by FrustratedWithFormsDesigner

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:

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

  • No interfaces
  • No private methods
  • 无接口
  • 没有私有方法

In your example, TYPE_NAME1and new_type_nameseem 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 Typesfor more info.

在您的示例中,TYPE_NAME1并且new_type_name看起来非常相似,因为对于对象类型 (TYPE_NAME1),您没有利用特定于对象类型的任何内容。有关更多信息,请参阅有关对象类型的 Oracle 页面