CREATE TABLE TBSPED (
    ID INTEGER NOT NULL,
    ESTABELECIMENTO_ID INTEGER,
    DATA_GERACAO TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    MES INTEGER,
    ANO INTEGER,
    USUARIO_ID INTEGER,
    TIPO_SPED INTEGER,
    STATUS INTEGER);

ALTER TABLE TBSPED
ADD CONSTRAINT PK_TBSPED
PRIMARY KEY (ID);

COMMENT ON COLUMN TBSPED.TIPO_SPED IS
'1 - SPED FISCAL;
2 - SPED CONTRIBUICOES;';

COMMENT ON COLUMN TBSPED.STATUS IS
'0 - NAO UTILIZADO;
1 - UTILIZADO;';

CREATE SEQUENCE GTBSPED;

create trigger ttbsped_b01i for tbsped
active before insert position 0
as
begin
  if (new.id is null) then
    new.id = gen_id(gtbsped,1);
end;


CREATE INDEX ITBSPED_01
ON TBSPED (ESTABELECIMENTO_ID);

CREATE INDEX ITBSPED_02
ON TBSPED (DATA_GERACAO);

CREATE INDEX ITBSPED_03
ON TBSPED (MES);

CREATE INDEX ITBSPED_04
ON TBSPED (ANO);

CREATE INDEX ITBSPED_05
ON TBSPED (USUARIO_ID);

CREATE INDEX ITBSPED_06
ON TBSPED (TIPO_SPED);

CREATE INDEX ITBSPED_07
ON TBSPED (STATUS);

