Auto Increment in Firebird

It seems not to be an easy task in firebird as it is in MySQL.

So let's see how we do this step by step:

We assume that you have already created a table and appointed theoretically some field to function as the auto incremented index.

Connect to the database with FlameRobin GUI client, and double click on the appropriate table.
Click on "properties" of the field you want to be auto incremented.
Check the "Not null", select the "Create new generator" button and assign it a name.
Then proceed with the "Execute".

Now that we have a generator we need to create a trigger for our table, so that every time a new row is inserted into it, the relevant field will be auto incremented.

Create a new trigger and do it this way (replacing the generator, trigger, field and table names with yours):
SET TERM ^ ;
CREATE TRIGGER AUDIT_BI FOR AUDIT
BEFORE INSERT POSITION 0
AS
BEGIN
if (NEW.ID is NULL) then NEW.ID = GEN_ID(GEN_AUDIT_ID, 1);
END^
SET TERM ; ^

Execute this command

And commit the transaction.

That's it, Enjoy!

Post a Comment

Previous Post Next Post