H2数据库中自动递增ID

问题描述 投票:0回答:6

有没有办法让表有一个自动递增的 BIGINT ID。 可以这样定义

id bigint auto_increment

但这没有任何效果(它不会自动增加)。 我想插入除 ID 字段之外的所有字段 - ID 字段应由 DBMS 提供。 或者我需要调用一些东西来增加 ID 计数器吗?

database auto-increment h2
6个回答
172
投票

这对我有用。 JDBC URL:

jdbc:h2:~/temp/test2

drop table test;
create table test(id bigint auto_increment, name varchar(255));
insert into test(name) values('hello');
insert into test(name) values('world');
select * from test; 

结果:

ID  NAME  
1   hello
2   world

58
投票

IDENTITY

现代方法使用

IDENTITY
类型,自动生成递增的 64 位长整数。

H2 中使用的单字语法是

SQL:2003
标准中定义的 GENERATED … AS IDENTITY 的缩写变体。请参阅 PDF 文档中的摘要SQL:2003 已发布。其他数据库正在实现此功能,例如 Postgres

CREATE TABLE event_ 
( 
    pkey_ IDENTITY NOT NULL PRIMARY KEY ,  -- ⬅ `identity` = auto-incrementing long integer.
    name_ VARCHAR NOT NULL ,
    start_ TIMESTAMP WITH TIME ZONE NOT NULL , 
    duration_ VARCHAR NOT NULL
) 
;

使用示例。无需为我们的

pkey
列值传递值,因为它是由 H2 自动生成的。

INSERT INTO event_ ( name_ , start_ , stop_ )
VALUES ( ? , ? , ? ) 
;

还有 Java。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
OffsetDateTime start = ZonedDateTime.of( 2021 , Month.JANUARY , 23 , 19 , 0 , 0 , 0 , z ).toOffsetDateTime() ; 
Duration duration = Duration.ofHours( 2 ) ;

myPreparedStatement.setString( 1 , "Java User Group" ) ;
myPreparedStatement.setObject( 2 , start ) ;
myPreparedStatement.setString( 3 , duration.toString() ) ; 

返回生成的密钥

Statement.RETURN_GENERATED_KEYS

您可以捕获插入命令执行期间生成的值。需要两个步骤。首先,在收到准备好的声明时传递旗帜

Statement.RETURN_GENERATED_KEYS

PreparedStatement pstmt = conn.prepareStatement( sql , Statement.RETURN_GENERATED_KEYS ) ;

Statement::getGeneratedKeys

第二步是在执行准备好的语句后调用

Statement::getGeneratedKeys
。您会得到一个
ResultSet
,其行是为创建的行生成的标识符。

示例应用程序

这是一个完整的示例应用程序。在 Java 14 上运行,并启用文本块预览功能以获取乐趣。使用H2版本1.4.200。

package work.basil.example;

import org.h2.jdbcx.JdbcDataSource;

import java.sql.*;
import java.time.*;
import java.util.Objects;

public class H2ExampleIdentity
{
    public static void main ( String[] args )
    {
        H2ExampleIdentity app = new H2ExampleIdentity();
        app.doIt();
    }

