CREATE TABLE ITSUBPRODMAN
   (CDFILIAL     	  VARCHAR2(04) NOT NULL,
    NRSEQSUBPROD 	  VARCHAR2(10) NOT NULL,
	NRLANCTONF   	  VARCHAR2(10) NOT NULL,
	NRSEQITPROD  	  VARCHAR2(04) NOT NULL,
	CDPRODUTO    	  VARCHAR2(10) NOT NULL,
    CDSUBPRODUTO 	  VARCHAR2(10) NOT NULL,
    CDFILIMOVI  	  VARCHAR2(04) NOT NULL,
 	CDALMOXARIFE	  VARCHAR2(06),
	CDLOCALESTOQ 	  VARCHAR2(06),
    NRLOTEESTQ   	  VARCHAR2(20),
    NRSUBLOTE    	  VARCHAR2(04),
    QTITLANCTO   	  FLOAT,
	NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    NRORGINCLUSAO     NUMBER          DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg')		NULL,
    CDOPERINCLUSAO    VARCHAR2(12)    DEFAULT '1' 					NULL,
    NRORGULTATU       NUMBER          DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg')		NULL,
    CDOPERULTATU      VARCHAR2(12)    DEFAULT '1',
		 CONSTRAINT CKC_IDATIVO_ITSUBPRODMAN CHECK (IDATIVO IN ('S','N')),
		 CONSTRAINT PK_ITSUBPRODMAN PRIMARY KEY (CDFILIAL, NRSEQSUBPROD)
   )
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01012 FOREIGN KEY  (CDFILIAL, NRLANCTONF, NRSEQITPROD)
       REFERENCES ITEMPRODUTO (CDFILIAL, NRLANCTONF, NRSEQITPROD) ON DELETE CASCADE
/

CREATE INDEX CDP01012_FK ON ITSUBPRODMAN (CDFILIAL ASC, NRLANCTONF ASC, NRSEQITPROD ASC)
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01013PROD FOREIGN KEY  (CDPRODUTO)
       REFERENCES PRODUTO (CDPRODUTO)
/

CREATE INDEX CDP01013_FK ON ITSUBPRODMAN (CDPRODUTO ASC)
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01014PRODSUB FOREIGN KEY  (CDSUBPRODUTO)
       REFERENCES PRODUTO (CDPRODUTO)
/

CREATE INDEX CDP01014_FK ON ITSUBPRODMAN (CDSUBPRODUTO ASC)
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01015LMOXARI FOREIGN KEY  (CDFILIMOVI, CDALMOXARIFE)
       REFERENCES ALMOXARIFADO (CDFILIAL, CDALMOXARIFE)
/

CREATE INDEX CDP01015_FK ON ITSUBPRODMAN (CDFILIMOVI ASC, CDALMOXARIFE ASC)
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01016LOCALEST FOREIGN KEY  (CDFILIMOVI, CDALMOXARIFE, CDLOCALESTOQ)
       REFERENCES LOCALESTOQUE (CDFILIAL, CDALMOXARIFE, CDLOCALESTOQ)
/

CREATE INDEX CDP01016_FK ON ITSUBPRODMAN (CDFILIMOVI ASC, CDALMOXARIFE ASC, CDLOCALESTOQ ASC)
/

ALTER TABLE ITSUBPRODMAN
    ADD CONSTRAINT FKCDP01017LOTE FOREIGN KEY  (CDFILIMOVI, CDSUBPRODUTO, NRLOTEESTQ, NRSUBLOTE)
       REFERENCES LOTE (CDFILIAL, CDPRODUTO, NRLOTEESTQ, NRSUBLOTE)
/

CREATE INDEX CDP01017_FK ON ITSUBPRODMAN (CDFILIMOVI ASC, CDSUBPRODUTO ASC, NRLOTEESTQ ASC, NRSUBLOTE ASC)
/

CREATE INDEX ITSUBPRODMAN_01 ON ITSUBPRODMAN (CDFILIAL ASC, NRLANCTONF ASC, NRSEQITPROD ASC, CDPRODUTO ASC, CDSUBPRODUTO ASC, CDFILIMOVI ASC, CDALMOXARIFE ASC, CDLOCALESTOQ ASC)
/