



									  ┌─────────────────────┐
									  │ project_information │
									  └─────────────────────┘




  ┌────────────┐   1:1   ┌─────────────────────┐   M:1   ┌────────────┐   M:M   ┌─────────────────────┐
  │ attributes ├─────────┤ attribute_relations ├─────────┤ objects    ├─────────┤ object_relations    │
  └────────────┘         └─────────────────────┘         └─────┬──────┘         └─────────────────────┘
                                                               │
                                                               │
                                                               │  1:M       ┌────────────┐
                                                               └────────────┤ features   │
                                                                            └────────────┘
																			
																																																					

This file describes the data structure needed to be able to recreate the database that the CSV files represent. 
The diagram above describes graphically how the tables are related (use font Lucida Console) and the SQL code below can be used to set up the database. 
Depending on the database manager you are using, the code may need to be adjusted. 
This SQL code is tested in PostgreSQL and in SQLite. The constraints at the end of the code do not 
work in SQLite and are not needed to recreate the database, but can give an overview of 
how the relationships between the tables look.


CREATE TABLE "objects" (
  "IntrasisId" INTEGER,
  "object_id" INTEGER PRIMARY KEY NOT NULL,
  "Name" TEXT,
  "Class" TEXT,
  "SubClass" TEXT,
  "Color" INTEGER,
  "geometry_count" INTEGER,
  "attributes" TEXT
  );
  

CREATE TABLE "features" ( 
	"IntrasisId" INTEGER,
	"object_id" INTEGER,
	"Name" TEXT,
	"Class" TEXT,
	"SubClass" TEXT,
	"SymbolId" INTEGER, 
	"GeoObjectId" INTEGER PRIMARY KEY NOT NULL, 
	"spatial_type" TEXT,
	"geom" TEXT
	);

Relationships from the table features
/*
features.object_id - M:1 - objects.object_id
*/

CREATE TABLE "attributes" ( 
	"attribute_id" INTEGER PRIMARY KEY  NOT NULL, 
	"object_id" INTEGER, 
	"attribute_label" TEXT, 
	"attribute_value" TEXT, 
	"attribute_unit" TEXT, 
	"class" INTEGER, 
	"data_type" TEXT
	);
	

CREATE TABLE "attribute_relations" ( 
    "id" INTEGER PRIMARY KEY  NOT NULL,
	"related_id" INTEGER,
    "base_id" INTEGER
	);
/*
Relationships from the table attribute_relations
attribute_relations.base_id - M:1 - objects.object_id
attribute_relations.related_id - 1:1 - attributes.attribute_id
*/

CREATE TABLE object_relations (
    "id" INTEGER PRIMARY KEY  NOT NULL,
    "base_id" INTEGER,
    "related_id" INTEGER,
    "base_text" TEXT,
    "related_text" TEXT
);
/*
Relationships from the table object_relations
object_relations.base_id - M:1 - objects.object_id
object_relations.related_id - M:1 - objects.object_id
*/


CREATE TABLE "project_information" ( 
	"SiteId" TEXT, 
	"Plats" TEXT, 
	"Lst Dnr" TEXT, 
	"Raä Dnr" TEXT, 
	"Inventarienummer" TEXT, 
	"Projektkod" TEXT, 
	"Projektnamn" TEXT, 
	"Projektledare" TEXT, 
	"Uppdragsgivare" TEXT, 
	"Exploateringstyp" TEXT, 
	"Undersökningstyp" TEXT, 
	"Slutdatum" TEXT, 
	"Beskrivning" TEXT,
	"geom" TEXT
);

/*Optional CONSTRAINT */
ALTER TABLE IF EXISTS "features"
    ADD CONSTRAINT fk_object_features FOREIGN KEY ("object_id")
    REFERENCES "objects" ("object_id") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;
	
ALTER TABLE IF EXISTS "attribute_relations"
    ADD CONSTRAINT fk_object_attributes FOREIGN KEY ("related_id")
    REFERENCES "objects" ("object_id") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;
	
ALTER TABLE IF EXISTS "attribute_relations"
    ADD CONSTRAINT fk_attributes_attribute_relations FOREIGN KEY ("related_id")
    REFERENCES "attributes" ("attribute_id") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;
	
ALTER TABLE IF EXISTS "object_relations"
    ADD CONSTRAINT fk_object_base FOREIGN KEY ("base_id")
    REFERENCES "objects" ("object_id") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;
	
ALTER TABLE IF EXISTS "object_relations"
    ADD CONSTRAINT fk_object_related FOREIGN KEY ("related_id")
    REFERENCES "objects" ("object_id") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE NO ACTION;