-- ============================================================
--   TABLE: TPOPERLIVROMAG 
-- ============================================================
CREATE TABLE TPOPERLIVROMAG
(
    CDLIVROMAG      VARCHAR2(10)    NOT NULL,
    IDENTRSAIDOP    VARCHAR2(3)     NOT NULL,
    CDTIPOOPER      VARCHAR2(50)    NOT NULL,
    NRORG 			NUMBER 			DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO 		VARCHAR2(1) 	DEFAULT 'S' NOT NULL,
		CONSTRAINT CKC_IDATIVO_TPOPERLIVROMAG 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_TPOPERLIVROMAG PRIMARY KEY (CDLIVROMAG, IDENTRSAIDOP, CDTIPOOPER)
)
/

CREATE INDEX FIS00767_FK ON TPOPERLIVROMAG (CDLIVROMAG ASC)
/

ALTER TABLE TPOPERLIVROMAG
    ADD CONSTRAINT FKFIS00767LIVROMAGNETICO FOREIGN KEY (CDLIVROMAG)
       REFERENCES LIVROMAGNETICO (CDLIVROMAG)
/

CREATE INDEX FIS00768_FK ON TPOPERLIVROMAG (IDENTRSAIDOP ASC, CDTIPOOPER ASC)
/

ALTER TABLE TPOPERLIVROMAG
    ADD CONSTRAINT FKFIS00768TIPOOPERAC FOREIGN KEY (IDENTRSAIDOP, CDTIPOOPER)
       REFERENCES TIPOOPERAC (IDENTRSAIDOP, CDTIPOOPER)
/

-- ============================================================
--   TABLE: CFOPLIVROMAGTPOP 
-- ============================================================
CREATE TABLE CFOPLIVROMAGTPOP
(
    CDLIVROMAG     	VARCHAR2(10)    NOT NULL,
    IDENTRSAIDOP   	VARCHAR2(01)    NOT NULL,
    CDTIPOOPER    	VARCHAR2(02)    NOT NULL,
    CDCFOPLMAG     	VARCHAR2(04)    NOT NULL,
    DSCFOPLMAG    	VARCHAR2(250)   NOT NULL,
	NRORG 			NUMBER 			DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
	IDATIVO 		VARCHAR2(1) DEFAULT 'S' NOT NULL,
		CONSTRAINT CKC_IDATIVO_CFOPLIVMAGTPOP 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_CFOPLIVROMAGTPOP PRIMARY KEY (CDLIVROMAG, IDENTRSAIDOP, CDTIPOOPER, CDCFOPLMAG)
)
/

CREATE INDEX FIS00769_FK ON CFOPLIVROMAGTPOP (CDLIVROMAG ASC, IDENTRSAIDOP ASC, CDTIPOOPER ASC)
/

ALTER TABLE CFOPLIVROMAGTPOP
    ADD CONSTRAINT FKFIS00769TPOPERLIVROMAG FOREIGN KEY  (CDLIVROMAG, IDENTRSAIDOP, CDTIPOOPER)
       REFERENCES TPOPERLIVROMAG (CDLIVROMAG, IDENTRSAIDOP, CDTIPOOPER)
/

CREATE INDEX FIS00770_FK ON CFOPLIVROMAGTPOP (CDCFOPLMAG ASC)
/

ALTER TABLE CFOPLIVROMAGTPOP
    ADD CONSTRAINT FKFIS00770CFOPPADRAO FOREIGN KEY  (CDCFOPLMAG)
       REFERENCES CFOPPADRAO (CDCFOPPADRAO)