    private void doIt ( )
    {
        JdbcDataSource dataSource = Objects.requireNonNull( new JdbcDataSource() );  // Implementation of `DataSource` bundled with H2.
        dataSource.setURL( "jdbc:h2:mem:h2_identity_example_db;DB_CLOSE_DELAY=-1" ); // Set `DB_CLOSE_DELAY` to `-1` to keep in-memory database in existence after connection closes.
        dataSource.setUser( "scott" );
        dataSource.setPassword( "tiger" );

        String sql = null;

        try (
                Connection conn = dataSource.getConnection() ;
        )
        {
            sql = """
                  CREATE TABLE event_
                     ( 
                        id_ IDENTITY NOT NULL PRIMARY KEY,  -- ⬅ `identity` = auto-incrementing integer number.
                        title_ VARCHAR NOT NULL ,
                        start_ TIMESTAMP WITHOUT TIME ZONE NOT NULL ,
                        duration_ VARCHAR NOT NULL
                      )
                  ;
                  """;
            System.out.println( "sql:  \n" + sql );
            try ( Statement stmt = conn.createStatement() ; )
            {
                stmt.execute( sql );
            }

            // Insert row.
            sql = """
                  INSERT INTO event_ ( title_ , start_ , duration_ )
                  VALUES ( ? , ? , ? )
                  ;
                  """;
            try (
                    PreparedStatement pstmt = conn.prepareStatement( sql , Statement.RETURN_GENERATED_KEYS ) ;
            )
            {
                ZoneId z = ZoneId.of( "America/Montreal" );
                ZonedDateTime start = ZonedDateTime.of( 2021 , 1 , 23 , 19 , 0 , 0 , 0 , z );
                Duration duration = Duration.ofHours( 2 );

                pstmt.setString( 1 , "Java User Group" );
                pstmt.setObject( 2 , start.toOffsetDateTime() );
                pstmt.setString( 3 , duration.toString() );

                pstmt.executeUpdate();
                try (
                        ResultSet rs = pstmt.getGeneratedKeys() ;
                )
                {
                    while ( rs.next() )
                    {
                        int id = rs.getInt( 1 );
                        System.out.println( "generated key: " + id );
                    }
                }
            }

            // Query all.
            sql = "SELECT * FROM event_ ;";
            try (
                    Statement stmt = conn.createStatement() ;
                    ResultSet rs = stmt.executeQuery( sql ) ;
            )
            {
                while ( rs.next() )
                {
                    //Retrieve by column name
                    int id = rs.getInt( "id_" );
                    String title = rs.getString( "title_" );
                    OffsetDateTime odt = rs.getObject( "start_" , OffsetDateTime.class );  // Ditto, pass class for type-safety.
                    Instant instant = odt.toInstant();  // If you want to see the moment in UTC.
                    Duration duration = Duration.parse( rs.getString( "duration_" ) );

                    //Display values
                    ZoneId z = ZoneId.of( "America/Montreal" );
                    System.out.println( "id_" + id + " | start_: " + odt + " | duration: " + duration + " ➙ running from: " + odt.atZoneSameInstant( z ) + " to: " + odt.plus( duration ).atZoneSameInstant( z ) );
                }
            }
        }
        catch ( SQLException e )
        {
            e.printStackTrace();
        }
    }
}

接下来,运行时查看结果。

Instant
OffsetDateTime
ZonedDateTime

执行此操作时,我的 JVM 当前的默认时区是

America/Los_Angeles
。在存储时刻的时间点(魁北克省 2021 年 1 月 23 日晚上 7 点),
America/Los_Angeles
区域与 UTC 的偏移晚了 8 个小时。因此,H2 JDBC 驱动程序返回的
OffsetDateTime
对象被设置为
-08:00
的偏移量。这确实会分散注意力,所以在实际工作中,我会立即将
OffsetDateTime
转换为 UTC 的
Instant
或我想要的特定时区的
ZonedDateTime
。请清楚地理解
Instant
OffsetDateTime
ZonedDateTime
对象都代表同一个同时发生的时刻,即时间线上的同一点。每个人都通过不同的挂钟时间观看同一时刻。想象一下,加利福尼亚、魁北克和冰岛(其时区为 UTC,偏移量为零)的 3 个人都在电话会议中交谈,最后他们在同一个巧合时刻抬头看着各自墙上的时钟。

生成的密钥:1

id_1 |开始_: 2021-01-23T16:00-08:00 |持续时间:PT2H ➙ 运行时间:2021-01-23T19:00-05:00[美国/蒙特利尔] 至:2021-01-23T21:00-05:00[美国/蒙特利尔]


顺便说一句,在应用程序预订未来约会的实际工作中,我们将在 Java 和数据库中使用不同的数据类型。

我们会在 Java 中使用

LocalDateTime
ZoneId
。在数据库中,我们将使用类似于 SQL 标准类型
TIMESTAMP WITHOUT TIME ZONE
的数据类型,第二列表示预期时区的名称。当从数据库中检索值来构建计划日历时,我们会将时区应用于存储的日期时间以获取
ZonedDateTime
对象。这将允许我们预订一天中特定时间的预约,而不管该司法管辖区的政客对 UTC 偏移量做出的更改如何。


8
投票

非常简单:

id int auto_increment primary key

H2会自动创建Sequence对象


6
投票

您还可以使用

default
:

create table if not exists my(id int auto_increment primary key,s text);
insert into my values(default,'foo');

0
投票

对于

oracle 模式 (jdbc:h2:mem:testdb;Mode=Oracle)

你有一个不同的语法

默认生成为身份主键 示例:

   CREATE TABLE "ONLINE_ARCHIVE"
   (
       "TABLE_KY"            NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
       "OTHER_COLUMN"        VARCHAR2(4000)
   )

-7
投票
id bigint(size) zerofill not null auto_increment,
© www.soinside.com 2019 - 2024. All rights reserved.