IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('FK_TAB_DEEGREE_IDX_TAB_Quadtree') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE TAB_DEEGREE_IDX DROP CONSTRAINT FK_TAB_DEEGREE_IDX_TAB_Quadtree
;

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('FK_TAB_Quadtree_TAB_QTNODE') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE TAB_Quadtree DROP CONSTRAINT FK_TAB_Quadtree_TAB_QTNODE
;

IF EXISTS (SELECT * FROM dbo.SYSOBJECTS WHERE id = object_id('TAB_DEEGREE_IDX') AND  OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE TAB_DEEGREE_IDX
;

IF EXISTS (SELECT * FROM dbo.SYSOBJECTS WHERE id = object_id('TAB_Quadtree') AND  OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE TAB_Quadtree
;

CREATE TABLE TAB_DEEGREE_IDX ( 
	ID int identity(1,1)  NOT NULL,
	column_name varchar(50) NOT NULL,
	table_name varchar(50) NOT NULL,
	owner varchar(50) NOT NULL,
	index_name varchar(50) NOT NULL,
	FK_INDEXTREE int NOT NULL
) 
;

CREATE TABLE TAB_Quadtree ( 
	ID int identity(1,1)  NOT NULL,
	FK_ROOT varchar(150),
	DEPTH int,
	VERSION varchar(15)
) 
;


ALTER TABLE TAB_DEEGREE_IDX ADD CONSTRAINT PK_TAB_DEEGREE_IDX 
	PRIMARY KEY (ID) 
;

ALTER TABLE TAB_Quadtree ADD CONSTRAINT PK_TAB_Quadtree 
	PRIMARY KEY (ID) 
;


ALTER TABLE TAB_DEEGREE_IDX
ADD CONSTRAINT UQ_TAB_DEEGREE_IDX_id UNIQUE (ID)
;
ALTER TABLE TAB_DEEGREE_IDX
ADD CONSTRAINT UQ_TAB_DEEGREE_IDX_index_name UNIQUE (index_name)
;
ALTER TABLE TAB_Quadtree
ADD CONSTRAINT UQ_TAB_Quadtree_ID UNIQUE (ID)
;


ALTER TABLE TAB_DEEGREE_IDX ADD CONSTRAINT FK_TAB_DEEGREE_IDX_TAB_Quadtree 
	FOREIGN KEY (FK_INDEXTREE) REFERENCES TAB_Quadtree (ID)
;
