Search code examples
sqloracle-databaseindexingobject-typesubtyping

Creating index on subtype-specific attribute in a SQL Object table


I created a table of the object type 'Document'. This table contains multiple sub-types of Document (example: Recipe, Publication, Contract). These sub-types all contain common attributes (id, title, file size) but often contain additional attributes (i.e. stock number). I created indexes on the commonly searched common attributes, but also would like to create indexes on commonly searched sub-type specific attributes.

For example, I created an index for Title of the Documents table:

CREATE INDEX i_title
ON Documents (Title);

I would like to do something similar to the following:

CREATE INDEX i_stock_number
ON DOCUMENTS d (Stock_Number) WHERE VALUE(d) IS OF TYPE(Publication);

or possibly

CREATE INDEX i_stock_number
ON DOCUMENTS (TREAT(DOCUMENTS AS Publication).Stock_Number);

Could you help me determine how to create the indexes for sub-type specific attributes?

Thank you for your time.


Solution

  • create index i_stock_number
    on documents d
    (treat(value(d) as publication).stock_number);
    

    Assuming a data model like this:

    create or replace type document is object
    (
        id number,
        title varchar2(100),
        file_size number
    ) not final;
    
    create or replace type publication under document
    (
        stock_number number
    );
    
    create table documents of document;
    
    insert into documents values(publication(1, 'title', 100, 200));
    commit;