Fast SQL query: the correct syntax for creating a table with a primary key in H2?

I am currently starting a new Java application using an H2 database , but I have some confusion about using basic SQL to create tables. How to create a table of records (rows) with unique, automatically increasing, non-zero, integer primary keys? One of the most basic things to do, but I'm not sure the right way to do this is with H2.

I blame them for my confusion (indicates more than one way to do the same thing between different databases, but cannot determine the correct path for H2): http://www.w3schools.com/Sql/sql_primarykey.asp http: // www.w3schools.com/Sql/sql_autoincrement.asp

+7
sql database create-table h2
source share
1 answer

If I read the H2 documentation correctly, this should work:

CREATE TABLE MyTableName(PKFieldName IDENTITY PRIMARY KEY, StringFieldName VARCHAR(255)) 

Basically, you just want to declare your key column IDENTITY .

For IDENTITY enter: http://www.h2database.com/html/datatypes.html#identity_type
For CREATE TABLE syntax, see: http://www.h2database.com/html/grammar.html#create_table

+9
source share

All Articles