Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

postgresql 重新设置自增id。 大多数时候没有立即生效

由于分库,需要配置某一个数据包起始位置。 比如a数据库起始1开始,比数据库起始1000开始

create table if not exists "public"."IdCards"
(
   "id" bigserial
 constraint IdCards_pk
         primary key,
   "coordinate" integer,
   "registerTime" timestamp without time zone  null default now(),
   "updateTime" timestamp without time zone null ,
   "removeTime" timestamp without time zone null
);
comment on table "IdCards" is '身份证表';
comment on column "public"."IdCards"."id" is '编号';
truncate "public"."IdCards";
DELETE FROM "public"."IdCards" where true;
select setval('"IdCards_id_seq"',100000, false) from "public"."IdCards";
-- select setval('"IdCards_id_seq"',1, false) from "public"."IdCards";
-- alter sequence "IdCards_id_seq" restart with 1 OWNED BY "public"."IdCards".id;
insert into "public"."IdCards" (coordinate)
    values
 (0);
select * from "IdCards";
-- select * from pg_tables;
-- d "public".IdCards

如上数据库脚本... alter sequence 是可以立即生效的,但是select setval('"IdCards_id_seq"',1, false) 没有立即生效...

但是我看别人电脑都是好好的.... wuwuwu


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
from "public"."IdCards";

这个不应该写,当然如果单独执行没有问题,下面这句单独是可以执行的

select setval('"IdCards_id_seq"',100000, false) from "public"."IdCards";

最好还是使用

alter sequence "IdCards_id_seq" restart with 1 OWNED BY "public"."IdCards".id;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...