CREATE TABLE HISTALTERADFE(
   CDPROCESSO        	VARCHAR2(03)  		NOT NULL,
   CDNOTATECNICA     	VARCHAR2(07)  		NOT NULL,
   CDVERSAONT        	VARCHAR2(04)  		NOT NULL,
   DSHISTORICO       	VARCHAR2(255) 		NOT NULL,
   DTINIVIGENCIAPROD 	DATE          		NOT NULL,
   DTINIVIGENCIAHOMO 	DATE          		NOT NULL,
   NRORG                       	NUMBER 			DEFAULT 	 SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
   IDATIVO                    	VARCHAR2(1)     		DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_HISTALTERADFE CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO             	DATE                   		NULL,
   DTULTATU                  	DATE                  		NULL,
   NRORGINCLUSAO      	NUMBER            		DEFAULT 1 NULL,
   CDOPERINCLUSAO    	VARCHAR2(12)  		DEFAULT '1' NULL,
   NRORGULTATU          	NUMBER             		DEFAULT 1 NULL,
   CDOPERULTATU        	VARCHAR2(12) 		DEFAULT '1',
	CONSTRAINT PK_HISTALTERADFE PRIMARY KEY (CDPROCESSO, CDNOTATECNICA, CDVERSAONT)
)
/
CREATE TABLE DETHISTALTERADFE(
   CDPROCESSO        	VARCHAR2(03)  		NOT NULL,
   CDNOTATECNICA     	VARCHAR2(07)  		NOT NULL,
   CDVERSAONT        	VARCHAR2(04)  		NOT NULL,
   CDITEMND          		VARCHAR2(10)  		NOT NULL,
   DSHISTORICO       	VARCHAR2(255) 		NOT NULL,
   SGESTADOAPLICA    	VARCHAR2(255) 		NOT NULL,
   DTINIVIGENCIAPROD 	DATE          		NOT NULL,
   DTINIVIGENCIAHOMO 	DATE          		NOT NULL,
   NRORG                       	NUMBER 			DEFAULT 	 SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
   IDATIVO                    	VARCHAR2(1)     		DEFAULT 'S' NOT NULL
	CONSTRAINT CKC_IDATIVO_DETHISTALTERADFE CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO             	DATE                   		NULL,
   DTULTATU                  	DATE                  		NULL,
   NRORGINCLUSAO      	NUMBER            		DEFAULT 1 NULL,
   CDOPERINCLUSAO    	VARCHAR2(12)  		DEFAULT '1' NULL,
   NRORGULTATU          	NUMBER             		DEFAULT 1 NULL,
   CDOPERULTATU        	VARCHAR2(12) 		DEFAULT '1',
   CONSTRAINT PK_DETHISTALTERADFE PRIMARY KEY (CDPROCESSO, CDNOTATECNICA, CDVERSAONT, CDITEMND)
)
/
CREATE INDEX FK_FIS00742 ON DETHISTALTERADFE (CDPROCESSO ASC, CDNOTATECNICA ASC, CDVERSAONT ASC)
/
ALTER TABLE DETHISTALTERADFE
   ADD CONSTRAINT FKFIS00742HISTALTERADFE  FOREIGN KEY (CDPROCESSO, CDNOTATECNICA, CDVERSAONT)
      REFERENCES HISTALTERADFE (CDPROCESSO, CDNOTATECNICA, CDVERSAONT) ON DELETE CASCADE
/
BEGIN
   INSERT INTO HISTALTERADFE
      (CDPROCESSO, 
       CDNOTATECNICA, 
       CDVERSAONT, 
       DSHISTORICO, 
       DTINIVIGENCIAPROD,
       DTINIVIGENCIAHOMO)
   VALUES
      ('NF', 
       '2018005', 
       '1.20', 
       'Alteração de leiaute da NF-e/NFC-e', 
       '07/05/2019',
       '25/02/2019');
       
   INSERT INTO DETHISTALTERADFE
      (CDPROCESSO,
       CDNOTATECNICA,
       CDVERSAONT,
       CDITEMND,
       DSHISTORICO,
       SGESTADOAPLICA,
       DTINIVIGENCIAPROD,
       DTINIVIGENCIAHOMO)
   VALUES
      ('NF',
       '2018005',
       '1.20',
       '3.7',
       'Grupo ZD. Informações do Responsável Técnico',
       '_AC_AP_BA_CE_DF_ES_GO_MA_MT_MG_PA_PB_PI_RR_RO_RJ_RN_RS_SP_SE_',
       '07/05/2019',
       '25/02/2019');
   COMMIT;
END;