postgresql设置主键为自增序列


postgresql设置主键为自增序列

  1. 对于才开始建表时,使用以下语句
    create table test
    (
    id serial PRIMARY KEY,
    name character varying(128)
    );
  2. 对于已经建立的表:
    1)CREATE SEQUENCE test_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
    2)alter table test alter column id set default nextval(‘test_id_seq’);
  3. 如果需要修改自增的序号:
    alter sequence s_seq restart with 7;

转载自:https://blog.csdn.net/weixin_43229107/article/details/84446465

You may also like...