--- ABS-12339
---------------   TARGETUSAGEPERBUDGETUNIT   ----------------------
CREATE SEQUENCE SEQ_TARGETUSAGEPERBUDGETUNIT
START WITH 1
INCREMENT BY 1;

CREATE TABLE TARGETUSAGEPERBUDGETUNIT
(
TARGETUSAGEPERBUDGETUNIT_ID NUMBER NOT NULL,
DEPARTMENT_LINK_ID NUMBER NOT NULL,
PRODUCT_ID NUMBER NOT NULL,
SIZEDEFINITION_ID NUMBER NOT NULL,
FINISHINGMETHOD_ID NUMBER NOT NULL,
BUDGETUNIT_ID NUMBER NOT NULL,
STARTDATE DATE NOT NULL,
ENDDATE DATE DEFAULT TO_DATE('31-12-2099','DD-MM-YYYY') NOT NULL,
TARGETUSAGE NUMBER(6,2) NOT NULL,
SYSTEMUSER_ID NUMBER NOT NULL,
TIMESTAMP DATE NOT NULL,
WEBUSER_ID NUMBER NULL
);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT XPKTARGETUSAGEPERBUDGETUNIT PRIMARY KEY (TARGETUSAGEPERBUDGETUNIT_ID);

CREATE UNIQUE INDEX XIETARGETUSAGEPERBUDGETUNIT ON TARGETUSAGEPERBUDGETUNIT (DEPARTMENT_LINK_ID ASC,
PRODUCT_ID ASC, SIZEDEFINITION_ID ASC, FINISHINGMETHOD_ID ASC, BUDGETUNIT_ID ASC, STARTDATE ASC);

CREATE INDEX XIF1TARGETUSAGEPERBUDGETUNIT ON TARGETUSAGEPERBUDGETUNIT (DEPARTMENT_LINK_ID ASC);

CREATE INDEX XIF2TARGETUSAGEPERBUDGETUNIT ON TARGETUSAGEPERBUDGETUNIT (PRODUCT_ID ASC);

CREATE INDEX XIF3TARGETUSAGEPERBUDGETUNIT ON TARGETUSAGEPERBUDGETUNIT (SIZEDEFINITION_ID ASC);

CREATE INDEX XIF4TARGETUSAGEPERBUDGETUNIT ON TARGETUSAGEPERBUDGETUNIT (BUDGETUNIT_ID ASC);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT TARGETUSAGEPERBUDGETUNIT_DEID FOREIGN KEY (DEPARTMENT_LINK_ID) REFERENCES DEPARTMENT_LINK (DEPARTMENT_LINK_ID);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT TARGETUSAGEPERBUDGETUNIT_PID FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCT (PRODUCT_ID);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT TARGETUSAGEPERBUDGETUNIT_SID FOREIGN KEY (SIZEDEFINITION_ID) REFERENCES SIZEDEFINITION (SIZEDEFINITION_ID);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT TARGETUSAGEPERBUDGETUNIT_FMID FOREIGN KEY (FINISHINGMETHOD_ID) REFERENCES FINISHINGMETHOD (FINISHINGMETHOD_ID);

ALTER TABLE TARGETUSAGEPERBUDGETUNIT ADD CONSTRAINT TARGETUSAGEPERBUDGETUNIT_BID FOREIGN KEY
(BUDGETUNIT_ID) REFERENCES BUDGETUNIT (BUDGETUNIT_ID);
