Firebird: Auto-increment / auto-value work-around

I like to use an integer for the primary key in many of my tables. I call the field ID usually.
In other database engines, I’ve set things up so that the field is filled in automatically for me (with the next available integer) whenever I create a new record.

So far, attempts to do that in Firebird have failed. And searches suggest that it isn’t easy. And I regard “not easy” “solutions” as risky, potentially unreliable. (Environment details below.)

So! Every table must have a primary key. What to do?

I’m afraid that after hours of trying to achieve a reliable auto-increment, I have thrown in the towel.

I set up my tables with a field called “ID”. I define it as holding type-integer data. I set that field as the table’s primary key.

Then, from time to time, I create, by hand, a bunch of otherwise empty records, each with a unique integer in the ID field. I then use those “shells” when I want to add a record to the database.

Tedious. But Gets The Job Done. Sigh.

(You can, of course, just enter all of each new record by hand, one at a time. But the method below gives you a way to “pre-fill” any fields that you wish with default values, too.)

===
For the “lazy”…

If you can’t stand the tedium of that “solution”, and have a taste for the arcane…

Build a simple spreadsheet with a line for each record. The spreadsheet, for the simplest version of this, will have just one column.

This technique would also serve, if you wanted to fill other fields at the same time as putting values in the new records that are being created on the fly. Just be sure to put something in the primary key’s field at the same time.


Here’s how…

A one column spreadsheet with the following would add new, otherwise blank, records with ID’s 1000, 1001, 1002, etc…

1000
1001
1002
1003
1004
1005
1006
1007
1008

(You must be careful not to create a second record with the same ID as that of a record that already exists.)

The values can be produced with formulae, by the way. (Put 1000 into cell A1 by hand, then put =A1+1 in A2. Then copy/paste A2 down the page. A3 will be =A2+1. A4 will be =A3+1, etc.

Select all the numbers.
Press ctrl-c, to copy what you have selected.

Open your database’s database’s main project manager window; get it displaying your tables.

Right-click on the table you want to add the records to.

Choose “Paste” from the pop-up menu.

A dialog will open (“Copy table”)

You want “APPEND DATA”, and you want to remove the tick in front of “Use first line as column names”.

Click the “Next” button.

On the second page, you’ll see two lists.

The first (“Source table”) indicates the columns in your spreadsheet, identified by the first value from each.

The second (“Destination table”) indicates the fields in the table you are appending the new records to.

Use the “up”/“down” arrows to “move” the field names of the table around, so that the top field in “destination” is where you want what’s in the top column-name in the “source”.

(For a simple just-add-new-records-with-suitable-values-for-primary-key task, as long as the table’s primary key is the first in the table, you won’t have to do any moving of things up or down.)

Don’t Worry when “nothing” happens when you do what’s next:

Click the “Create” button.

If “nothing” happens, be happy. Check your table. You should find the new records waiting there.

If you get an error message, you may have SOME of the records create. The error probably arose when the process met either a value that didn’t suit the data-type of the field you were trying to put it in, or you accidentally tried to create a record with an already-used value in the primary key field.

===
My environment: Windows 10 (2H22) / LO 7.1.1.2 / Embedded Firebird version? I don’t know how to find that!

it’s just another shortcoming in Firebird.
all is explained here: Release Notes.
.
since version 3.0 it’s been easy to create an auto value primary key when first creating the table using an sql statement or via Base ‘Create Table in Design View’
.
it’s impossible to add an auto value primary key to an existing table.
.
it’s even impossible to rename a table.

For get Firebird version, menu Tools->SQL…

SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION')
FROM rdb$database;

Thank you elmau… I’d hoped someone would respond to that challenge!

Alas, on my system, all I got was…

SQL Status: HY000
Error code: 1000
Syntax error in SQL statement

@MSPhobe,
I copied then pasted elmau’s code and it runs as expected on my system.
I have used the exact same code on several different versions of libreoffice over time without issue:
.
Version: 7.3.7.2 (x64) / LibreOffice Community
Build ID: e114eadc50a9ff8d8c8a0567d6da8f454beeb84f
CPU threads: 8; OS: Windows 10.0 Build 19045; UI render: Skia/Vulkan; VCL: win
Locale: en-GB (en_GB); UI: en-GB
Calc: CL
.
your workaround is impractical, not only does it violate the principles of normalisation but many tables will have fields which are set not null, have default values, or are foreign keys.
it’s a shame that a Firebird enthusiast has not come forward with a possible solution.
.
the only real solution is a functioning auto value primary key.
close the office suite and make a backup of your database.
.
1
I will reference your table as ‘A’.
load your database and select ‘menu:Tools > Relationships’.
delete all relationships which reference table ‘A’ one at a time e.g. delete one then save, delete next then save etc. until all are deleted then save the database.
delete any views which reference table ‘A’.
.
2
hit ‘Create Table in Design View’
design a table in the image of table A
select the field ID then in ‘Field Properties’ set ‘AutoValue’ = yes
save as B
3
right click table A and select ‘copy’
right click table B and select ‘Paste’
Table name: B
Append data
hit Create
4
delete table A
5
drag table B and drop over white space
Table name: A (REMEMBER THIS IS YOUR ORIGINAL TABLES NAME)
Definition and data
hit Create
6
save database
7
reset any defaults, re-establish your relationships and recreate your views.
open table A and insert a record.
now you have a copy of your original table with a fully functioning AutoValue.
I found that the first value created was max(ID) + 1 but as the actual value is not critical there’s really no point in resetting it.
delete table B any time your ready.

Awesome. This worked for me. Took me a while to realise that I had to open the orginal table in edit view then control Click and copy the field names in original table to past just the heading over in your step 2, which only worked if the primary key was not set, (paste did not show up otherwise)

Also, I just copied and renamed the original table, deleted the orignal table and renamed the new created as the oringal table name, and it worked fine for me.

Thank you sooooo much.