【问题标题】:Oracle SQL - Table Create IssueOracle SQL - 表创建问题
【发布时间】:2015-02-11 14:22:26
【问题描述】:

我目前正在尝试创建一个人员表,该表允许一个人成为客户或员工。目前这是我的代码:-

Create Or Replace Type Person As OBJECT
(
  person_id number(5) not null,
  firstname varchar2(15) not null,
  surname varchar2(15) not null,
  address1 varchar2(70) not null,
  address2 varchar2(70),
  address3 varchar2(70),
  postcode varchar2(9) not null
);
/

Create Or Replace Type Client Under Person
(
  marital_status varchar2(10),
  no_of_children number(2)
);
/

Create Or Replace Type Staff Under Person
(
  job_title varchar2(15) not null,
  salary number(4,2) not null,
  manager_id number(5) not null
);
/

Create Table Person Of Person
(
  person_id Primary Key
);

对象类型全部编译但是当它去创建表时我得到以下错误:-

SQL Error: ORA-00902: invalid datatype
00902. 00000 -  "invalid datatype"

导致此错误发生的原因以及可以采取哪些措施来纠正它。非常感谢任何帮助。

********编辑**********

我已将表的名称更改为 person_tbl,但仍然出现相同的错误。由于某种原因,这个错误现在出现在编译器日志中:-

Error: PL/SQL: Compilation unit analysis terminated
Error(1,18): PLS-00905: object [ConnectionName].PERSON is invalid

我不知道为什么它不让我使用 Person 类型,因为我之前已经成功尝试过这种方法。

【问题讨论】:

  • 你需要这个。 Create TYPE PersonTable as TABLE Of Person; 实际上,您无法创建具有类型的堆组织表。
  • @MaheswaranRavisankar:在 Oracle 中,您可以基于类型创建“对象表”。问题中的语法在答案中解决的问题之外是有效的。

标签: sql oracle sqldatatypes


【解决方案1】:

不知道,为什么会出现这些错误。你应该得到错误

00955. 00000 -  "name is already used by an existing object"

尝试创建与类型同名的表时。

试试

Create Table Persons Of Person
(
  person_id Primary Key
);

【讨论】:

    【解决方案2】:

    你有几个问题。正如@tonirush 所提到的,数据库中不能有多个同名对象。

    此外,person 类型在编译时出现错误(特别是 PLS-00218: a variable declared NOT NULL must have an initialization assignment)。在解决这些错误之前,您无法基于对象构建对象表。

    您的子类型也有编译错误:PLS-00590: attempting to create a subtype UNDER a FINAL type,但这与无法创建对象表无关。


    作为脚注,此答案(以及一般在 Oracle 数据库中)中的“对象”一词已超载。在第一段中,我说的是“数据库对象”,它几乎是使用create 命令在数据库中创建的任何东西。剩下的我说的是“对象类型”,它们是由create type ... object专门创建的对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多