更新时间:2023-11-28 22:47:04
在Firebird 3中,它很容易创建,因为它引入了标识列.实际上,它是语法糖,可以为您生成序列+触发器(如Firebird 2.5所示).
In Firebird 3 it is easy to create, as it introduced identity columns. In practice it is syntactic sugar for generating a sequence + trigger (as shown for Firebird 2.5) for you.
例如
create table t1 (
id integer generated by default as identity primary key
)
Firebird 3仅支持"generated by default
",这意味着用户能够指定自己的id值(这可能导致重复的值错误); "generated always
"将添加Firebird 4.
Firebird 3 only supports "generated by default
", which means users are able to specify their own id values (which might lead to duplicate value errors); "generated always
" will be added Firebird 4.
另请参见身份列类型"部分.
Firebird 2.5及更早版本没有自动递增字段.您需要使用序列(也称为生成器)和触发器自己创建它们.
Firebird 2.5 and earlier do not have auto-increment fields. You need to create them yourself with a sequence (aka generator) and a trigger.
序列是SQL标准术语,生成器是Firebird的历史术语,两者都被使用.
Sequence is the SQL standard term and generator is the historical Firebird term, they are both used.
要创建序列:
CREATE SEQUENCE t1_id_sequence;
创建触发器以始终使用主键ID
在表T1
上生成ID:
To create a trigger to always generate the id on a table T1
with primary key ID
:
set term !! ;
CREATE TRIGGER T1_AUTOINCREMENT FOR T1
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
NEW.ID = next value for t1_id_sequence;
END!!
set term ; !!
另请参见:如何创建自动增量列?
Flamerobin还提供了为您创建序列+触发器的工具.如果有现有表,则可以按照以下步骤操作:
Flamerobin also provides tooling to create a sequence + trigger for you. If you have an existing table, you can follow these steps:
打开表属性:
Open the table properties:
打开主键列的列属性
默认列属性,选择新生成器和创建触发器:
生成器(序列)和Flamerobin生成的触发代码.请注意,与我上面的示例相反,此触发器允许用户指定自己的id值,并带有一些逻辑以避免将来重复.执行此操作(不要忘记提交):
Generator (sequence) and trigger code generated by flamerobin. Note that contrary to my example above this trigger allows a user to specify their own id value, with some logic to avoid future duplicates. Execute this (and don't forget to commit):