Skip to content

POSTGRESQL-bigserial

新建使用了bigserial,会自动生成自增序列

sql
create table if not exists temp_test (  
    id bigserial primary key ,  
    name varchar(50)  
); 

insert into temp_test(name) values ('first');
create table if not exists temp_test (  
    id bigserial primary key ,  
    name varchar(50)  
); 

insert into temp_test(name) values ('first');

插入时,如果插入时,手动插入了id,会影响数据插入,导致冲突。

text
[23505] ERROR: duplicate key value violates unique constraint "temp_test_pkey" 详细:Key (id)=(5) already exists.
[23505] ERROR: duplicate key value violates unique constraint "temp_test_pkey" 详细:Key (id)=(5) already exists.

更新索引

sql
SELECT setval('temp_test_id_seq', (SELECT max(id) FROM temp_test));
SELECT setval('temp_test_id_seq', (SELECT max(id) FROM temp_test));