Oracle SQL 在嵌套 table 中创建标识列
Oracle SQL create identity column in nested table
我正在制作一个包含嵌套 table:
的 table
create DOCUMENT as OBJECT (
DOC_ID NUMBER,
DESCRIPTION VARCHAR(1000));
create type documents_t is table of DOCUMENT;
create table projects (
ID NUMBER GENERATED ALWAYS AS IDENTITY ,
DOCUMENTS documents_t)
NESTED TABLE DOCUMENTS STORE AS documents_nested(
(PRIMARY KEY(nested_table_id, DOC_ID)) ORGANIZATION INDEX);
这工作正常,但我似乎无法找到如何将嵌套 table 的主键作为标识列。有什么建议么?
谢谢
请找到代码片段,
CREATE SEQUENCE seq_documents
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
CREATE OR REPLACE TYPE documents_q AS OBJECT
(
doc_id NUMBER,
description VARCHAR2(1000),
CONSTRUCTOR FUNCTION documents_q(p_description VARCHAR2) RETURN SELF AS RESULT
);
CREATE OR REPLACE TYPE BODY documents_q AS
CONSTRUCTOR FUNCTION documents_q(p_description VARCHAR2) RETURN SELF AS RESULT IS
BEGIN
self.doc_id := seq_documents.nextval;
self.description := p_description;
RETURN;
END;
END;
/
CREATE TYPE documents_t AS TABLE OF documents_q;
create table projects (
id NUMBER GENERATED ALWAYS AS IDENTITY ,
documents documents_t)
NESTED TABLE documents STORE AS documents_nested(
(PRIMARY KEY(nested_table_id, doc_id)) ORGANIZATION INDEX);
INSERT INTO PROJECTS(documents) VALUES (documents_t(documents_q('Description One'),documents_q('Description Two')));
SELECT * FROM projects;
如果它能为您提供解决方案,请告诉我。谢谢
如果你想要另一个没有构造函数的解决方案(因为使用构造函数会增加更多维护)
我采纳了@Sujitmohanty30 的想法并在插入语句中使用了序列。它使代码更易于维护,但强制您在插入时使用序列并在每个查询中插入一项(因为“sequence.nextval”不会在同一查询中递增):
CREATE SEQUENCE documents_seq NOCACHE;
/
CREATE TYPE document_type AS OBJECT (
doc_id NUMBER,
description VARCHAR2(1000)
);
/
CREATE TYPE documents AS TABLE OF document_type;
/
create table projects_docs (
id NUMBER GENERATED ALWAYS AS IDENTITY ,
docs documents)
NESTED TABLE docs STORE AS docs_nested(
(PRIMARY KEY(nested_table_id, doc_id)) ORGANIZATION INDEX);
/
INSERT INTO projects_docs (docs) VALUES(
documents(document_type(documents_seq.nextval, 'doc')));
/
select p.id, d.* from projects_docs p, table(p.docs) d;
我正在制作一个包含嵌套 table:
的 tablecreate DOCUMENT as OBJECT (
DOC_ID NUMBER,
DESCRIPTION VARCHAR(1000));
create type documents_t is table of DOCUMENT;
create table projects (
ID NUMBER GENERATED ALWAYS AS IDENTITY ,
DOCUMENTS documents_t)
NESTED TABLE DOCUMENTS STORE AS documents_nested(
(PRIMARY KEY(nested_table_id, DOC_ID)) ORGANIZATION INDEX);
这工作正常,但我似乎无法找到如何将嵌套 table 的主键作为标识列。有什么建议么? 谢谢
请找到代码片段,
CREATE SEQUENCE seq_documents
START WITH 1
INCREMENT BY 1
NOCACHE
NOCYCLE;
CREATE OR REPLACE TYPE documents_q AS OBJECT
(
doc_id NUMBER,
description VARCHAR2(1000),
CONSTRUCTOR FUNCTION documents_q(p_description VARCHAR2) RETURN SELF AS RESULT
);
CREATE OR REPLACE TYPE BODY documents_q AS
CONSTRUCTOR FUNCTION documents_q(p_description VARCHAR2) RETURN SELF AS RESULT IS
BEGIN
self.doc_id := seq_documents.nextval;
self.description := p_description;
RETURN;
END;
END;
/
CREATE TYPE documents_t AS TABLE OF documents_q;
create table projects (
id NUMBER GENERATED ALWAYS AS IDENTITY ,
documents documents_t)
NESTED TABLE documents STORE AS documents_nested(
(PRIMARY KEY(nested_table_id, doc_id)) ORGANIZATION INDEX);
INSERT INTO PROJECTS(documents) VALUES (documents_t(documents_q('Description One'),documents_q('Description Two')));
SELECT * FROM projects;
如果它能为您提供解决方案,请告诉我。谢谢
如果你想要另一个没有构造函数的解决方案(因为使用构造函数会增加更多维护)
我采纳了@Sujitmohanty30 的想法并在插入语句中使用了序列。它使代码更易于维护,但强制您在插入时使用序列并在每个查询中插入一项(因为“sequence.nextval”不会在同一查询中递增):
CREATE SEQUENCE documents_seq NOCACHE;
/
CREATE TYPE document_type AS OBJECT (
doc_id NUMBER,
description VARCHAR2(1000)
);
/
CREATE TYPE documents AS TABLE OF document_type;
/
create table projects_docs (
id NUMBER GENERATED ALWAYS AS IDENTITY ,
docs documents)
NESTED TABLE docs STORE AS docs_nested(
(PRIMARY KEY(nested_table_id, doc_id)) ORGANIZATION INDEX);
/
INSERT INTO projects_docs (docs) VALUES(
documents(document_type(documents_seq.nextval, 'doc')));
/
select p.id, d.* from projects_docs p, table(p.docs) d;