CREATE TABLE WORKFLOW (
  NRWORKFLOW NUMBER(10) NOT NULL, 
  CDOPERADOR VARCHAR2(12) NOT NULL, 
  NRPROCESSOAPROVA NUMBER(10) NOT NULL, 
  NRITEMPROCAPROVA NUMBER(10) NOT NULL, 
  DSMOTIVOWFLOW VARCHAR2(2000) NOT NULL, 
  IDTIPOPERWFLOW VARCHAR2(1) DEFAULT '1' NOT NULL, 
  IDSITWORKFLOW VARCHAR2(1) DEFAULT '1' NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 NOT NULL, 
  DTINCLUSAO DATE NOT NULL, 
  DTULTATU DATE NOT NULL, 
  NRORGINCLUSAO NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERINCLUSAO VARCHAR2(12) NOT NULL, 
  NRORGULTATU NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERULTATU VARCHAR2(12) NOT NULL, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL, 
  NMITEMWFLOW VARCHAR2(2000) NOT NULL,
  CONSTRAINT PK_WORKFLOW PRIMARY KEY(NRWORKFLOW, NRORG)
)
/
ALTER TABLE WORKFLOW ADD CONSTRAINT FKCOC00302OPERADOR FOREIGN KEY (CDOPERADOR, NRORG) REFERENCES OPERADOR (CDOPERADOR, NRORG)
/
ALTER TABLE WORKFLOW ADD CONSTRAINT CKC_IDATIVO_WORKFLOW CHECK (IDATIVO in ('S','N'))
/
ALTER TABLE WORKFLOW ADD CONSTRAINT CKC_IDTIPOPERWFLOW_WORKFLOW CHECK (IDTIPOPERWFLOW in ('1','2','3'))
/
ALTER TABLE WORKFLOW ADD CONSTRAINT CKC_IDSITWORKFLOW_WORKFLOW CHECK (IDSITWORKFLOW in ('1','2','3'))
/
CREATE INDEX COC00302_FK ON WORKFLOW (CDOPERADOR, NRORG)
/
CREATE TABLE DADOSWFLOW (
  NRWORKFLOW NUMBER(10) NOT NULL, 
  NMTABELAWFLOW VARCHAR2(50) NOT NULL, 
  NMCAMPOWFLOW VARCHAR2(50) NOT NULL, 
  DSINFORANTERIOR VARCHAR2(2000) NOT NULL, 
  DSINFORATUAL VARCHAR2(2000) NOT NULL, 
  DSPRIMARYKEY VARCHAR2(2000) NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 NOT NULL, 
  DTINCLUSAO DATE NOT NULL, 
  DTULTATU DATE NOT NULL, 
  NRORGINCLUSAO NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERINCLUSAO VARCHAR2(12) NOT NULL, 
  NRORGULTATU NUMBER(10) DEFAULT 1 NOT NULL, 
  CDOPERULTATU VARCHAR2(12) NOT NULL, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL,
  CONSTRAINT PK_DADOSWFLOW PRIMARY KEY(NRWORKFLOW, NMTABELAWFLOW, NMCAMPOWFLOW, NRORG)
)
/
ALTER TABLE DADOSWFLOW ADD CONSTRAINT FKCOC00303WORKFLOW FOREIGN KEY (NRWORKFLOW, NRORG) REFERENCES WORKFLOW (NRWORKFLOW, NRORG)
/
ALTER TABLE DADOSWFLOW ADD CONSTRAINT CKC_IDATIVO_DADOSWFLOW CHECK (IDATIVO in ('S','N'))
/
CREATE TABLE PROCESSOAPROVA (
  NRPROCESSOAPROVA NUMBER(10) NOT NULL, 
  NMPROCESSOAPROVA VARCHAR2(200) NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 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, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL,
  CONSTRAINT PK_PROCESSOAPROVA PRIMARY KEY(NRPROCESSOAPROVA, NRORG)
)
/
ALTER TABLE PROCESSOAPROVA ADD CONSTRAINT CKC_IDATIVO_PROCESSOAPROVA CHECK (IDATIVO in ('S','N'))
/
CREATE TABLE ITEMPROCAPROVA (
  NRPROCESSOAPROVA NUMBER(10) NOT NULL, 
  NRITEMPROCAPROVA NUMBER(10) NOT NULL, 
  NMITEMPROCAPROVA VARCHAR2(200) NOT NULL, 
  NRDIASPROCAPROVA NUMBER(10) DEFAULT 10 NOT NULL, 
  NRPTOPROCAPROVA DOUBLE PRECISION DEFAULT '10' NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 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, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL,
  CONSTRAINT PK_ITEMPROCAPROVA PRIMARY KEY(NRPROCESSOAPROVA, NRITEMPROCAPROVA, NRORG)
)
/
ALTER TABLE ITEMPROCAPROVA ADD CONSTRAINT FKCOC00304PROCESSOAPROVA FOREIGN KEY (NRPROCESSOAPROVA, NRORG) REFERENCES PROCESSOAPROVA (NRPROCESSOAPROVA, NRORG)
/
ALTER TABLE ITEMPROCAPROVA ADD CONSTRAINT CKC_IDATIVO_ITEMPROCAPROVA CHECK (IDATIVO in ('S','N'))
/
create index COC00301_FK on WORKFLOW (NRPROCESSOAPROVA asc, NRITEMPROCAPROVA asc, NRORG ASC)
/
alter table WORKFLOW
    add constraint FKCOC00301ITEMPROCAPROVA foreign key  (NRPROCESSOAPROVA, NRITEMPROCAPROVA, NRORG)
       references ITEMPROCAPROVA (NRPROCESSOAPROVA, NRITEMPROCAPROVA, NRORG)
