CREATE TABLE PRODREPLICDOC
(
	CDFILIAL 		VARCHAR2(4) 	NOT NULL, 
	NRREPLICDOC 	VARCHAR2(4) 	NOT NULL,
	CDFILIORIGRD 	VARCHAR2(4) 	NOT NULL,
	CDFILIDESTRD 	VARCHAR2(4) 	NOT NULL,
	IDENTRSAIDOP 	VARCHAR2(1) 	NOT NULL
		CONSTRAINT CKC_IDENTRSAIDOP_PRODREPDOC CHECK (IDENTRSAIDOP IN ('E','S')),
	CDTIPOOPER 		VARCHAR2(2) 	NOT NULL,
	CDPRODORIGEM 	VARCHAR2(30),
	CDPRODDEST 		VARCHAR2(30),
    NRORG 				NUMBER DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO 			VARCHAR2(1) DEFAULT 'S' NOT NULL,
		CONSTRAINT CKC_IDATIVO_PRODREPLICDOC CHECK (IDATIVO IN ('S','N')),
	DTINCLUSAO 			DATE 				NULL,
	DTULTATU 			DATE 			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',
		CONSTRAINT PK_PRODREPLICDOC PRIMARY KEY (CDFILIAL, NRREPLICDOC, CDFILIORIGRD, CDFILIDESTRD, IDENTRSAIDOP, CDTIPOOPER, CDPRODORIGEM)
)
/

CREATE INDEX FIS00759_FK ON PRODREPLICDOC (CDFILIAL ASC, NRREPLICDOC ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00759REPLICDOC FOREIGN KEY (CDFILIAL, NRREPLICDOC)
		REFERENCES REPLICDOC (CDFILIAL, NRREPLICDOC) ON DELETE CASCADE
/

CREATE INDEX FIS00760_FK ON PRODREPLICDOC (CDFILIAL ASC, NRREPLICDOC ASC, CDFILIORIGRD ASC, CDFILIDESTRD ASC, IDENTRSAIDOP ASC, CDTIPOOPER ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00760ITREPLICDOC FOREIGN KEY (CDFILIAL, NRREPLICDOC, CDFILIORIGRD, CDFILIDESTRD, IDENTRSAIDOP, CDTIPOOPER)
		REFERENCES ITREPLICDOC (CDFILIAL, NRREPLICDOC, CDFILIORIGRD, CDFILIDESTRD, IDENTRSAIDOP, CDTIPOOPER) ON DELETE CASCADE
/

CREATE INDEX FIS00761_FK ON PRODREPLICDOC (CDFILIAL ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00761FILIAL FOREIGN KEY (CDFILIAL)
		REFERENCES FILIAL (CDFILIAL) 
/

CREATE INDEX FIS00762_FK ON PRODREPLICDOC (CDPRODORIGEM ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00762PRODREPLICIT FOREIGN KEY (CDPRODORIGEM)
		REFERENCES PRODUTO (CDPRODUTO) 
/

CREATE INDEX FIS00763_FK ON PRODREPLICDOC (CDPRODDEST ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00763PRODREPLICIT FOREIGN KEY (CDPRODDEST)
		REFERENCES PRODUTO (CDPRODUTO) 
/

CREATE INDEX FIS00764_FK ON PRODREPLICDOC (CDTIPOOPER ASC, IDENTRSAIDOP ASC)
/

ALTER TABLE PRODREPLICDOC
	ADD CONSTRAINT FKFIS00764PRODREPLICIT FOREIGN KEY (CDTIPOOPER, IDENTRSAIDOP)
		REFERENCES TIPOOPERAC (CDTIPOOPER, IDENTRSAIDOP) 
/

ALTER TABLE TRABREPLICDOC ADD NRREPLICDOC VARCHAR2(04)
/

ALTER TABLE TRABREPLICDOC ADD CDFILIALREPLIC VARCHAR2(04)