Oracle数据库-REF不支持的数据类型

时间:2020-04-30 13:16:05

标签: sql database oracle

因此,我有三个表,即Staff,Book和Sale。

员工:

 CREATE TYPE StaffType AS OBJECT (
     ID VARCHAR2(5),
     Name VARCHAR2(30)
     );
/
    CREATE TABLE Staff of StaffType;

对于图书:

CREATE TYPE BookType AS OBJECT (
 B_ID VARCHAR2(8));
/
 CREATE TABLE Book of TicketType;

待售:

CREATE TYPE SaleType AS OBJECT (
 S_ID VARCHAR2(8),
 S_Date VARCHAR2(10),
 Price NUMBER(10),
 With REF StaffType,
 For REF BookType );
/
CREATE TABLE Sale of SaleType (
 Scope for (With) is Staff,
 Scope for (For) is Book);

所以我这样在Sale表中插入了一行

insert into Sale VALUES(SaleType('S12','33th',22,(SELECT REF(c) FROM Staff c WHERE c.ID='C13'),(SELECT REF(t) FROM Book t WHERE t.B_ID='T18'));

但是,出现以下错误:

ORA-00917: missing comma 

我不明白为什么会收到此错误。.我错过了什么逗号? 编辑:我遵循@thatjeffsmith的解决方案,尽管插入语句有效,当我尝试查看表时,出现以下错误:

ORA-00932: inconsistent datatypes: expected CHAR got REF SQL_VTJOLYEBVKAOJEHORTCXVPTBP.STAFFTYPE

这是代码。.我删除了HR。在销售表声明中在Book and Staff之前添加,因为这导致工作表显示错误,表明Staff和Book在范围之外。这是代码:

CREATE TYPE STAFFTYPE AS OBJECT (
       ID    VARCHAR2(5),
       NAME  VARCHAR2(30)
);
/
CREATE TABLE STAFF OF STAFFTYPE;
/
INSERT INTO STAFF VALUES('C13','James');
/
CREATE TYPE BOOKTYPE AS OBJECT (
       B_ID VARCHAR2(8)
);
/
CREATE TABLE BOOK OF BOOKTYPE;
/
INSERT INTO BOOK VALUES('T18');
/
CREATE TYPE SALETYPE AS OBJECT (
       S_ID    VARCHAR2(8),
       S_DATE  VARCHAR2(10),
       PRICE   NUMBER(10),
       WITHS   REF STAFFTYPE,
       FORS    REF BOOKTYPE
);
/
CREATE TABLE SALE OF SALETYPE (
       SCOPE FOR ( WITHS ) IS STAFF,
       SCOPE FOR ( FORS ) IS BOOK
);
INSERT INTO SALE VALUES ( SALETYPE(
       'S12', '33th',
       22,
       (
              SELECT REF(C)
                FROM STAFF C
               WHERE C.ID = 'C13'
       ),
       (
              SELECT REF(T)
                FROM BOOK T
               WHERE T.B_ID = 'T18'
       )
) );

SELECT * FROM SALE;

1 个答案:

答案 0 :(得分:0)

WITH和FOR是关键字,不能作为表名或列名使用。您可以对这些进行“批注”,但不要那样做-这会使您的生活更加艰难。

您的BOOK表错误,您使用的示例类型不存在。

您的INSERT缺少最后一个')'

CREATE TYPE STAFFTYPE AS OBJECT (
       ID    VARCHAR2(5),
       NAME  VARCHAR2(30)
);
/
CREATE TABLE STAFF OF STAFFTYPE;
CREATE TYPE BOOKTYPE AS OBJECT (
       B_ID VARCHAR2(8)
);
/
CREATE TABLE BOOK OF BOOKTYPE;
CREATE TYPE SALETYPE AS OBJECT (
       S_ID    VARCHAR2(8),
       S_DATE  VARCHAR2(10),
       PRICE   NUMBER(10),
       WITHS   REF STAFFTYPE,
       FORS    REF BOOKTYPE
);
/
CREATE TABLE SALE OF SALETYPE (
       SCOPE FOR ( WITHS ) IS HR.STAFF,
       SCOPE FOR ( FORS ) IS HR.BOOK
);

INSERT INTO SALE VALUES ( SALETYPE(
       'S12', '33th',
       22,
       (
              SELECT REF(C)
                FROM STAFF C
               WHERE C.ID = 'C13'
       ),
       (
              SELECT REF(T)
                FROM BOOK T
               WHERE T.B_ID = 'T18'
       )
) );

enter image description here