【问题标题】:PL/SQL inherit rowtypePL/SQL 继承行类型
【发布时间】:2014-08-08 05:55:28
【问题描述】:

我知道 Oracle PL/SQL 允许使用“under”关键字创建对象子类型。但是是否可以创建一个继承特定表的 RowType 的子类型?

例如,如果有一个名为 Customer 的表,我想创建一个对象类型,该对象类型包含 Customer 表中的所有属性以及额外的字段。

我试过了:

create or replace type t_sub_type as object under Customer%RowType (
  price NUMBER
);

当然,代码编译失败。那么有什么办法可以实现呢?

【问题讨论】:

  • 不,这是不可能的。 customer%rowtype 是记录(customer 是记录数据类型)不是对象。
  • 那么有什么建议可以创建一个包含表中所有属性和一些额外字段的对象类型吗?
  • 所有属性都需要明确指定。
  • 回到过去(90 年代初到中期)Knowledge Systems Corp. 的 Kyle Brown 和 Object People 的 Bruce Whitenack 提出了一系列模式来处理通过尝试将对象存储到一个简单的关系数据库中(即一个没有内置支持对象(例如 Oracle 的类型)或继承(例如 Postgresql 中的表继承)的数据库。他们称之为 Crossing Chasms,看起来论文是still available on the Knowledge Systems Corp website 。您可能想给他们一个通读。分享和享受。
  • Crossing Chasms的基础论文收录在《Pattern Languages of Program Design 2》一书中,即available from Amazon。

标签: oracle plsql


【解决方案1】:

除非customer 恰好是一个对象表,否则不会。如果customer 是一个普通的旧表,customer%rowtype 是一个记录,而不是一个对象,所以它不能被继承。如果您创建了对象类型customer_typ 并创建了这些对象类型的表customer,那么您可以创建customer_typ 的子类型。类似的东西

SQL> ed
Wrote file afiedt.buf

  1  create type customer_typ
  2    as object (
  3      name varchar2(100),
  4      address varchar2(1000),
  5      phone varchar2(30)
  6    )
  7*   not final
SQL> /

Type created.

SQL> create table customer_tbl of customer_typ;

Table created.

SQL> create type subcustomer_typ
  2    under customer_typ (
  3      newColumn number
  4    );
  5  /

Type created.

SQL> desc subcustomer_typ;
 subcustomer_typ extends SCOTT.CUSTOMER_TYP
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 NAME                                               VARCHAR2(100)
 ADDRESS                                            VARCHAR2(1000)
 PHONE                                              VARCHAR2(30)
 NEWCOLUMN                                          NUMBER

当然,我很难想象这种情况会很有意义。但这是可能的。如果您能描述为什么要从 %rowtype 记录继承,我们或许可以建议一些替代方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2018-09-27
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多