CREATE TABLE CONTCTBLMAPA
(
   NRSEQCONTCTBLMAPA  NUMBER                        NOT NULL,
   CDIMPOSTO          VARCHAR2(02)                  NOT NULL,
   CDEMPRESA          VARCHAR2(02)                  NOT NULL,
   NRLANCCONT         VARCHAR2(12)                  NOT NULL,
   NRSEQLANC          VARCHAR2(05)                  NOT NULL,   
   CDCONTCTBL         VARCHAR2(20)                  NOT NULL,
   VRLANCAMENT        FLOAT                         NULL,
   IDFORMAPUR         VARCHAR2(01) DEFAULT 'C'      NOT NULL
      CONSTRAINT CKC_IDFORMAPUR_CONTCTBLMAPA CHECK (IDFORMAPUR IN ('C','P')),
   IDFORMAPURITEM      VARCHAR2(01) DEFAULT 'C'      NOT NULL
      CONSTRAINT CKC_IDFORMAPURITEM_CONTCTBLMAP CHECK (IDFORMAPURITEM IN ('C','P')),
   IDTPLANCAMENTO     VARCHAR2(01) DEFAULT 'R'      NOT NULL
      CONSTRAINT CKC_IDTPLANCAMENTO_CONTCTBLMAP CHECK (IDTPLANCAMENTO IN ('R','D','A','E','C')), 
   NRSEQUMAPAPU       VARCHAR2(10)                  NULL,
   IDORIGEM           VARCHAR2(20)                  NULL,
   NRORG              NUMBER       DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') 	NOT NULL,
   IDATIVO            VARCHAR2(1)  DEFAULT 'S'      NOT NULL
      CONSTRAINT CKC_IDATIVO_CONTCTBLMAPA    CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO         DATE         DEFAULT  	NULL,
   DTULTATU           DATE         DEFAULT  	NULL,
   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' 			NULL,
   CONSTRAINT PK_CONTCTBLMAPA PRIMARY KEY (NRSEQCONTCTBLMAPA)
)
/

ALTER TABLE CONTCTBLMAPA
  ADD CONSTRAINT FKFIS00808IMPOSTONFE FOREIGN KEY (CDIMPOSTO)
       REFERENCES IMPOSTONFE (CDIMPOSTONFE)
/

CREATE INDEX FIS00808_FK ON CONTCTBLMAPA (CDIMPOSTO)
/

ALTER TABLE CONTCTBLMAPA
  ADD CONSTRAINT FKFIS00809LANCCONT FOREIGN KEY (CDEMPRESA, NRLANCCONT, NRSEQLANC)
       REFERENCES LANCCONT (CDEMPRESA, NRLANCCONT, NRSEQLANC)
/

CREATE INDEX FIS00809_FK ON CONTCTBLMAPA (CDEMPRESA, NRLANCCONT, NRSEQLANC)
/


ALTER TABLE CONTCTBLMAPA
  ADD CONSTRAINT FKFIS00810CONTCTBL FOREIGN KEY (CDCONTCTBL)
       REFERENCES CONTCTBL (CDCONTCTBL)
/

CREATE INDEX FIS00810_FK ON CONTCTBLMAPA (CDCONTCTBL)
/

ALTER TABLE CONTCTBLMAPA
  ADD CONSTRAINT FKFIS00811MAPAAPURAC FOREIGN KEY (NRSEQUMAPAPU)
       REFERENCES MAPAAPURAC (NRSEQUMAPAPU) ON DELETE CASCADE
/

CREATE INDEX FIS00811_FK ON CONTCTBLMAPA (NRSEQUMAPAPU)
/

CREATE TABLE RELACNFCONTCTBL
(
   NRSEQRELACNFCONT   NUMBER                        NOT NULL,
   NRSEQCONTCTBLMAPA  NUMBER                        NOT NULL,
   CDFILIAL           VARCHAR2(10)                  NOT NULL,
   NRLANCTONF         VARCHAR2(10)                  NOT NULL,
   NRORG              NUMBER       DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') 	NOT NULL,
   IDATIVO            VARCHAR2(1)  DEFAULT 'S'      NOT NULL
      CONSTRAINT CKC_IDATIVO_RELACNFCONTCTBL    CHECK (IDATIVO IN ('S','N')),
   DTINCLUSAO         DATE         DEFAULT  	NULL,
   DTULTATU           DATE         DEFAULT  	NULL,
   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' 			NULL,
   CONSTRAINT PK_RELACNFCONTCTBL PRIMARY KEY (NRSEQRELACNFCONT)
)
/

ALTER TABLE RELACNFCONTCTBL
  ADD CONSTRAINT FKFIS00812CONTCTBLMAPA FOREIGN KEY (NRSEQCONTCTBLMAPA)
       REFERENCES CONTCTBLMAPA (NRSEQCONTCTBLMAPA) ON DELETE CASCADE
/

CREATE INDEX FIS00812_FK ON RELACNFCONTCTBL (NRSEQCONTCTBLMAPA)
/

ALTER TABLE RELACNFCONTCTBL
  ADD CONSTRAINT FKFIS00813NOTAFISCAL FOREIGN KEY (CDFILIAL, NRLANCTONF)
       REFERENCES NOTAFISCAL (CDFILIAL, NRLANCTONF) ON DELETE CASCADE
/

CREATE INDEX FIS00813_FK ON RELACNFCONTCTBL (CDFILIAL, NRLANCTONF)
/