How to create an auto-increment column in HSQLDB?

Member

by wilhelm , in category: Other , a year ago

How to create an auto-increment column in HSQLDB?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jesse , a year ago

@wilhelm 

You can use the IDENTITY column type to create an auto-increment column in HSQLDB. Here's an example of how to create a table with an auto-increment column:

1
2
3
4
CREATE TABLE my_table (
  id INTEGER IDENTITY,
  name VARCHAR(255)
);


The IDENTITY type allows you to specify the start value and the increment value for the auto-increment column. For example:

1
2
3
4
CREATE TABLE my_table (
  id INTEGER IDENTITY(1,1),
  name VARCHAR(255)
);


This will create an id column that starts at 1 and increments by 1 for each new row.


You can also specify a different data type for the auto-increment column, such as BIGINT or DOUBLE. For example:

1
2
3
4
CREATE TABLE my_table (
  id BIGINT IDENTITY(1,1),
  name VARCHAR(255)
);


Note that the auto-increment column must be defined as the primary key of the table. If you don't specify a primary key, HSQLDB will automatically create a hidden primary key column for you.

Member

by khalid , 4 months ago

@wilhelm 

To create an auto-increment column in HSQLDB, you can use the GENERATED BY DEFAULT AS IDENTITY constraint. Here's an example:


CREATE TABLE my_table ( id INTEGER GENERATED BY DEFAULT AS IDENTITY, name VARCHAR(255) );


In this example, the id column is defined as an auto-increment column using the GENERATED BY DEFAULT AS IDENTITY constraint. The id column will automatically generate a unique value for each new row inserted into the table.


You can also specify a starting value and an increment value for the auto-increment column. For example:


CREATE TABLE my_table ( id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), name VARCHAR(255) );


In this example, the id column will start with a value of 1 and increment by 1 for each new row inserted into the table.