table 个 REF 的范围

SCOPE for a table of REFs

我正在使用 Oracle (18.4.0) 设计对象关系模型,我想将 SCOPE 约束添加到对象 table 类型的列中 table .可能吗?这里是一个简化模型:

CREATE OR REPLACE TYPE t_cycler AS OBJECT (
    name VARCHAR2(50)
);

CREATE TABLE cycler OF t_cycler (
    name PRIMARY KEY
);


CREATE OR REPLACE TYPE t_cycler_list IS TABLE OF REF t_cycler;

CREATE OR REPLACE TYPE t_team AS OBJECT (
    name VARCHAR2(50),
    cyclers t_cycler_list
);

CREATE TABLE team OF t_team (
    name PRIMARY KEY
)
NESTED TABLE cyclers STORE AS cyclers_tab;

我需要 team.cyclers 只包含 REFcycler 中的对象。我查看了文档,但不幸的是它并没有说明很多关于 SCOPE 约束的信息,比如 here:

You can constrain a column type, collection element, or object type attribute to reference a specified object table. Use the SQL constraint subclause SCOPE IS when you declare the REF.

但它提供的唯一示例是一个简单的列类型。我尝试在 team table 的创建中以多种方式指定 SCOPE IS cycler 但没有结果。

您想将范围添加到嵌套 table 的 COLUMN_VALUE 伪列:

ALTER TABLE cyclers_tab ADD SCOPE FOR ( COLUMN_VALUE ) IS cycler;

如果你这样做:

INSERT INTO cycler ( name ) VALUES ( 'c1.1' );
INSERT INTO cycler ( name ) VALUES ( 'c1.2' );

INSERT INTO team (
  name,
  cyclers
) VALUES (
  'team1',
  t_cycler_list(
    ( SELECT REF(c) FROM cycler c WHERE name = 'c1.1' ),
    ( SELECT REF(c) FROM cycler c WHERE name = 'c1.2' )
  )
);

然后就可以插入行了。但是,如果您有另一个相同对象类型的 table:

CREATE TABLE cycler2 OF t_cycler (
    name PRIMARY KEY
);

INSERT INTO cycler2 ( name ) VALUES ( 'c2.1' );

并尝试做:

INSERT INTO team (
  name,
  cyclers
) VALUES (
  'team2',
  t_cycler_list(
    ( SELECT REF(c) FROM cycler2 c WHERE name = 'c2.1' )
  )
);

然后你得到错误:

ORA-22889: REF value does not point to scoped table

db<>fiddle here