CREATE TABLE HISTORICOMYMEALS (
    NRHISTORICOMYMEALS  NUMBER          NOT NULL,
    CDOPERADOR          VARCHAR2(12)    NOT NULL,
    CDFILIAL            VARCHAR2(4)     NOT NULL,
    CDSERVFILI          VARCHAR2(5)     NOT NULL,
    DTCARDAPIO          DATE            NOT NULL,
    DTHISTORICO         DATE            NOT NULL,
    QTCOMENSAIS         FLOAT           NOT NULL,
    IDSITUACAO          VARCHAR2(30)    CHECK (IDSITUACAO IN ('PENDENTE', 'APROVADO', 'REPROVADO')),
    DSOBSERVACAO        VARCHAR2(255)   NULL,
    DSJUSTIFICATIVA     VARCHAR2(255)   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 CKC_IDATIVO_HISTORICOMYMEALS CHECK (IDATIVO IN ('S','N')),
		CONSTRAINT PK_HISTORICOMYMEALS PRIMARY KEY (NRHISTORICOMYMEALS)
)
/

CREATE SEQUENCE SEQ_HISTORICOMYMEALS INCREMENT BY 1 MINVALUE 1
/

ALTER TABLE HISTORICOMYMEALS
   ADD CONSTRAINT FKPLA01340OPERADOR FOREIGN KEY (CDOPERADOR)
      REFERENCES OPERADOR (CDOPERADOR)
/
CREATE INDEX PLA01340_FK ON HISTORICOMYMEALS (CDOPERADOR)
/

CREATE INDEX IDXHISTORICOMYMEALS01 ON HISTORICOMYMEALS(CDOPERADOR, CDFILIAL, CDSERVFILI, DTCARDAPIO, IDSITUACAO)
/

CREATE INDEX IDXHISTORICOMYMEALS02 ON HISTORICOMYMEALS(CDOPERADOR, DTHISTORICO)
/

ALTER TABLE CONFIRMAREFE ADD NRHISTORICOMYMEALS NUMBER NULL
/

ALTER TABLE CONFIRMAREFE
   ADD CONSTRAINT FKPLA01341HISTORICOMYMEALS FOREIGN KEY (NRHISTORICOMYMEALS)
      REFERENCES HISTORICOMYMEALS (NRHISTORICOMYMEALS)
/

CREATE INDEX PLA01341_FK ON CONFIRMAREFE (NRHISTORICOMYMEALS)
/