CREATE TABLE TBMDF_NF (
    ID INTEGER NOT NULL,
    MDF_ID INTEGER NOT NULL,
    ORIGEM INTEGER NOT NULL,
    NF_ID INTEGER NOT NULL,
    DATAHORA TIMESTAMP DEFAULT 'NOW');


ALTER TABLE TBMDF_NF
ADD CONSTRAINT PK_TBMDF_NF
PRIMARY KEY (ID);

CREATE SEQUENCE GTBMDF_NF;

CREATE TRIGGER TBMDF_NF_B01I FOR TBMDF_NF
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBMDF_NF,1);
END;

CREATE INDEX TBMDF_NF_01
ON TBMDF_NF (MDF_ID);


CREATE INDEX TBMDF_NF_02
ON TBMDF_NF (NF_ID);

CREATE INDEX TBMDF_NF_03
ON TBMDF_NF (ORIGEM);

COMMENT ON COLUMN TBMDF_NF.ORIGEM IS
'0 - ENTRADA;
1 - SAÍDA;';