DROP TABLE IF EXISTS datas;
DROP TABLE IF EXISTS refs;

CREATE TABLE refs (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
   ,ref_type STRING NOT NULL DEFAULT 'H'
   ,refcount INTEGER NOT NULL DEFAULT 1
   ,classname STRING
);

CREATE TABLE datas (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
   ,ref_id INTEGER NOT NULL
   ,data_type STRING DEFAULT 'S'
   ,`key` STRING NOT NULL
   ,value STRING
   ,FOREIGN KEY (ref_id) REFERENCES refs (id)
        ON DELETE CASCADE ON UPDATE CASCADE
   ,UNIQUE (ref_id, `key` )
);