How to store data with dynamic number of attributes in a database

前端 未结 8 2336
挽巷
挽巷 2020-12-04 13:14

I have a number of different objects with a varying number of attributes. Until now I have saved the data in XML files which easily allow for an ever changing number of attr

8条回答
  •  情歌与酒
    2020-12-04 13:16

    I used to implement this scheme:

    t_class (id RAW(16), parent RAW(16)) -- holds class hierachy.
    t_property (class RAW(16), property VARCHAR) -- holds class members.
    t_declaration (id RAW(16), class RAW(16)) -- hold GUIDs and types of all class instances
    t_instance (id RAW(16), class RAW(16), property VARCHAR2(100), textvalue VARCHAR2(200), intvalue INT, doublevalue DOUBLE, datevalue DATE) -- holds 'common' properties
    
    t_class1 (id RAW(16), amount DOUBLE, source RAW(16), destination RAW(16)) -- holds 'fast' properties for class1.
    t_class2 (id RAW(16), comment VARCHAR2(200)) -- holds 'fast' properties for class2
    --- etc.
    

    RAW(16) is where Oracle holds GUIDs

    If you want to select all properties for an object, you issue:

    SELECT  i.*
    FROM    (
            SELECT  id 
            FROM    t_class
            START WITH
                    id = (SELECT class FROM t_declaration WHERE id = :object_id)
            CONNECT BY
                    parent = PRIOR id
            ) c
    JOIN    property p
    ON      p.class = c.id
    LEFT JOIN
            t_instance i
    ON      i.id = :object_id
            AND i.class = p.class
            AND i.property = p.property
    

    t_property hold stuff you normally don't search on (like, text descriptions etc.)

    Fast properties are in fact normal tables you have in the database, to make the queries efficient. They hold values only for the instances of a certain class or its descendants. This is to avoid extra joins.

    You don't have to use fast tables and limit all your data to these four tables.

提交回复
热议问题