/
CREATE TABLE OPERWORKFLOW (
  NRWORKFLOW NUMBER(10) NOT NULL, 
  CDOPERADOR VARCHAR2(12) NOT NULL, 
  IDSITOPERWFLOW VARCHAR2(1) DEFAULT 'P' NOT NULL, 
  DTACAOWFLOW DATE DEFAULT NULL NULL, 
  DSMOTIVOACAO VARCHAR2(2000) DEFAULT NULL NULL, 
  DTLIMITEWFLOW DATE NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 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, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL,
  CONSTRAINT PK_OPERWORKFLOW PRIMARY KEY(NRWORKFLOW, CDOPERADOR, NRORG)
)
/
ALTER TABLE OPERWORKFLOW ADD CONSTRAINT FKCOC00305WORKFLOW FOREIGN KEY (NRWORKFLOW, NRORG) REFERENCES WORKFLOW (NRWORKFLOW, NRORG)
/
ALTER TABLE OPERWORKFLOW ADD CONSTRAINT FKCOC00306OPERADOR FOREIGN KEY (CDOPERADOR, NRORG) REFERENCES OPERADOR (CDOPERADOR, NRORG)
/
ALTER TABLE OPERWORKFLOW ADD CONSTRAINT CKC_IDSITOPERWFLOW_OPERWORK CHECK (IDSITOPERWFLOW in ('P','A','C','N'))
/
ALTER TABLE OPERWORKFLOW ADD CONSTRAINT CKC_IDATIVO_OPERWORKFLOW CHECK (IDATIVO in ('S','N'))
/
CREATE INDEX FKCOC00306_FK ON OPERWORKFLOW (CDOPERADOR, NRORG)
/
CREATE TABLE OPERITEMPAPROVA (
  NRPROCESSOAPROVA NUMBER(10) NOT NULL, 
  NRITEMPROCAPROVA NUMBER(10) NOT NULL, 
  CDOPERADOR VARCHAR2(12) NOT NULL, 
  NRDIASOPERAPRO NUMBER(10) DEFAULT 10 NOT NULL, 
  IDTIPOACAOOPER VARCHAR2(1) DEFAULT 'A' NOT NULL, 
  NRPTOACAOOPER DOUBLE PRECISION NOT NULL, 
  NRORG NUMBER(10) DEFAULT 1 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, 
  IDATIVO VARCHAR2(1) DEFAULT 'S' NOT NULL,
  CONSTRAINT PK_OPERITEMPAPROVA PRIMARY KEY(NRPROCESSOAPROVA, NRITEMPROCAPROVA, CDOPERADOR, NRORG)
)
/
ALTER TABLE OPERITEMPAPROVA ADD CONSTRAINT FKCOC00307ITEMPROCAPROVA FOREIGN KEY (NRPROCESSOAPROVA, NRITEMPROCAPROVA, NRORG) REFERENCES ITEMPROCAPROVA (NRPROCESSOAPROVA, NRITEMPROCAPROVA, NRORG)
/
ALTER TABLE OPERITEMPAPROVA ADD CONSTRAINT FKCOC00308OPERADOR FOREIGN KEY (CDOPERADOR, NRORG) REFERENCES OPERADOR (CDOPERADOR, NRORG)
/
ALTER TABLE OPERITEMPAPROVA ADD CONSTRAINT CKC_IDTIPOACAOOPER_OPERITEMPA CHECK (IDTIPOACAOOPER in ('A','C'))
/
ALTER TABLE OPERITEMPAPROVA ADD CONSTRAINT CKC_IDATIVO_OPERITEMPAPROVA CHECK (IDATIVO in ('S','N'))
/
CREATE INDEX FKCOC00308_FK ON OPERITEMPAPROVA (CDOPERADOR, NRORG)