ALTER TABLE PRECOBASE DROP CONSTRAINT CKC_IDULTPRECPROMO
/

ALTER TABLE PRECOBASE ADD CONSTRAINT CKC_IDULTPRECPROMO CHECK (IDULTPRECPROMO IN ('S','N', 'M'))
/

CREATE TABLE MARGEMITPREBASE
(
    CDPRECOBASE       VARCHAR2(2) 	  NOT NULL,
    DTINIVALIPB       DATE  	  	  NOT NULL,
    CDPRODUTO         VARCHAR2(10) 	  NOT NULL,
    NRSEQMARGEM       NUMBER		  NOT NULL,
    VRMARGEM          FLOAT  	  	  NULL, 
	NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    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_MARGEMITPRE PRIMARY KEY (CDPRECOBASE, DTINIVALIPB, CDPRODUTO, NRSEQMARGEM),
		CONSTRAINT FKPED00452PRECOB_MARGEMITPRBAS FOREIGN KEY  (CDPRECOBASE) REFERENCES PRECOBASE (CDPRECOBASE),
		CONSTRAINT FKPED00453ITEMPB_MARGEMITPRBAS FOREIGN KEY  (CDPRECOBASE, DTINIVALIPB, CDPRODUTO) 
			REFERENCES ITEMPREBASE (CDPRECOBASE, DTINIVALIPB, CDPRODUTO) ON DELETE CASCADE
)
/

CREATE INDEX PED00452_FK ON MARGEMITPREBASE (CDPRECOBASE ASC, DTINIVALIPB ASC, CDPRODUTO ASC)
/

CREATE INDEX PED00453_FK ON MARGEMITPREBASE (CDPRECOBASE ASC)
/