CREATE TABLE CUSTOHAB (
  CDHABILIDADE VARCHAR2(3) NOT NULL, 
  CDCUSTOHAB VARCHAR2(3) NOT NULL, 
  DSCUSTOHAB VARCHAR2(50) NOT NULL, 
  VRCUSTOHAB DOUBLE PRECISION DEFAULT NULL NULL, 
  CDCONTCTBLHAB VARCHAR2(20) DEFAULT NULL NULL, 
  NRORG NUMBER(10) DEFAULT 1 NOT NULL, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL, 
  DTINCLUSAO DATE NOT NULL, 
  DTULTATU DATE NOT NULL, 
  NRORGINCLUSAO NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERINCLUSAO VARCHAR2(12) DEFAULT '1' NOT NULL, 
  NRORGULTATU NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERULTATU VARCHAR2(12) DEFAULT '1' NOT NULL,
  CONSTRAINT PK_CUSTOHAB PRIMARY KEY(NRORG, CDHABILIDADE, CDCUSTOHAB)
)
/
ALTER TABLE CUSTOHAB ADD CONSTRAINT FKMAN02259CUSTOHAB FOREIGN KEY (CDHABILIDADE, NRORG) REFERENCES HABILIDADE (CDHABILIDADE, NRORG)
/
ALTER TABLE CUSTOHAB ADD CONSTRAINT FKMAN02260CUSTOHAB FOREIGN KEY (CDCONTCTBLHAB, NRORG) REFERENCES CONTCTBL (CDCONTCTBL, NRORG)
/
CREATE TABLE CUSTOHABOP (
  CDFILIAL VARCHAR2(4) DEFAULT NULL NULL, 
  NRORDPROD VARCHAR2(10) DEFAULT NULL NULL, 
  NRSUBORDPROD VARCHAR2(10) DEFAULT NULL NULL, 
  CDETAPAPROD VARCHAR2(10) DEFAULT NULL NULL, 
  NRAPRCUSTOP VARCHAR2(10) DEFAULT NULL NULL, 
  CDHABILIDADEOP VARCHAR2(3) DEFAULT NULL NULL, 
  CDCUSTOHABOP VARCHAR2(3) DEFAULT NULL NULL, 
  VRCUSTOHABOP DOUBLE PRECISION NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 NULL, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NULL, 
  DTINCLUSAO DATE NOT NULL, 
  DTULTATU DATE NOT NULL, 
  NRORGINCLUSAO NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERINCLUSAO VARCHAR2(12) DEFAULT '1' NOT NULL, 
  NRORGULTATU NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERULTATU VARCHAR2(12) DEFAULT '1' NOT NULL,
  CONSTRAINT PK_CUSTOHABOP PRIMARY KEY(CDCUSTOHABOP, CDFILIAL, NRORG, NRORDPROD, NRSUBORDPROD, CDETAPAPROD, NRAPRCUSTOP)
)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02261CUSTOHABOP FOREIGN KEY (CDFILIAL, NRORG) REFERENCES FILIAL (CDFILIAL, NRORG)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02262CUSTOHABOP FOREIGN KEY (CDFILIAL, NRORDPROD, NRSUBORDPROD, NRORG) REFERENCES ORDPRODUCAO (CDFILIAL, NRORDPROD, NRSUBORDPROD, NRORG)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02263CUSTOHABOP FOREIGN KEY (CDETAPAPROD, NRORG) REFERENCES ETAPAPROD (CDETAPAPROD, NRORG)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02264CUSTOHABOP FOREIGN KEY (CDFILIAL, NRORDPROD, NRSUBORDPROD, CDETAPAPROD, NRAPRCUSTOP, NRORG) REFERENCES APRCUSTOP (CDFILIAL, NRORDPROD, NRSUBORDPROD, CDETAPAPROD, NRAPRCUSTOP, NRORG)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02265CUSTOHABOP FOREIGN KEY (CDHABILIDADEOP, NRORG) REFERENCES HABILIDADE (CDHABILIDADE, NRORG)
/
ALTER TABLE CUSTOHABOP ADD CONSTRAINT FKMAN02266CUSTOHABOP FOREIGN KEY (NRORG, CDHABILIDADEOP, CDCUSTOHABOP) REFERENCES CUSTOHAB (NRORG, CDHABILIDADE, CDCUSTOHAB)