-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Technology definitions
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------


--
-- Categories
--
CREATE TABLE tech.categories(
	name_id			INT NOT NULL PRIMARY KEY ,
	description_id	INT NOT NULL
);

CREATE INDEX idx_categories_description
	ON tech.categories (description_id);

ALTER TABLE tech.categories
	ADD CONSTRAINT fk_categories_name
		FOREIGN KEY (name_id) REFERENCES defs.strings ,
	ADD CONSTRAINT fk_categories_description
		FOREIGN KEY (description_id) REFERENCES defs.strings;


--
-- Technologies
--

CREATE TABLE tech.technologies(
	name_id			INT NOT NULL PRIMARY KEY ,
	description_id	INT NOT NULL ,
	category_id		INT NOT NULL ,
	points			INT NOT NULL CHECK( points > 0 ) ,
	cost			INT NOT NULL CHECK( cost > 0 )
);

CREATE INDEX idx_technologies_description
	ON tech.technologies (description_id);
CREATE INDEX idx_technologies_category
	ON tech.technologies (category_id);

ALTER TABLE tech.technologies
	ADD CONSTRAINT fk_technologies_category
		FOREIGN KEY (category_id) REFERENCES tech.categories ,
	ADD CONSTRAINT fk_technologies_name
		FOREIGN KEY (name_id) REFERENCES defs.strings ,
	ADD CONSTRAINT fk_technologies_description
		FOREIGN KEY (description_id) REFERENCES defs.strings;



--
-- Dependencies
--

CREATE TABLE tech.dependencies(
	technology_id	INT NOT NULL ,
	depends_on		INT NOT NULL ,
	PRIMARY KEY( technology_id , depends_on )
);

CREATE INDEX idx_dependencies_dependson
	ON tech.dependencies (depends_on);

ALTER TABLE tech.dependencies
	ADD CONSTRAINT fk_dependencies_technology
		FOREIGN KEY (technology_id) REFERENCES tech.technologies ,
	ADD CONSTRAINT fk_dependencies_dependson
		FOREIGN KEY (depends_on) REFERENCES tech.technologies;