CREATE TABLE TBNF_REFERENCIADA_AVULSA (
    ID INTEGER NOT NULL,
    NFE_ID INTEGER DEFAULT 0 NOT NULL,
    NFS_ID INTEGER DEFAULT 0 NOT NULL,
    CHAVE_ACESSO VARCHAR(44),
    DATAHORA TIMESTAMP DEFAULT 'NOW');

ALTER TABLE TBNF_REFERENCIADA_AVULSA
ADD CONSTRAINT PK_TBNF_REFERENCIADA_AVULSA
PRIMARY KEY (ID);

CREATE SEQUENCE GTBNF_REFERENCIADA_AVULSA;

SET TERM ^ ;

create trigger ttbnf_referenciada_avulsa_b01i for tbnf_referenciada_avulsa
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gtbnf_referenciada_avulsa,1);
end
^

SET TERM ; ^

CREATE INDEX ITBNF_REFERENCIADA_AVULSA_01
ON TBNF_REFERENCIADA_AVULSA (NFS_ID);

CREATE INDEX ITBNF_REFERENCIADA_AVULSA_02
ON TBNF_REFERENCIADA_AVULSA (CHAVE_ACESSO);

CREATE INDEX ITBNF_REFERENCIADA_AVULSA_03
ON TBNF_REFERENCIADA_AVULSA (NFE_ID);

