Apply this sql script:
create table software (
id bigint not null,
name varchar(255),
description varchar(255),
constraint pk_software primary key (id))
;
create sequence software_seq;
Then this one:
alter sequence software_seq start with 1000;;
insert into software (id, name, description) values ( 1, 'Soft1', 'Description1');
Then when insert new software programatically (from java), got new software with id = 24
Why not with 1001? Since ‘alter sequence software_seq start with 1000;’
You have a few things wrong here.
First of all, just creating a sequence with a particular name doesn’t attach it to the table and column that you want using it. You need to change
software.idto usesoftware_seqfor default values:and you’ll want to change the sequence’s ownership too (unless of course you’re using the sequence in other places):
So you should:
Then when inserting, you’d either leave out the
id:or specify DEFAULT:
Your other problem is that
start withdoesn’t do what you think it does:If you want the sequence to start at 1000 then you can:
Alternatively, you could use
setval:Of course, you could also use
bigserial:So using
bigserialas theidcolumn type would set up all the sequence stuff for you. Then you’d set the starting value as before usingalter sequenceorsetval.