我正在设计一个对象关系模型数据库。我有一个名为" topico_t"的超类型。这个超类型的4个子类型命名为" anotacao_t"," tarefa_t"," memo_t"和" contacto_t"。请参阅 DDL代码段:
CREATE OR REPLACE TYPE categoria_t AS OBJECT(
nome VARCHAR2(25),
pai REF categoria_t
);
CREATE OR REPLACE TYPE categoria_tab_t AS TABLE OF REF categoria_t;
CREATE OR REPLACE TYPE topico_t AS OBJECT(
titulo VARCHAR2(100),
ultimaAlteracao DATE,
categorias categoria_tab_t
--referencias topico_tab_t
) NOT FINAL;
CREATE OR REPLACE TYPE topico_tab_t AS TABLE OF REF topico_t;
ALTER TYPE topico_t ADD ATTRIBUTE referencias topico_tab_t CASCADE;
CREATE OR REPLACE TYPE periodo_t AS OBJECT(
inicio DATE,
fim DATE
);
CREATE OR REPLACE TYPE repeticao_t AS OBJECT(
frequencia VARCHAR2(10),
duracao periodo_t
);
CREATE OR REPLACE TYPE anotacao_t UNDER topico_t(
periodo periodo_t,
repeticao repeticao_t
);
CREATE OR REPLACE TYPE telefone_t AS OBJECT(
numero VARCHAR2(25)
);
CREATE OR REPLACE TYPE telefone_tab_t AS TABLE OF telefone_t;
CREATE OR REPLACE TYPE morada_t AS OBJECT(
rua VARCHAR2(100),
localidade VARCHAR2(50),
codigoPostal VARCHAR2(10)
);
CREATE OR REPLACE TYPE morada_tab_t AS TABLE OF morada_t;
CREATE OR REPLACE TYPE contacto_t UNDER topico_t(
telefones telefone_tab_t,
moradas morada_tab_t,
email VARCHAR2(100),
url VARCHAR2(150)
);
CREATE OR REPLACE TYPE tarefa_t UNDER topico_t(
dataFim DATE,
completo NUMBER(1,0),
conteudo VARCHAR2(255)
);
CREATE OR REPLACE TYPE memo_t UNDER topico_t(
conteudo VARCHAR2(255)
);
CREATE TABLE categorias OF categoria_t;
CREATE TABLE topicos OF topico_t
NESTED TABLE categorias STORE AS categorias_nested
NESTED TABLE referencias STORE AS referencias_nested;
所以,然后我填充这个表" topicos"有了这个:
INSERT INTO topicos VALUES (tarefa_t(
'Dissertacao',
TO_DATE('2018/02/13', 'YYYY/MM/DD'),
categoria_tab_t((select ref(c) from categorias c where nome='FEUP')),
topico_tab_t((select ref(t) from topicos t where titulo='Diogo Pereira'), -- which is an object of the subtype "contacto_t"
(select ref(t) from topicos t where titulo='Comecar a dissertacao'), -- which is an object of the subtype "memo_t"
(select ref(t) from topicos t where titulo='Apresentar Dissertacao'), -- which is an object of the subtype "tarefa_t"
(select ref(t) from topicos t where titulo='Reuniao com Orientador da Dissertacao')), -- which is an object of the subtype "anotacao_t"
TO_DATE('2018/08/13', 'YYYY/MM/DD'),
0,
'Dissertacao all over again'));
所以,我必须构建一个查询,甚至是一个PL / SQL块,它返回表格的所有行" topicos"在嵌套表" referencias"中至少包含上述4种子类型中的每一种的一个实例对象。理想情况下,此查询会返回该行(在上面的 INSERT 中提到)。
致以最诚挚的问候,并希望您有一个好工人的一天! ;)
答案 0 :(得分:1)
您的DDL代码有误。您正在创建基本类型topico_t
,然后在该类型下创建一些子类型,但您只是创建一个基本类型对象的表。这样做的问题是,您希望该表保存任何子类型的对象,但由于所谓的object slicing而不是这种情况。您创建的表格只包含与您为基本类型定义的字段相对应的列。
因此,您需要为最终实例化的所有子类型创建表。如果对要实例化的类型有意义,那么您应该只为基类型创建一个表,也就是说,如果不属于任何子类型的基类型对象可能存在。
执行此操作后,您尝试编写的查询应使用TREAT功能。请注意,根据documentation,当表达式不是您尝试强制转换的类型时,此函数会返回NULL
,因此您需要编写的查询应该像测试从嵌套表中选择并尝试强制转换为您创建的每个子类型时,是否至少有一行具有非空结果字段。检查文档中的示例以了解如何使用此功能。