HSQL identify auto increase ID

In HSQLDB you can use IDENTITY keyword to define an auto-increment column, normally, this is the primary key. Review below examples :

1. IDENTITY – Default

By default, the IDENTITY value is start with zero.

 
CREATE TABLE users (
  id INTEGER IDENTITY PRIMARY KEY,
  name VARCHAR(30),
  email  VARCHAR(50)
);

INSERT INTO users (name, email) VALUES ('mkyong', '[email protected]');
INSERT INTO users (name, email) VALUES ('alex', '[email protected]');
INSERT INTO users (name, email) VALUES ('joel', '[email protected]');

Output


0, mkyong, [email protected]
1, alex, [email protected]
2, joel, [email protected]

2. IDENTITY – Start With

The IDENTITY value is starting with 100 and increase by 1.


CREATE TABLE users (
  id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 100, INCREMENT BY 1) PRIMARY KEY,
  name VARCHAR(30),
  email  VARCHAR(50)
);

Output


100, mkyong, [email protected]
101, alex, [email protected]
102, joel, [email protected]

Reference

  1. HSQLDB – Identity Auto-Increment Columns

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
juan taday
5 years ago

how do I get the primary key when inserting data