{$IFNOTEXISTS 
select *
from rdb$relation_fields f
where f.rdb$relation_name = 'WEB_MAGACIN_KOLICINE'
}

CREATE TABLE WEB_MAGACIN_KOLICINE (
    ID              INTEGER NOT NULL,
    SIFRA_MAGACINA  INTEGER,
    DATE_CHANGE     TIMESTAMP,
    RADNIK_CHANGE   INTEGER,
    AKTIVAN         INTEGER
);

commit work;

ALTER TABLE WEB_MAGACIN_KOLICINE ADD CONSTRAINT PK_WEB_MAGACIN_KOLICINE PRIMARY KEY (ID);
ALTER TABLE WEB_MAGACIN_KOLICINE ADD CONSTRAINT FK_WEB_MAGACIN_KOLICINE_MAGACIN FOREIGN KEY (SIFRA_MAGACINA) REFERENCES TBLMAGACIN (SIFRA_MAGACINA);
ALTER TABLE WEB_MAGACIN_KOLICINE ADD CONSTRAINT FK_WEB_MAGACIN_KOLICINE_RADNIK FOREIGN KEY (RADNIK_CHANGE) REFERENCES TBLRADNICI (SIFRA_RADNIKA);

commit work;

CREATE SEQUENCE GEN_WEB_MAGACIN_KOLICINE_ID;
ALTER SEQUENCE GEN_WEB_MAGACIN_KOLICINE_ID RESTART WITH 0;

commit work;

SET TERM ^ ;

create or alter procedure SP_GEN_WEB_MAGACIN_KOLICINE_ID
returns (
    ID integer)
as
begin
  id = gen_id(gen_web_magacin_kolicine_id, 1);
  suspend;
end^

SET TERM ; ^

commit work;

{$ENDIF}