Autoincrement, but omit existing values in the column

前端 未结 3 1505
孤独总比滥情好
孤独总比滥情好 2021-01-24 11:46

I have a table:

create table DB.t1 (id  SERIAL,name varchar(255));

and insert some data:

insert into DB.t1 (name) values (\'nam         


        
3条回答
  •  春和景丽
    2021-01-24 12:21

    @erwin-brandstetter Won't it be faster to first find the missing value and then simply setval('t1_id_seq'::regclass, ), thus removing excessive nextval calls? Also, if the question is how to make ids unique, assigning this code for default value won't solve the problem.

    I'd suggest using unique constraint or primary key constraint and handle unique violation exception.

提交回复
热议问题