INSERT,并获取自动递增的值(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) );

我会用这种方式插入一个条目。

insert into language(name) values ('value');

一个人如何知道创建id价值? 只是使用name字段做一个SELECT是无效的,因为可能有重复的条目。

Consider the following table:

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');

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

当从Java类(通过JDBC)执行此操作时,可以在使用approriate executeUpdate()方法“请求”它们之后使用getGeneratedKeys() 。

Through plain SQL:

insert into language(name) values ('value'); SELECT IDENTITY_VAL_LOCAL();

See the manual for details: http://db.apache.org/derby/docs/10.7/ref/rrefidentityvallocal.html

When doing this from a Java class (through JDBC) you can use getGeneratedKeys() after "requesting" them with the approriate executeUpdate() method.

更多推荐