doniyor
doniyor

Reputation: 37846

H2 sql how to set column to auto_increment

i am trying to set my ID to auto_increment, but it is not doing it. i saved 2 more data into db, all are still getting 0 id. as a result i am not able to set ID as primary key.

how is it possible? can i set the field to auto_increment with JPA annotation or so?

i even tried this command which is the first attempt:

  alter table user alter column id int not null auto_increment;

no affect.

can someone help me please

thanks in advance!

Upvotes: 3

Views: 3017

Answers (1)

Mohit Pandey
Mohit Pandey

Reputation: 3813

If you just want to set ID as a primary key which is auto generated,then sql uniqueidentifier for it.

CREATE TABLE userTable(userId uniqueidentifier primary key, userName nvarchar(50)) 
         --create a table having userId as a primary key 

   INSERT INTO userTable(userId, userName ) VALUES (NEWID(), 'mohit');
   INSERT INTO userTable(userId, userName ) VALUES (NEWID(), 'doniyor');

   SELECT * FROM userTable



Result will be:

userId                                             userName

{E8E0A79D-436F-49CB-BCEC-EC9E5D69F1BB}              mohit
{21081DFA-7DBB-46AF-A160-550631160C25}              doniyor

Upvotes: 1

Related Questions