在 Oracle 中创建嵌套 Table 时出错

Error While Creating Nested Table in Oracle

下面是我创建的 Tables。

CREATE TYPE ft_obj AS OBJECT (
    ftid         NUMBER(5),
    ftlocation   VARCHAR(30),
    country      VARCHAR(10)
);
/

CREATE TABLE ft_table OF ft_obj (
    ftid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/

CREATE TYPE frod_obj AS OBJECT (
    prodid           NUMBER(6),
    ft_ref           ft_obj,
    proddesc         VARCHAR(50),
    costperitem      DECIMAL,
    labcostperitem   DECIMAL
);
/

CREATE TABLE frod_table OF frod_obj (
    prodid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;

CREATE TYPE wf_obj AS OBJECT (
    wfid           NUMBER,
    ft_ref         ft_obj,
    wfname         VARCHAR(30),
    taxcode        INT,
    yearlyincome   DECIMAL,
    yearlytax      DECIMAL
);
/

CREATE TABLE wf_table OF wf_obj (
    wfid PRIMARY KEY
) OBJECT IDENTIFIER IS PRIMARY KEY;
/

CREATE TYPE wfusage_obj AS OBJECT (
    jobdate         DATE,
    jobhours        INT,
    jobhourlyrate   DECIMAL,
    jobposted       CHAR,
    wfid_ref        REF wf_obj
);
/

CREATE TYPE wfusage_nesttabtyp AS
    TABLE OF wfusage_obj;
/

CREATE TABLE wfusage_objtab OF wfusage_obj;
/

CREATE TYPE odetails_obj AS OBJECT (
    mfid           NUMBER,
    prodid_ref     REF frod_obj,
    quantity       INT,
    itemprice      DECIMAL,
    wfusage_ntab   wfusage_nesttabtyp
);
/

CREATE TYPE odetails_nesttabtyp AS
    TABLE OF odetails_obj;
/

CREATE TYPE prod_obj AS OBJECT (
    prodoid          NUMBER,
    odate            DATE,
    promisedate      DATE,
    completiondate   DATE,
    shipmentdate     DATE,
    status           VARCHAR(20),
    odetails_ntab    odetails_nesttabtyp
);
/

CREATE TABLE prod_objtab OF prod_obj (
    PRIMARY KEY ( prodoid )
) OBJECT IDENTIFIER IS PRIMARY KEY
NESTED TABLE odetails_ntab STORE AS oprod_ntab ( (
    PRIMARY KEY ( nested_table_id,
                  mfid )
)
ORGANIZATION INDEX
COMPRESS ) RETURN AS LOCATOR
/

ALTER TABLE oprod_ntab ADD (
    SCOPE FOR ( prodid_ref ) IS frod_table
);
/

创建嵌套 Table 时出现以下错误。

ORA-02320: failure in creating storage table for nested table column odetails_ntab ORA-25175: no PRIMARY KEY constraint found 02320. 00000 - "failure in creating storage table for nested table column %s" *Cause: An error occurred while creating the storage table for the specified nested table column. *Action: See the messages that follow for more details. If the situation they describe can be corrected, do so; otherwise contact Oracle Support.

    INSERT INTO prod_objtab  VALUES ( 45000,
    '12-April-2019',
    '01-MAy-2019',
    '01-MAy-2019',
    '01-MAy-2019',
    'COMPLETED',
    odetails_nesttabtyp()
);
INSERT INTO TABLE (SELECT pr.odetails_ntab  FROM prod_objtab pr WHERE pr.prodorderid = 45000 ) 
                    values (45001,(SELECT REF(pt) FROM frod_table pt WHERE pt.prodid = 10001 ),100,500,
                    wfusage_nesttabtyp(wfusage_obj('12-April-2019',60,100,'AME',
                    (SELECT REF(wf) FROM wf_table wf WHERE wf.wfid = 240))));

第 9 行出现错误 ORA-01401: 插入的值对于列

来说太大

ORA-02320: failure in creating storage table for nested table column odetails_ntab ORA-25175: no PRIMARY KEY constraint found 02320. 00000 - "failure in creating storage table for nested table column %s" *Cause: An error occurred while creating the storage table for the specified nested table column. *Action: See the messages that follow for more details. If the situation they describe can be corrected, do so; otherwise contact Oracle Support.

由于您已完成 Multi-level 嵌套,因此在创建 table 时,您还需要 2 层 Storage 以及 Nested table。请参阅下面的操作方法。

CREATE TABLE prod_objtab OF prod_obj (
    PRIMARY KEY ( prodoid )
) OBJECT IDENTIFIER IS PRIMARY KEY 
NESTED TABLE odetails_ntab STORE AS oprod_ntab ( ( PRIMARY KEY (NESTED_TABLE_ID, mfid )) 
ORGANIZATION INDEX COMPRESS
NESTED TABLE wfusage_ntab STORE AS  XX ) RETURN AS LOCATOR;

阅读更多 https://docs.oracle.com/en/database/oracle/oracle-database/18/adobj/multilevel-collection-types.html#GUID-76D5A6B0-28AD-483D-942C-B7F3B90AC379