插入,并获得自动增加的值 [英] INSERT, and get the auto-incremented value
本文介绍了插入,并获得自动增加的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请考虑以下表:
create table language (
id integer generated always as identity (START WITH 1, INCREMENT BY 1),
name long varchar,
constraint language_pk primary key (id)
);
我将以此方式向其中插入条目。
To which I'd insert an entry this way.
insert into language(name) values ('value');
人们怎么知道 id
的值被创造了吗?仅使用 name
字段进行SELECT无效,因为可能存在重复的条目。
How does one know what value for id
was created? Just doing a SELECT using the name
field is not valid, because there can be duplicate entries.
推荐答案
通过普通SQL:
insert into language(name) values ('value');
SELECT IDENTITY_VAL_LOCAL();
有关详细信息,请参见手册: http://db.apache.org/derby/docs/10.7/ref/rrefidentityvallocal.html
See the manual for details: http://db.apache.org/derby/docs/10.7/ref/rrefidentityvallocal.html
从Java类(通过JDBC)执行此操作时,可以使用 getGeneratedKeys()后,请使用适当的 executeUpdate()方法。
When doing this from a Java class (through JDBC) you can use getGeneratedKeys() after "requesting" them with the approriate executeUpdate() method.
这篇关于插入,并获得自动增加的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文