CREATE SCHEMA IF NOT EXISTS BASIC;

CREATE TABLE BASIC.ATTRIBUTE_GROUP (
	ID SERIAL PRIMARY KEY,
	GROUP_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	GROUP_NAME VARCHAR(64) NOT NULL DEFAULT '',
	IS_REQUIRED SMALLINT NOT NULL DEFAULT -1,
	
	UNIQUE (GROUP_TYPE)
);

CREATE TABLE BASIC.DATA_TYPE (
	ID SERIAL PRIMARY KEY,
	DATA_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	DATABASE_TYPE VARCHAR(32) NOT NULL DEFAULT '',
	
	UNIQUE (DATA_TYPE)
);

CREATE TABLE BASIC.MODEL_TYPE (
	ID SERIAL PRIMARY KEY,
	MODEL_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	MODEL_NAME VARCHAR(64) NOT NULL DEFAULT '',
	REMARK VARCHAR(128) NOT NULL DEFAULT '',
	
	UNIQUE (MODEL_TYPE)
);

CREATE TABLE BASIC.ATTRIBUTE (
	ID SERIAL PRIMARY KEY,
	ATTRIBUTE_GROUP VARCHAR(64) NOT NULL DEFAULT '',
	ATTRIBUTE VARCHAR(128) NOT NULL DEFAULT '',
	ATTRIBUTE_NAME VARCHAR(64) NOT NULL DEFAULT '',
	DATA_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	LENGTH_PRECISION INTEGER NOT NULL DEFAULT -1,
	SCALE INTEGER NOT NULL DEFAULT -1,
	IS_NOT_NULL SMALLINT NOT NULL DEFAULT -1,
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT ',',
	MODEL_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	
	UNIQUE (ATTRIBUTE_GROUP, ATTRIBUTE),
    UNIQUE (ATTRIBUTE, ATTRIBUTE_NAME),
	
	FOREIGN KEY (ATTRIBUTE_GROUP) REFERENCES BASIC.ATTRIBUTE_GROUP (GROUP_TYPE),
	FOREIGN KEY (DATA_TYPE) REFERENCES BASIC.DATA_TYPE (DATA_TYPE),
	FOREIGN KEY (MODEL_TYPE) REFERENCES BASIC.MODEL_TYPE (MODEL_TYPE)
);

CREATE TABLE BASIC.ATTRIBUTE_TEMPLATE (
	ID SERIAL PRIMARY KEY,
	TEMPLATE_NAME VARCHAR(64) NOT NULL DEFAULT '',
	ATTRIBUTE_GROUP VARCHAR(64) NOT NULL DEFAULT '',
	DATA_TYPE VARCHAR(64) NOT NULL DEFAULT '',
	LENGTH_PRECISION INTEGER NOT NULL DEFAULT -1,
	SCALE INTEGER NOT NULL DEFAULT -1,
	IS_NOT_NULL SMALLINT NOT NULL DEFAULT -1,
	DEFAULT_VALUE VARCHAR(64) NOT NULL DEFAULT '',
	VALUE_RANGE VARCHAR(128) NOT NULL DEFAULT '',
	
	FOREIGN KEY (ATTRIBUTE_GROUP) REFERENCES BASIC.ATTRIBUTE_GROUP (GROUP_TYPE),
	FOREIGN KEY (DATA_TYPE) REFERENCES BASIC.DATA_TYPE (DATA_TYPE)
);

CREATE TABLE BASIC.MODEL_CONNECTIVITY (
	ID SERIAL PRIMARY KEY,
	FROM_MODEL VARCHAR(64) NOT NULL DEFAULT '',
	TO_MODEL VARCHAR(64) NOT NULL DEFAULT '',
	CONNECTIVITY SMALLINT NOT NULL DEFAULT -1
);